Q:

How to catch machine name on copy-item error? 

I'm trying to write a file and delete a file on multiple remote Windows machines.   If the machine is not available, ie.  not on line I want to capture that in an error log so that I have a list of problematic machine names to send to a help desk.   It's probably ugly but I'm close to having something that works.   Any clarification would be appreciated. 
$from="D:\whatever\machinfo"
$to="\\$machine\c\\scripts\"
output_file="D:\whatever\reports\$machine_writeerror.txt"

foreach($machine in(gc d:\whatever\machinfo\testworkstations.txt))
    {
    $machine
    IF (! $to) 
    {
    Copy-Item D:\whatever\machinfo\010RunGetmachinfo.bat \\$machine    \c\scripts -verbose
    # $errormsg="destination not found"
    $machine > output_file
    }
    ELSE
    {
    # DO NOTHING
    Remove-Item \\$machine\c\scripts\000dontrun.bat
    }
    }  

OK, I've rewritten this but I'm not doing something right.   I want an unique error file that contains either a single file for each machine connection failure or one file that contains the computername of all machines that could not be connected to.   I think the following is close (but not right). 
$logfile="D:\Projects\StoreControls\machinfo\reports\"+$machine+"_writeerror.txt"

foreach($machine in(gc d:\projects\StoreControls\machinfo\testworkstations.txt))
   {
   $machine
   If ( (Test-Connection -Computername $machine -Quiet -Count 1) -eq "False"){
   $machine > $logfile}
   Else{
   Remove-Item \\$machine\c\scripts\tasks\000dontStart.bat
   Copy-Item D:\Projects\StoreControls\machinfo\010RunPCsNServersGetmachinfo.bat \\$machine\c\scripts\tasks\ 
   }
   }

Changed "False" to $False after reading more on Test-Connection.   Works!   Thank you! 

A:

you can test the correct execution of a command by testing the automatic variable $? 
so you can use something like
Copy-Item D:\whatever\machinfo\010RunGetmachinfo.bat \\$machine\c\scripts 
if($?  -eq $false){
    # copy has failed
    "Copy error on $machine" |out-file d:\whatever\reports\$machine_writeerror.txt

}

by the way, a more efficient way could be to ping the host and see if it's alive :
if ( (Test-Connection -ComputerName $machine -Quiet  -Count 1) -eq  $false){
    #host not available

}

