- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 03:05 AM
I am not able to make API call via powershell. I thought it could be credential issue but the same cred works on python and postman. See error below.
PS C:\temp> .\snowint.ps1
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At C:\temp\snowint.ps1:34 char:13
+ $response = Invoke-RestMethod -Headers $headers -Method $method -Uri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
My code:
$user = 'admin'
$pwd = 'admin'
$env = "dev127921"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$uri = "https://"+ $env + ".service-now.com/api/now/table/sc_req_item?sysparm_query=active%3Dtrue&sysparm_display_value=number%3DRITM0000001&sysparm_limit=1"
$method = "get"
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
$response.RawContent
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 03:08 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2022 03:08 AM
Gotcha. I was just using another variable for password. Resolved now