2017-02-13 76 views
0

简单这里脚本,以及至少我认为它应该是,但我有最终结果的问题:if/else语句和复制项问题

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

的问题是,它从来没有把文件拷贝,我哪里做错了?

感谢您的帮助提前。

回答

2

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

只需拨打Copy-ItemWrite-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" 
     } 
    } 
} 

也请看一看格式和命名。

+0

完美运作。非常感谢你。一如既往的可以指望这个社区指出一个新手正确的补丁。感谢苏打水。 – NuckinFutz