We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

401 Unauthorized error from Powershell to ServiceNow

Vikram3
Tera Guru

Hello Experts,

Don't know whether this is a right question asking here. But I need your assistance on this.

There is a powershell script which connects SN to customer_contact table to fetch the record via token concept. I don't know why I am getting below error. I've tried changing the token but still issue persists. Can anybody help me to sort this out.

VERBOSE: Getting customer contacts from SNOW ContactUri: https://{instance_name}.service-now.com/api/now/table/customer_contact?sysparm_query=u_portalenabled_1=yes&sysparm_fields=name,email,u_portalenabled_1,sys_id,active,user_name
VERBOSE: GET https://{instance_name}.service-now.com/api/now/table/customer_contact?sysparm_query=u_portalenabled_1=yes&sysparm_fields=name,email,u_portalenabled_1,sys_id,active,user_name with 0-byte payload
>> TerminatingError(Invoke-WebRequest): "The remote server returned an error: (401) Unauthorized."
>> TerminatingError(LogEvent): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: Error retreiving data from the SNOW web service: The remote server returned an error: (401) Unauthorized."
13 REPLIES 13

I did it via postman and it works fine. Really I don't know how to debug this. Can you help me via zoom call please.

Hi Vikram,

So definitely issue with the way API is called from powershell command.

I am not having any expertise in powershell scripts/command so may not be able to guide you.

But I believe the root cause will be found when you check the powershell script

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

dtoomey
Mega Contributor

This is an old thread at this point, but I was getting a similar error and the root cause was that my password contained a "$" character which, in a PowerShell script, requires the escape character preceding it: `

Alternatively, the password can be sandwiched in single quotes instead of double quotes, which may be a valid enhancement request for the ServiceNow REST API Explorer.

bgmoreira
Tera Expert

For my issue , i resolved with this

 

In my case when a create a new user to user in this powershell command my default password is so long (+- 128 caracters). I changed to a short password (16 caracteres) and the 401 problem is resolved. 

 

importante

 

I put this roles to user 

 

import_transformer

import_set_loader

import_scheduler

import_admin

 

and flag the option "Internal Integration User" in user register