0

Simple script here, well at least I think it should be but I am having issues with the final results:

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

The issue is it never copies the file, where did I go wrong?

Thanks for the help in advance.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
NuckinFutz
  • 85
  • 5
  • 18

1 Answers1

2

Aside printing to screen, Write-Host does not send anything down the pipeline, so Copy-Item does not receive anything to copy.

Just call Copy-Item after Write-Host instead of piping the latter in the former:

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

Please also take a look at the formatting and the naming.

sodawillow
  • 12,497
  • 4
  • 34
  • 44
  • worked perfectly. Thank you very much. As always can count on this community to point a novice down the right patch. Thanks Soda. – NuckinFutz Feb 14 '17 at 12:21