Looking for a powershell script that can pull a file from a set of PC's and FTP

Solution 1:

Edited :

$down = "C:\Script\log\down-hosts.log"
$nofile = "C:\Script\log\no-file.log"
$computers = Get-Content "C:\Script\list\Computers.txt"
$TargetPath = "\\server\directory\directory\"
$SourceFileName = "file_name.csv"
foreach ($computer in $computers) {
  if ( Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue 
{
    $sourcefilePath = "\\$computer\c$\UPS CSV Exports\$SourceFileName"
    Write-Host "$computer is up"
    Write-Host "Copying $SourceFilePath ..."
    Try {
      If (Test-Path $SourceFilePath) {
         Move-Item $SourceFilePath "$TargetPath\$computer`_$SourceFileName" -force
      } Else {
        #Throw "$SourceFilePath does not exist"
        Write-Host "$computer file does not exist"
        "$computer $SourceFileName file does not exist" | Out-File $nofile -append
      }
    } Catch {
       Write-Host "Error: $($Error[0].Exception.Message)"
    }
  } Else {
    Write-Host "$computer is down"
    "$computer is down $(get-date)" | Out-File $down -append 
  }
}

Some new explanations :

  • Use of Test-Connection to test if host is up (no ping). - Kept this as it worked well

  • Use of New-Item not necessary.

  • Use of Move-Item instead of FTP protocol.

  • Added new log features: "$computer $SourceFileName file does not exist" | Out-File $nofile -append which offers a second log showing the file did not exist.

  • Added new log feature: "$computer is down $(get-date)" | Out-File $down -append which shows the computer is down but also stamps it with a date/time.