繁体   English   中英

从Powershell中的服务器列表中删除文件列表

[英]Delete a list of files from a list of servers in powershell

我需要从服务器列表(computer-list.txt)中删除文件列表(在remove-files.txt中)。 我已经尝试了以下方法,但是没有用,我希望有人可以帮助我纠正错误。

$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}

错误


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

$ REMOVE是一个数组,其元素是remove-list.txt的每一行。 \\\\$computer\\$DESTINATION\\$REMOVE ,$ REMOVE扩展为数组元素的列表。 您的代码中没有任何内容告诉PowerShell迭代$ REMOVE的元素。 您需要一个内部循环:

foreach ($computer in $computerlist) {
  foreach ($file in $REMOVE) {
    Remove-Item "\\$computer\$DESTINATION\$file" -Recurse
  }
}

顺便说一句, -Recurse到底要完成什么? 您是否认为这将使Remove-Item遍历路径末尾的文件名数组? 那不是它的作用。 -Recurse开关告诉Remove-Item不仅删除路径指定的项目,还删除其所有子项。 如果要在文件系统上调用Remove-Item,则可以对目录使用-Recurse来删除整个子树(所有文件,子目录和子目录中的文件)。 如果(如您的示例所示)$ REMOVE仅包含文件而不包含目录,则不需要-Recurse。

另外,如果任何文件名包含空格或特殊字符,最好对路径双引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM