2017-02-13 75 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