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:

https://dbatools.io/agent/#:~:text=So%20it%20basically%20boils%20down%20to%20this:%20,%20%20Often%20(always?)%20works%20as%20expected