0

这里有简单的脚本,至少我认为应该是这样,但我对最终结果有疑问:

$a = Get-Content "content\file\location"
$destfile = "destination\of\file"
$source ="source\file\location"
$dest = "c$\destination"
$destfolder = "c:\folder\destination"

foreach ($a in $a) {
    if (Test-Connection $a -Count 1 -Quiet) {
        if (Test-Path "\\$a\$destfile") {
            Write-Host $a "File exists" -ForegroundColor Green
        } else {
            Write-Host $a "File is missing and will now be copied to $a\$destfolder" -ForegroundColor Red |
                Copy-Item $source -Destination "\\$a\$dest"
        }
    }
}

问题是它从不复制文件,我哪里出错了?

我在这里先向您的帮助表示感谢。

4

1 回答 1

2

除了打印到屏幕上,Write-Host不会发送任何内容,因此Copy-Item不会收到任何要复制的内容。

只需调用Copy-ItemafterWrite-Host而不是在前者中管道后者:

$computerList = Get-Content "content\file\location"
$destfile = "destination\of\file"
$source ="source\file\location"
$dest = "c$\destination"
$destfolder = "c:\folder\destination"

foreach ($computerName in $computerList) {
    if (Test-Connection $computerName -Count 1 -Quiet) {
        if (Test-Path "\\$computerName\$destfile") {
            Write-Host $computerName "File exists" -ForegroundColor Green
        } else {
            Write-Host $computerName "File is missing and will now be copied to $computerName\$destfolder" -ForegroundColor Red
            Copy-Item $source -Destination "\\$computerName\$dest"
        }
    }
}

还请查看格式和命名。

于 2017-02-13T20:06:20.737 回答