我需要从服务器列表(computer-list.txt)中删除一列文件(在remove-files.txt中)。我尝试了以下方法,但没有奏效,我希望有人能帮助我纠正我的错误。从PowerShell中的服务器列表中删除文件列表
$SOURCE = "C:\powershell\copy\data"
$DESTINATION = "d$\copy"
$LOG = "C:\powershell\copy\logsremote_copy.log"
$REMOVE = Get-Content C:\powershell\copy\remove-list.txt
Remove-Item $LOG -ErrorAction SilentlyContinue
$computerlist = Get-Content C:\powershell\copy\computer-list.txt
foreach ($computer in $computerlist) {
Remove-Item \\$computer\$DESTINATION\$REMOVE -Recurse}
ERROR
Remove-Item : Cannot find path '\\NT-xxxx-xxxx\d$\copy\File1.msi, File2.msi, File3.exe, File4, File5.msi,' because it does not exist.
At C:\powershell\copy\REMOVE_DATA_x.ps1:13 char:12
+ Remove-Item <<<< \\$computer\$DESTINATION\$REMOVE -Recurse}
+ CategoryInfo : ObjectNotFound: (\\NT-xxxx-xxxxx\...-file1.msi,:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
尝试包装你的变量:删除项目“\\ $计算机\ $ DESTINATION \ $删除”-Recurse} –
是的,我同意,如果任何文件名包含空格或shell特殊字符。但是,这不是这个错误的原因。错误的原因是$ REMOVE是一个多值数组,扩展为文件名列表而不是单个文件名。 –