LOG for Copy-item

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Wallace Lima Wallace Lima 2 years, 4 months ago.

  • Author
    Posts
  • #17731
    Profile photo of Wallace Lima
    Wallace Lima
    Participant

    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

  • #17732
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    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!

  • #17733
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

    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.

  • #17753
    Profile photo of Wallace Lima
    Wallace Lima
    Participant

    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

  • #17754
    Profile photo of Adnan Rashid
    Adnan Rashid
    Participant

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

  • #17771
    Profile photo of Wallace Lima
    Wallace Lima
    Participant

    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
    }
    }
    

You must be logged in to reply to this topic.