Try catch not working

Good afternoon!
Running the following script:

$output1 = foreach ($name in $names) {
try {
Invoke-Command -ComputerName $name -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Silentlycontinue -ErrorVariable exception | Out-File -FilePath ‘D:\Powershell_temp\psversion.txt’ -Append
} catch {
$exception = $.Exception.Message
#$failedItem = $
Out-File -FilePath ‘D:\Powershell_temp\psversion_message.txt’ -Append -InputObject $exception
#Out-File -FilePath ‘D:\Powershell_temp\psversion_itemname.txt’ -Append -InputObject $failedItem


The script completes but there is nothing in the psversion_message.txt file. What am I missing?

Thank you for your input.


Hope the following will be of some help in solving this opportunity:

[4:32 PM]ndl@FWTS012:Powershell_Test $ clear-content -Path ‘D:\Powershell_temp\psversion.txt’
$output1 = foreach ($name in $names) {
try {
Invoke-Command -ComputerName $name -ScriptBlock {$PSVersionTable.psversion} -ErrorAction Silentlycontinue -errorvariable exception | Out-File -FilePath ‘D:\Powershell_temp\psversion.txt’ -Append
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
$.Exception.Message | out-file -FilePath `D:\PowerShell_temp\foo.out` -Append
Write-Host "Message: [$($
.Exception.Message)"] -ForegroundColor Red -BackgroundColor DarkBlue
$exception = $_.Exception.Message
Out-File -FilePath ‘D:\Powershell_temp\psversion_message.txt’ -Append -InputObject $exception


[4:40 PM]ndl@FWTS012:Powershell_Test $ $error[0].Exception.Message
Connecting to remote server TESTVM160418 failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the des
tination 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 i
s 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_Troubleshootin
g Help topic.

[4:41 PM]ndl@FWTS012:Powershell_Test $ $error[0].Exception | Get-Member

TypeName: System.Management.Automation.Remoting.PSRemotingTransportException

Name MemberType Definition

Equals Method bool Equals(System.Object obj), bool _Exception.Equals(System.Object obj)
GetBaseException Method System.Exception GetBaseException(), System.Exception _Exception.GetBaseException()
GetHashCode Method int GetHashCode(), int _Exception.GetHashCode()
GetObjectData Method void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context), void ISerializabl…
GetType Method type GetType(), type _Exception.GetType()
ToString Method string ToString(), string _Exception.ToString()
Data Property System.Collections.IDictionary Data {get;}
ErrorCode Property int ErrorCode {get;set;}
ErrorRecord Property System.Management.Automation.ErrorRecord ErrorRecord {get;}
HelpLink Property string HelpLink {get;set;}
HResult Property int HResult {get;set;}
InnerException Property System.Exception InnerException {get;}
Message Property string Message {get;}
Source Property string Source {get;set;}
StackTrace Property string StackTrace {get;}
TargetSite Property System.Reflection.MethodBase TargetSite {get;}
TransportMessage Property string TransportMessage {get;set;}
WasThrownFromThrowStatement Property bool WasThrownFromThrowStatement {get;set;}

Thank you in advance for your input!


This is your issue:

-ErrorAction Silentlycontinue

Basically, you’re telling PS to completely ignore all errors that crop up. This effectively negates the try block for the command you apply it to and renders it useless; it can’t catch a suppressed error.

By default, some errors in PS are terminating, and others are not. To consistently ensure that any errors trigger your catch block, you must instead use -ErrorAction Stop :slight_smile:

Thank you Joel, that was the issue.

Thanks again