Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 如何为管道中的每个元素运行脚本?_Shell_Powershell - Fatal编程技术网

Shell 如何为管道中的每个元素运行脚本?

Shell 如何为管道中的每个元素运行脚本?,shell,powershell,Shell,Powershell,上面的脚本可以正常工作,现在我想将其与FLOWING脚本组合成一个脚本: Get-ChildItem -recurse | Where {!$_.PSIsContainer -and ` $_.LastWriteTime -lt (get-date).AddDays(-31)} | Remove-Item -whatif Get-ChildItem -recurse | Where {$_.PSIsContainer -and ` @(Get-ChildItem -Lit $_.Fullnam

上面的脚本可以正常工作,现在我想将其与FLOWING脚本组合成一个脚本:

Get-ChildItem -recurse | Where {!$_.PSIsContainer -and `
$_.LastWriteTime -lt (get-date).AddDays(-31)} | Remove-Item -whatif

Get-ChildItem -recurse | Where {$_.PSIsContainer -and `
@(Get-ChildItem -Lit $_.Fullname -r | Where {!$_.PSIsContainer}).Length -eq 0} |
Remove-Item -recurse -whatif
但是,我总是收到错误消息:

Get-ChildItem -recurse | Where {$_.PSIsContainer -and `
@(Get-ChildItem -Lit $_.Fullname -r | Where {!$_.PSIsContainer}).Length -eq 0} |
$path = $_.Fullname
$shell = new-object -comobject "Shell.Application"
$item = $shell.Namespace(0).ParseName("$path")
$item.InvokeVerb("delete") -recurse -whatif

任何人都可以帮助我吗?

您需要在管道的最后一部分使用
Foreach对象
cmdlet(别名为Foreach)。此外,您不希望每次在管道中创建Shell.Application对象:

Expressions are only allowed as the first element of a pipeline.
At line:3 char:7

You must provide a value expression on the right-hand side of the '-' operator.
At line:6 char:28

Unexpected token 'recurse' in expression or statement.
At line:6 char:29

Unexpected token '-whatif' in expression or statement.
At line:6 char:37
也就是说,我不确定您为什么不使用
Remove Item
cmdlet,例如:

$shell = new-object -comobject "Shell.Application"
Get-ChildItem -recurse | 
    Where {$_.PSIsContainer -and `
           @(Get-ChildItem -Lit $_.Fullname -r | Where {!$_.PSIsContainer}).Length -eq 0} |
    Foreach {
        $item = $shell.Namespace(0).ParseName(($_.Fullname))
        $item.InvokeVerb("delete")
    }
要使其成为脚本,只需将上述命令放入.ps1文件中,如下所示:

Get-ChildItem . -r | Where {$_.PSIsContainer -and !$(Get-ChildItem $_.fullname)} | 
    Remove-Item -WhatIf
-- Contents of DeleteEmptyDirs.ps1 --
param([string]$path, [switch]$whatif)

Get-ChildItem $path -r | Where {$_.PSIsContainer -and !$(Get-ChildItem $_.fullname)} | 
    Remove-Item -WhatIf:$whatif
然后像这样调用:

Get-ChildItem . -r | Where {$_.PSIsContainer -and !$(Get-ChildItem $_.fullname)} | 
    Remove-Item -WhatIf
-- Contents of DeleteEmptyDirs.ps1 --
param([string]$path, [switch]$whatif)

Get-ChildItem $path -r | Where {$_.PSIsContainer -and !$(Get-ChildItem $_.fullname)} | 
    Remove-Item -WhatIf:$whatif

什么是完整的组合脚本?嗨,我已经发布了组合脚本。哦,是的,这看起来非常无效:(看起来目标是“为管道中的每个元素”执行”第二个片段,是吗?如果是,请参阅-简言之,
。| ForEach对象{“第二个脚本”}
-但也可以查看
foreach
。您可以发布最终脚本吗?您的脚本可以正常工作,但我始终会收到提示消息框,如何避免它?您会收到哪个提示消息框?尝试在Remove-Item cmdlet上使用
-Force
参数。我正在使用$Item.InvokeVerb(“delete”),消息框是“您确定要将此文件夹移动到Recyle Bin吗?”为什么不使用
删除项目
,这样您就不会收到提示?因为我不想永久删除它们,所以在执行此操作时可能会出错。稍后,我可以检查Recyle Bin以避免出错。