Copy file to multiple remote computers

lee roberts 186 Reputation points
2021-03-30T15:56:47.527+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 96,266 Reputation points MVP
    2021-03-30T17:36:02.93+00:00

    Hi @lee roberts ,

    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

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. lee roberts 186 Reputation points
    2021-03-30T17:23:52.65+00:00
    $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}
    
    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-03-31T08:29:51.383+00:00

    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.

    0 comments No comments

  3. Steven Falls 1 Reputation point
    2022-10-25T18:57:10.15+00:00

    A few things I did to get this to work

    Changed:
    $dest = "C:\"

    To:
    $dest = "C$\"

    Then line 16 is backwards

    Changed:
    Copy-Item -path $name\$Source -Destination $dest

    To:
    Copy-Item -path $Source -Destination $name\$dest

    0 comments No comments

  4. Bo 41 Reputation points
    2023-03-22T00:46:13.82+00:00

    Hi,

    where is the script run?

    if it's on each computer where you need to copy the file, then why check if it's online?

    if it's on a remote computer, how does it work with the -destination "C:...." if not using a PSSession ?

    0 comments No comments