question

leeroberts-8039 avatar image
0 Votes"
leeroberts-8039 asked IanXue-MSFT edited

Copy file to multiple remote computers

Hi,

I'm trying to write a script that copies a file to the temp folder of multiple remove computers across the network. I want to to look in a CVS file for the host name, Ping the host name, If it gets a response then copies the file. If not then writes it out to another CSV file so I can copy it at a later date. When i run the below I get the error saying:

else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

This is the script im running:

$csv = Import-Csv "C:\temp\computers.csv"
$Source = "\\Server1\FileToCopy.exe"
$dest = "C:\temp\"
$Output = "C:\temp\missingworkstations.csv"
$items = @()

reads .csv for workstation NAME.

foreach ($line in $csv)
{

pings each Host. If true, Copy file.

 if (Test-Connection $line.Name -count 1 -quiet)
 {
     write-Host "true", $line.Name
     $name = "\\" + $line.Name
       
     #copies the file over to target machine
     Copy-Item -path $Source -Destination $dest

if ping fails, log which workstation and that workstation's IP in a new CSV.

 else
 {
     write-host "false" $line.Name $line.IP
     $items += New-Object psobject -Property @{IP=$line.IP; Name=$line.Name}
 }

}

exports array of workstations that were unreachable for manual processing at a later date.

$items | Export-Csv -NoTypeInformation -Path $Output}

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please post the entire script using the Code Sample tool on the site. The way you have posted it with interspersed comments and code sections makes it hard to recognize what is code and what isn't.

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @leeroberts-8039 ,

could you please try this (not tested by myself):

 $csv = Import-Csv "C:\temp\computers.csv"
 $Source = "\\server1\FileToCopy.exe"
 $dest = "C:\temp\"
 $Output = "C:\temp\missingworkstations.csv"
 $items = @()
 #reads .csv for workstation NAME.
 foreach ($line in $csv) {
         #pings each Host. If true, Copy file.
         if (Test-Connection $line.Name -count 1 -quiet) {
             write-Host "true", $line.Name
             $name = "\\" + $line.Name
             #copies the file over to target machine
             Copy-Item -path $Source -Destination $dest
             }
         #if ping fails, log which workstation and that workstation's IP in a new CSV.
         else {
             write-host "false" $line.Name $line.IP
             $items += New-Object psobject -Property @{IP=$line.IP; Name=$line.Name}
         }
     }
     #exports array of workstations that were unreachable for manual processing at a later date.
     $items | Export-Csv -NoTypeInformation -Path $Output


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

leeroberts-8039 avatar image
0 Votes"
leeroberts-8039 answered
 $csv = Import-Csv "C:\temp\computers.csv"
 $Source = "\\server1\FileToCopy.exe"
 $dest = "C:\temp\"
 $Output = "C:\temp\missingworkstations.csv"
 $items = @()
 #reads .csv for workstation NAME.
 foreach ($line in $csv)
 {
 #pings each Host. If true, Copy file.
     if (Test-Connection $line.Name -count 1 -quiet)
     {
         write-Host "true", $line.Name
         $name = "\\" + $line.Name
           
         #copies the file over to target machine
         Copy-Item -path $Source -Destination $dest
    
 #if ping fails, log which workstation and that workstation's IP in a new CSV.
     else
     {
         write-host "false" $line.Name $line.IP
         $items += New-Object psobject -Property @{IP=$line.IP; Name=$line.Name}
     }
 }
 #exports array of workstations that were unreachable for manual processing at a later date.
 $items | Export-Csv -NoTypeInformation -Path $Output}
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

The last closing brace "}" in Line 26 should be moved to Line 17 and the source path in Line 16 is always 'server1'.

  $csv = Import-Csv "C:\temp\computers.csv"
  $Source = "somefolder\FileToCopy.exe"
  $dest = "C:\temp\"
  $Output = "C:\temp\missingworkstations.csv"
  $items = @()
  #reads .csv for workstation NAME.
  foreach ($line in $csv)
  {
  #pings each Host. If true, Copy file.
      if (Test-Connection $line.Name -count 1 -quiet)
      {
          write-Host "true", $line.Name
          $name = "\\" + $line.Name
               
          #copies the file over to target machine
          Copy-Item -path $name\$Source -Destination $dest
       }
  #if ping fails, log which workstation and that workstation's IP in a new CSV.
      else
      {
          write-host "false" $line.Name $line.IP
          $items += New-Object psobject -Property @{IP=$line.IP; Name=$line.Name}
      }
  }
  #exports array of workstations that were unreachable for manual processing at a later date.
  $items | Export-Csv -NoTypeInformation -Path $Output

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.