Issues with placing Catch output into a variable

Good afternoon all!

Here is my current opportunity; Using the following script I’m unable to “Catch” the errors into the $ErrorMessage2012R2 variable.

Thanks in advance for your input and rebukes.

Norm

SCRIPT

$servers2012R2 = Get-Datacenter -Name “WVHQ” | Get-VM | where { ($.PowerState -eq “PoweredOn”) -and ($.Guest.OSFullName -like “2012” -and $.Name -cnotlike “TEST*” -and $.Name -cnotlike “Test*” -and $.Name -cnotlike “test*” -and $.Name -cnotlike “W2K*”) } `
| Select-Object -ExpandProperty Name | Sort

#Using a foreach loop to determine current version of PowerShell with try catch to collect errors.

Clear-Content -Path D:\PowerShell_temp\psversion_2012R2_error.txt

$result2012R2 = foreach ($server2012R2 in $servers2012R2) {
Try {

Invoke-Command -Computername $server2012R2 -Scriptblock {$PSVersionTable.psversion}
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {

     $ErrorMessage2012R2 = "Server Name: $($_.TargetObject) $server2012R2, $($_.Exception.Message)" 
           

          }

}
Add-Content -Path D:\Powershell_temp\psversion_2012R2_error.txt -Value $ErrorMessage2012R2

OUTPUT FROM $ERROR

$error[0] | select -Property *
writeErrorStream : True
PSMessageDetails :
Exception : System.Management.Automation.Remoting.PSRemotingTransportException: Connecting to remote server FwSQL604 failed with the following error message : WinRM cannot
process the request. The following error with errorcode 0x80090322 occurred while using Kerberos authentication: An unknown security error occurred.
Possible causes are:
-The user name or password specified are invalid.
-Kerberos is used when no authentication method and no user name are specified.
-Kerberos accepts domain user names, but not local user names.
-The Service Principal Name (SPN) for the remote computer name and port does not exist.
-The client and remote computers are in different domains and there is no trust between the two domains.
After checking for the above issues, try the following:
-Check the Event Viewer for events related to authentication.
-Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
Note that computers in the TrustedHosts list might not be authenticated.
-For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help
topic.
TargetObject : FwSQL604
CategoryInfo : OpenError: (FwSQL604:String) [], PSRemotingTransportException
FullyQualifiedErrorId : -2144108387,PSSessionStateBroken
ErrorDetails : [FwSQL604] Connecting to remote server FwSQL604 failed with the following error message : WinRM cannot process the request. The following error with errorcode
0x80090322 occurred while using Kerberos authentication: An unknown security error occurred.
Possible causes are:
-The user name or password specified are invalid.
-Kerberos is used when no authentication method and no user name are specified.
-Kerberos accepts domain user names, but not local user names.
-The Service Principal Name (SPN) for the remote computer name and port does not exist.
-The client and remote computers are in different domains and there is no trust between the two domains.
After checking for the above issues, try the following:
-Check the Event Viewer for events related to authentication.
-Change the authentication method; add the destination computer to the WinRM TrustedHosts configuration setting or use HTTPS transport.
Note that computers in the TrustedHosts list might not be authenticated.
-For more information about WinRM configuration, run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help
topic.
InvocationInfo :
ScriptStackTrace :
PipelineIterationInfo : {}

This is something which is constantly confusing to people because try/catch does not work in powershell the way people expect. It only catches terminating errors, to ensure that the error in Invoke-Command results in a terminating error you can add -ErrorAction Stop to the call.

Invoke-Command -Computername $server2012R2 -Scriptblock {$PSVersionTable.psversion} -ErrorAction Stop
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
$ErrorMessage2012R2 = "Server Name: $($_.TargetObject) $server2012R2, $($_.Exception.Message)"
}

Alternatively you can use the ErrorVariable on the call and check that after its done rather than use the try/catch at all.

Hello James;
Well your was a good step in the right direction, however the $Message2012R2 variable contains only the last error message, know for certain this script produces more than one error.

Any thoughts as to why?

Thank you for your input

Norm

Hello James;
Got it to work, please note the following:

$result2012R2 = foreach ($server2012R2 in $servers2012R2) {
Try {

Invoke-Command -Computername $server2012R2 -Scriptblock {$PSVersionTable.psversion} -ErrorAction Stop
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {

     $Message2012R2 = "Error Message $($_.Exception.Message)" 
     Add-Content D:\PowerShell_temp\foobarxx.txt -Value $Message2012R2
         
          }

}

Thanks again for your help

Norm