Not Catching exceptions when I run powershell script in sql agent job

Hi All,

I used the below piece of command lines in my powershell scritp. The command line works fine when I run in powershell terminal .BUt when I use the same piece of code in sql agent job it not catching the exception.

Powershell version installed: 4.0

SQLVERSION: SQL 2012(11.0.7462)


try {
invoke-command -ComputerName DISPATCH -ScriptBlock {Get-ChildItem C:\Users -Exclude admin*,default*,public,temp*} -ErrorAction Stop

catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
Write-Host “Remoting disabled.”

Do I need to Load any assemblies ,need some inputs from the experts out there !!


Thanks in Advance.

Take a look at the following article and in the comments they are making suggestions regarding error handling: