【发布时间】:2012-12-17 10:02:01
【问题描述】:
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
上面的脚本可以正常工作,现在我想把它和下面的脚本合并成一个脚本:
$path = "<path to file>"
$shell = new-object -comobject "Shell.Application"
$item = $shell.Namespace(0).ParseName("$path")
$item.InvokeVerb("delete")
这是我的组合脚本:
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
但是,我总是收到错误消息:
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
谁能帮帮我?
【问题讨论】:
-
什么是full组合脚本?
-
嗨,我已经发布了组合脚本。
-
哦,是的,这看起来非常无效:(看起来目标是“为管道中的每个元素”“执行”第二个 sn-p,是吗?如果是这样,请参阅ForEach -简而言之,
.. | ForEach-Object { "2nd script" }- 但也请参阅foreach。 -
你能发布最终的脚本吗?
标签: shell powershell