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.

How can we pass credentials in a secure way in powershell script for connecting to Service now uisng rest api.

So199787
Kilo Contributor

I have a power shell script for upload attachment to Service now incident using Rest api.

In the script I have to pass username and password in a secure way instead of harded coded values.

 

Below is the script using.

$user = "xxx"
$pass = "xx"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')
# $headers.Add('Content-Type','text/plain')

# Specify endpoint uri

$uri = "https://xx-now.com/api/now/attachment/file?table_name=xx&table_sys_id=xxx&file_name=xxx.pdf"

# Specifiy file to attach
$fileToAttach = "xxx"

# Specify HTTP method (POST, PATCH, PUT)
$method = "POST"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -InFile $fileToAttach

# Print response
$response.RawContent

Can some one help.

5 REPLIES 5

Did you read the link? It explains what you need to do. In brief you should

- install the powershell module mentioned
- manually run the get credential in a powershell to store the password 
- modify your powershell script to use the get credential script to get the password stored in step 2.