Skip to content

Commit 92530f7

Browse files
authored
Update Get-ServiceNowAuth.ps1
Add Namespace parameter to support different API endpoints; defaults to 'now'
1 parent 1f61779 commit 92530f7

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

ServiceNow/Private/Get-ServiceNowAuth.ps1

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ function Get-ServiceNowAuth {
1313
[CmdletBinding()]
1414
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'requirement of azure automation')]
1515

16-
Param (
16+
param (
1717
[Parameter()]
1818
[Alias('C')]
1919
[hashtable] $Connection,
2020

21+
[Parameter()]
22+
[Alias('N')]
23+
[string] $Namespace = 'now',
24+
2125
[Parameter()]
2226
[Alias('S')]
2327
[hashtable] $ServiceNowSession
@@ -30,7 +34,11 @@ function Get-ServiceNowAuth {
3034
process {
3135

3236
if ( $ServiceNowSession.Count -gt 0 ) {
33-
$hashOut.Uri = $ServiceNowSession.BaseUri
37+
if ($Namespace -ne 'now') {
38+
$hashOut.Uri = $($ServiceNowSession.BaseUri -split ('api'))[0] + 'api/' + $Namespace
39+
} else {
40+
$hashOut.Uri = $ServiceNowSession.BaseUri
41+
}
3442

3543
# check if we need a new access token
3644
if ( $ServiceNowSession.ExpiresOn -lt (Get-Date) -and $ServiceNowSession.RefreshToken -and $ServiceNowSession.ClientCredential ) {
@@ -46,7 +54,7 @@ function Get-ServiceNowAuth {
4654
refresh_token = $ServiceNowSession.RefreshToken.GetNetworkCredential().password
4755
}
4856
}
49-
57+
5058
$response = Invoke-RestMethod @refreshParams
5159

5260
$ServiceNowSession.AccessToken = New-Object System.Management.Automation.PSCredential('AccessToken', ($response.access_token | ConvertTo-SecureString -AsPlainText -Force))
@@ -64,8 +72,7 @@ function Get-ServiceNowAuth {
6472
$hashOut.Headers = @{
6573
'Authorization' = 'Bearer {0}' -f $ServiceNowSession.AccessToken.GetNetworkCredential().password
6674
}
67-
}
68-
else {
75+
} else {
6976
# issue 248
7077
$pair = '{0}:{1}' -f $ServiceNowSession.Credential.UserName, $ServiceNowSession.Credential.GetNetworkCredential().Password
7178
$hashOut.Headers = @{ Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) }
@@ -75,34 +82,28 @@ function Get-ServiceNowAuth {
7582
$hashOut.Proxy = $ServiceNowSession.Proxy
7683
if ( $ServiceNowSession.ProxyCredential ) {
7784
$hashOut.ProxyCredential = $ServiceNowSession.ProxyCredential
78-
}
79-
else {
85+
} else {
8086
$hashOut.ProxyUseDefaultCredentials = $true
8187
}
8288
}
83-
}
84-
elseif ( $Connection ) {
89+
} elseif ( $Connection ) {
8590
Write-Verbose 'connection'
8691
# issue 248
8792
$pair = '{0}:{1}' -f $Connection.Username, $Connection.Password
8893
$hashOut.Headers = @{ Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) }
89-
$hashOut.Uri = 'https://{0}/api/now/v1' -f $Connection.ServiceNowUri
90-
}
91-
elseif ( $env:SNOW_SERVER ) {
92-
$hashOut.Uri = 'https://{0}/api/now' -f $env:SNOW_SERVER
94+
$hashOut.Uri = 'https://{0}/api/{1}/v1' -f $Connection.ServiceNowUri, $Namespace
95+
} elseif ( $env:SNOW_SERVER ) {
96+
$hashOut.Uri = 'https://{0}/api/{1}' -f $env:SNOW_SERVER, $Namespace
9397
if ( $env:SNOW_TOKEN ) {
9498
$hashOut.Headers = @{
9599
'Authorization' = 'Bearer {0}' -f $env:SNOW_TOKEN
96100
}
97-
}
98-
elseif ( $env:SNOW_USER -and $env:SNOW_PASS ) {
101+
} elseif ( $env:SNOW_USER -and $env:SNOW_PASS ) {
99102
$hashOut.Credential = New-Object System.Management.Automation.PSCredential($env:SNOW_USER, ($env:SNOW_PASS | ConvertTo-SecureString -AsPlainText -Force))
100-
}
101-
else {
103+
} else {
102104
throw 'A ServiceNow server environment variable has been set, but authentication via SNOW_TOKEN or SNOW_USER/SNOW_PASS was not found'
103105
}
104-
}
105-
else {
106+
} else {
106107
throw "You must authenticate by either calling the New-ServiceNowSession cmdlet or passing in an Azure Automation connection object"
107108
}
108109
}

0 commit comments

Comments
 (0)