【发布时间】:2020-01-14 00:11:22
【问题描述】:
我编写了一个脚本,其目的是从从 .txt 文件导入的计算机列表中列出目录中的所有文件,并将文件压缩到网络路径。不知道我写的对不对,希望大家看看有没有更好的办法告诉我。
脚本如下所示:
$destination = \\networkpath
$machines = (Get-Content "\\networkpath\machines.txt")
foreach ($machine in $machines) { $sourcefiles = gci "\\$machine\C$\localpath" }
foreach ($sourcefile in $sourcefiles) {
Compress-Archive -Path "\\$machine\C$\localpath" -Destinationpath "$Destination\$Sourcefile.zip" -CompresionLevel Optimal -Verbose }
该脚本适用于一台机器(文本文件中的最后一台),所以看起来它只是没有循环,可能是因为我正在使用这个双 foreach 循环。我尝试将 $sourcefiles 的输出通过管道传输到压缩存档,但这不会单独压缩文件。
我在这里出错的任何想法将不胜感激!
【问题讨论】:
标签: windows powershell scripting