2017-02-13 8 views
0

간단한 스크립트가 잘 적어도 나는 그것이해야한다고 생각하지만 최종 결과에 문제가 데 경우 : 문제는 결코 복사 파일입니다여기/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은 복사 할 내용을받지 못합니다.

그냥 대신 전자에서 후자를 배관의 Write-HostCopy-Item 전화 :

$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