Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Ignore errors in Powershell

Neha79
ServiceNow Employee
ServiceNow Employee

Hi,

 

Im running a script which pulls list of AD group members from a remote server. When i run the script for some groups im not able to fetch the members due to some error. I want the script to ignore the error and continue execution for rest other groups. For this i added $ErrorActionPreference= 'silentlycontinue'; to the script. This worked when i ran on the server but it fails from the servicenow instance. Any ideas here?

$groups= @("LISTOFGrouphere");

foreach ($group in $groups){

$ErrorActionPreference= 'silentlycontinue';

Get-ADGroupMember -Identity $group | Select samaccountname | ConvertTo-Json;

}

PS: I also tried Get-ADGroupMember -Identity $group -ErrorAction SilentlyContinue | Select samaccountname | ConvertTo-Json;

Thanks!

0 REPLIES 0