LOG for Copy-item

Hello guys

Thanks to you I was able to resolve many issues in these cases of copy (one-to-one, one-to-many, with all kinds of exceptions that I needed).

But still can not find a simple way to log success or failure

example:

$a = Get-Content c:\temp\servers.txt

foreach ($i in $a) {

Copy-Item "C:\temp\Source\*" -Recurse -Destination "\\$i\c$\destination\" -force -EA SilentlyContinue 
}

Works great, but I do not know how to extract a log if some of these servers is offline, if any file on the target can not be overwritten, etc.

sorry for misspelling

thank you very much

You can put any error messages generated by a cmdlet into a variable with the ErrorVariable common parameter. Then, you can save the error in a file by piping the variable into Out-File.

PS C:\Windows\system32> Get-Service kkkkkk -ErrorVariable e

Get-Service : Cannot find any service with service name 'kkkkkk'.
At line:1 char:1
+ Get-Service kkkkkk -ErrorVariable e
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (kkkkkk:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
 

PS C:\Windows\system32> $e

Get-Service : Cannot find any service with service name 'kkkkkk'.
At line:1 char:1
+ Get-Service kkkkkk -ErrorVariable e
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (kkkkkk:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand
 

PS C:\Windows\system32> $e | Out-File c:\error.txt

You can append other errors to the error varible like this: Get-Service bbbbbbbbb -ErrorVariable +e

You can also have a look at the help file: about_CommonParameters

I hope it helped!

You could also do a test-connection to each of the servers to ensure that those servers are actually available before doing your copy-item

you could then log the failed test connections with the error variable mentioned above instead of logging a failed copy or log that separately also - which could actually be for a different reason.

thanks, I try some scenarios and not capable use this examples

I try something like

$computer = Get-Content C:\temp\computer.txt

foreach ($c in $computer) {

if (Test-Connection -ComputerName $c -Count 1 -Quiet ) 
{
Copy-Item C:\temp\AKANE.JPG -Destination \\$c\c$\ranma\ -ErrorAction SilentlyContinue -ErrorVariable copy_log
}
if ($copy_log) {Write-Output "On computer $c connection ok, but copy has fail" | out-file c:\temp\copy_log.txt
}
else
{ 
Write-Output "Computer $c is Offline" | Out-File C:\temp\connection_log.txt
}
}

The function copy its ok, but logs are going crazy.

its not working, is like -errorvariable put only the last computer on TXT logs.

thank you guys

try -append to the out-file that should capture each computer

success!!

Thank you Rashid, I forgot of this trick -append, very nice.

thank you Szarka, I did not know how to use errorvariable, another one to my tools.

$computer = Get-Content C:\temp\computer.txt
 
foreach ($c in $computer) {
 
if (Test-Connection -ComputerName $c -Count 1 -Quiet ) 
{
Copy-Item C:\temp\AKANE.JPG -Destination \\$c\c$\ranma\ -ErrorAction SilentlyContinue -ErrorVariable A
     if($A) {Write-Output "falha na cópia do Servidor $c" | out-file C:\temp\copy_log.txt -Append}
}
else
{ 
Write-Output "Computer $c is Offline" | Out-File C:\temp\copy_log.txt -Append
}
}