error Handling

Welcome Forums General PowerShell Q&A error Handling

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
2 years, 8 months ago.

  • Author
    Posts
  • #37344

    Participant
    Points: 1
    Rank: Member

    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

    Participant
    Points: 0
    Rank: Member

    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
    

The topic ‘error Handling’ is closed to new replies.