PowerShell 脚本 - Get-ChildItem
我编写了一个脚本,用于从服务器归档日志文件。除了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
答案在命令的完整描述中(get-help get-childitem -full):
所以下面的代码无需递归即可工作。
The answer is in the full description of the command (get-help get-childitem -full):
So the following would work without recurse.
这是预期的行为,但不可否认的是令人困惑。从 Get-ChildItem 帮助文件中:
仅检索指定的项目。此参数的值限定 Path 参数。输入路径元素或模式,例如“*.txt”。允许使用通配符。
仅当命令包含 Recurse 参数或路径指向目录内容时,Include 参数才有效,例如 C:\Windows*,其中通配符指定 C:\Windows 目录的内容。
附注>帮助目录-full |更多
希望这有帮助,
-Oisin
This is expected behaviour, but admittedly confusing. From the Get-ChildItem help file:
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
我无法告诉您确切的原因(但我会继续寻找),但该行为记录在 Get-ChildItem 的 Get-Help 中:
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: