error Handling

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Yuan Li Yuan Li 8 months ago.

  • Author
    Posts
  • #37344
    Profile photo of amit aman
    amit aman
    Participant

    Hi Team,

    I am trying to print the error by using $Error variable, I am able to print the error, but want to know from which server, the following error is coming.
    I am using following code;

    Invoke-Command -ComputerName $vm -ScriptBlock {get-childitem -Path C:\inetpub\logs\LogFiles\ -recurse $vm | where-object {$_.lastwritetime -lt (get-date).addDays(-300)} | Foreach-Object { del $_.FullName } }

    Kindly help me to get the error message along with the server name.

  • #37347
    Profile photo of Yuan Li
    Yuan Li
    Participant

    Hey Amit

    I normally use something like

    Invoke-Command -ComputerName $vm -ScriptBlock {get-childitem -Path C:\inetpub\logs\LogFiles\ -recurse $vm | where-object {$_.lastwritetime -lt (get-date).addDays(-300)} | Foreach-Object { del $_.FullName } } -ErrorAction SilentlyContinue -ErrorVariable err
    $err.targetobject
    

You must be logged in to reply to this topic.