How can we pass credentials in a secure way in powershell script for connecting to Service now uisng rest api.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-03-2019 12:40 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2019 08:19 AM
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.
