PowerShell 脚本 - Get-ChildItem

发布于 2024-08-03 15:02:48 字数 1785 浏览 4 评论 0原文

我编写了一个脚本,用于从服务器归档日志文件。除了 Get-ChildItem 的递归性之外,我对一切都处于良好状态...

我似乎遇到的问题是,当 Get-ChildItem 不是递归的并且 -Ininclude 仅存在一个过滤器时,它会被忽略!或者,我做错了什么(可能)。

我已经清理了一些输出...

PS C:\foo> Get-childitem -path "c:\foo"

Name
----
bar1.doc
bar2.doc
bar3.doc
foo1.txt
foo2.txt
foo3.txt

PS C:\foo> Get-childitem -path "c:\foo" -Include *.txt
PS C:\foo> Get-childitem -path "c:\foo" -Include *.txt -recurse

Name
----
foo1.txt
foo2.txt
foo3.txt

Sooo???我有一个幻想,我所要做的就是分支到没有递归开关的脚本路径。 (顺便说一句,是否可以可变地应用参数,以避免重复的代码路径,其中唯一的可变性是 cmdlet 的参数?)无论如何

,除了 Get-ChildItem 的问题之外,这是我的脚本以确保完整性。

function MoveFiles()
{
    Get-ChildItem -Path $source -Recurse -Include $ext | where { $_.LastWriteTime -lt (Get-Date).AddDays(-$days) } | foreach {
        $SourceDirectory = $_.DirectoryName;
        $SourceFile = $_.FullName;
        $DestinationDirectory = $SourceDirectory -replace [regex]::Escape($source), $dest;
        $DestionationFile = $SourceFile -replace [regex]::Escape($source), $dest;

        if ($WhatIf){
            #Write-Host $SourceDirectory;
            #Write-Host $DestinationDirectory;
            Write-Host $SourceFile -NoNewline
            Write-Host " moved to " -NoNewline
            Write-Host $DestionationFile;
        }
        else{
            if ($DestinationDirectory)
            {
                if ( -not [System.IO.Directory]::Exists($DestinationDirectory)) {
                    [void](New-Item $DestinationDirectory -ItemType directory -Force);
                }
                Move-Item -Path $SourceFile -Destination $DestionationFile -Force;
            }
        }
    }
}

I've written a script that will be used for archiving log files from a server. I'm in pretty good shape with everything but the recursiveness or not of Get-ChildItem...

The issue I seem to be having is that when Get-ChildItem is not recursive and -Include is present with only one filter, it is ignored! Or, I'm doing something wrong (likely).

I've cleaned up the output a little...

PS C:\foo> Get-childitem -path "c:\foo"

Name
----
bar1.doc
bar2.doc
bar3.doc
foo1.txt
foo2.txt
foo3.txt

PS C:\foo> Get-childitem -path "c:\foo" -Include *.txt
PS C:\foo> Get-childitem -path "c:\foo" -Include *.txt -recurse

Name
----
foo1.txt
foo2.txt
foo3.txt

Sooo??? I had a fantasy where all I had to do was branch to a path of the script that did not have the recurse switch. (By the way, is it possible to variably apply parameters so as to avoid duplicated code paths where the only variability is the parameters to a cmdlet?)

Anyway, here is my script for completeness, in addition to my issue with Get-ChildItem.

function MoveFiles()
{
    Get-ChildItem -Path $source -Recurse -Include $ext | where { $_.LastWriteTime -lt (Get-Date).AddDays(-$days) } | foreach {
        $SourceDirectory = $_.DirectoryName;
        $SourceFile = $_.FullName;
        $DestinationDirectory = $SourceDirectory -replace [regex]::Escape($source), $dest;
        $DestionationFile = $SourceFile -replace [regex]::Escape($source), $dest;

        if ($WhatIf){
            #Write-Host $SourceDirectory;
            #Write-Host $DestinationDirectory;
            Write-Host $SourceFile -NoNewline
            Write-Host " moved to " -NoNewline
            Write-Host $DestionationFile;
        }
        else{
            if ($DestinationDirectory)
            {
                if ( -not [System.IO.Directory]::Exists($DestinationDirectory)) {
                    [void](New-Item $DestinationDirectory -ItemType directory -Force);
                }
                Move-Item -Path $SourceFile -Destination $DestionationFile -Force;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缘字诀 2024-08-10 15:02:48

答案在命令的完整描述中(get-help get-childitem -full):

包含参数有效
仅当命令包含
递归参数或路径通向
目录的内容,例如
C:\Windows\*,其中通配符
字符指定的内容
C:\Windows 目录。

所以下面的代码无需递归即可工作。

PS C:\foo> Get-childitem -path "c:\foo\*" -Include *.txt

The answer is in the full description of the command (get-help get-childitem -full):

The Include parameter is effective
only when the command includes the
Recurse parameter or the path leads to
the contents of a directory, such as
C:\Windows\*, where the wildcard
character specifies the contents of
the C:\Windows directory.

So the following would work without recurse.

PS C:\foo> Get-childitem -path "c:\foo\*" -Include *.txt
初相遇 2024-08-10 15:02:48

这是预期的行为,但不可否认的是令人困惑。从 Get-ChildItem 帮助文件中:

-Include <string[]>

仅检索指定的项目。此参数的值限定 Path 参数。输入路径元素或模式,例如“*.txt”。允许使用通配符。

仅当命令包含 Recurse 参数或路径指向目录内容时,Include 参数才有效,例如 C:\Windows*,其中通配符指定 C:\Windows 目录的内容。

附注>帮助目录-full |更多

希望这有帮助,

-Oisin

This is expected behaviour, but admittedly confusing. From the Get-ChildItem help file:

-Include <string[]>

Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\ Windows directory.

ps> help dir -full | more

Hope this helps,

-Oisin

×纯※雪 2024-08-10 15:02:48

我无法告诉您确切的原因(但我会继续寻找),但该行为记录在 Get-ChildItem 的 Get-Help 中:

-Include <string[]>
    Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path elem
    ent or pattern, such as "*.txt". Wildcards are permitted.

    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to th
    e contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\
    Windows directory.

I can't tell you the exact why of it (but I will keep looking), but the behavior is documented in the Get-Help for Get-ChildItem:

-Include <string[]>
    Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path elem
    ent or pattern, such as "*.txt". Wildcards are permitted.

    The Include parameter is effective only when the command includes the Recurse parameter or the path leads to th
    e contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\
    Windows directory.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文