I’m running an Invoke-Command which I have working as I want. In my test environment there is 1 computer that I turned off the ability to run commands against to log errors as I know production will have servers in all states.
I’ve attempted to use Try/Catch to grab the message I’m receiving (that it wont run against XYZ machine) but I cannot get it to spit anything out. I’d like for it to append a text document.
n Number of machines give me what I want and it is logged accordingly.
n Number of machines are offline/not allowing commands and it is logged accordingly.
Output:
[SERVER01] Connecting to remote server SERVER01 failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is
running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run
the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (SERVER01:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
Display Info That was Logged
Display Info That was Logged
Display Info That was Logged
Anyone have any idea how to catch that kind of message? I’ve tried it with $._Exception, $._Warning, and other variations I’ve found online. No luck.
But say I’m scanning against 500 servers. I want it to keep running against all the servers it can connect to and run against then tell me which it had issues with, so I could rectify those issues.
Seeing as I’d rather not stop the command if possible so it can keep running, is there another method to capture the error message into a separate log/file then?
I guess, would this stop all the other Invoke’s or just the one it had an error on?
You take a good return and duplicate the object in your catch. Now you are returning the same object structure for success and fails. This can now be analyzed in Powershell or Excel using filters:
In other words, this code, as is, will loop through the full list of $computers even if some error out in the middle.
Have you not tested the code before asking the question!!??
Second, read the blessed article posted. It specifically addresses this scenario, and I quote:
What if we want to process all files that do exist, yet be able to act individually on those that do not. One way to do that is to read data
from the Error pipeline:
$Error.Clear()
Get-Item -Path .\iis1.txt,.\not-there1.txt,.\iis2.txt,.\not-there2.txt,.\iis3.txt
$MyErrors = @()
If ($Error) {
$Error | % {
$Props = [ordered]@{
Name = $_.CategoryInfo.TargetName
Category = $_.CategoryInfo.Category
Exception = $_.Exception | Out-String
}
$MyErrors += New-Object -TypeName PSObject -Property $Props
}
}
$MyErrors | FT -Auto
In this example, I cleared the Error pipeline, ran the Get-Item cmdlet, then read through the Error records, extracted information I need and
may want to act on, and saved them to members of $MyErrors array.
You take a good return and duplicate the object in your catch. Now you are returning the same object structure for success and fails. This can now be analyzed in Powershell or Excel using filters: