我有以下脚本:
try {
Invoke-Command -Computer $Server -ScriptBlock {
param ($dir, $name)
Get-ChildItem -Path $dir |
Where {$_.Name -Match "$name"} |
Remove-Item -confirm:$false -Recurse -Verbose
} -ArgumentList $Directory, $DB
Write-Host "`r`nsuccessfull! " -foregroundcolor yellow -backgroundcolor black
}
catch {
write-host "`r`nFAILED!" -foregroundcolor red -backgroundcolor black
Write-Host "$($error[0])`r`n" -foregroundcolor magenta -backgroundcolor black
}
如果不存在要删除的文件...当前仍显示“成功”作为输出...我可以细说一下
"there was no files to delete" else "successfully deleted x number of files"
?
此外,还需要详细的recurse
,因为否则我将不得不手动进行确认,即使使用Confirm参数。我认为是因为我要删除的目标文件夹中有很多子项...
但是,对于其中每一个说
的项目,我都会收到大量的冗长消息VERBOSE:对目标执行“删除目录”操作 \ name1 \ subitem
VERBOSE:在以下位置执行“删除目录”操作 目标\ name1 \ subitem1
VERBOSE:执行操作“删除 目录”上的目标\ name1 \ subitem2
我能做到这一点,以便它仅在文件夹级别上打印详细信息,而不是为每个项目打印吗?
详细:在目标\ name1上执行“删除目录”操作
答案 0 :(得分:1)
将文件列表分配给您可以检查的变量。通过管道将所有内容都扔掉对于快速处理是很好的,但是如果您想在各个点提取状态信息,那么就不行了。
$delfiles = Get-ChildItem -Path $dir | Where {$_.Name -Match "$name"}
If ($delfiles) {
Write-Host "There are $($delfiles.count) to delete "
$delfiles | Remove-Item -confirm:$false -Recurse #put a try/catch here maybe
Write-Host "`r`nsuccessful! "
}
else {
write-host "there were no files to delete"
}