Skip to content

Commit 6316feb

Browse files
authored
Merge pull request #125 from Snow-Shell/proxy-support
Proxy support
2 parents 53586a9 + b23f62b commit 6316feb

File tree

4 files changed

+63
-7
lines changed

4 files changed

+63
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## v2.1
2+
- Add proxy support to `New-ServiceNowSession`, [#97](https://github.com/Snow-Shell/servicenow-powershell/issues/97).
3+
14
## v2.0
25
- Although still in the module for backward compatibility, `Set-ServiceNowAuth` is being replaced with `New-ServiceNowSession`. With this comes OAuth support, removal of global variables, and much more folks have asked for. The ability to provide credentials directly to functions has been retained for this release, but will be deprecated in a future release in favor of using `New-ServiceNowSession`.
36
- Support for different api versions. `Set-ServiceNowAuth` will continue to use v1 of the api, but `New-ServiceNowSession` defaults to the latest. Check out the `-ApiVersion` parameter of `New-ServiceNowSession`.

ServiceNow/Private/Get-ServiceNowAuth.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,27 @@ function Get-ServiceNowAuth {
3636
} else {
3737
$hashOut.Credential = $ServiceNowSession.Credential
3838
}
39+
40+
if ( $ServiceNowSession.Proxy ) {
41+
$hashOut.Proxy = $ServiceNowSession.Proxy
42+
if ( $ServiceNowSession.ProxyCredential ) {
43+
$hashOut.ProxyCredential = $ServiceNowSession.ProxyCredential
44+
} else {
45+
$hashOut.ProxyUseDefaultCredentials = $true
46+
}
47+
}
48+
3949
} elseif ( $Credential -and $ServiceNowURL ) {
4050
Write-Warning -Message 'This authentication path, providing URL and credential directly, will be deprecated in a future release. Please use New-ServiceNowSession.'
4151
$hashOut.Uri = 'https://{0}/api/now/v1' -f $ServiceNowURL
4252
$hashOut.Credential = $Credential
53+
4354
} elseif ( $Connection ) {
4455
$SecurePassword = ConvertTo-SecureString $Connection.Password -AsPlainText -Force
4556
$Credential = New-Object System.Management.Automation.PSCredential ($Connection.Username, $SecurePassword)
4657
$hashOut.Credential = $Credential
4758
$hashOut.Uri = 'https://{0}/api/now/v1' -f $Connection.ServiceNowUri
59+
4860
} else {
4961
throw "Exception: You must do one of the following to authenticate: `n 1. Call the New-ServiceNowSession cmdlet `n 2. Pass in an Azure Automation connection object `n 3. Pass in an endpoint and credential"
5062
}

ServiceNow/Public/New-ServiceNowSession.ps1

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Required for OAuth. Credential where the username is the Client ID and the pass
2020
.PARAMETER AccessToken
2121
Provide the access token directly if obtained outside of this module.
2222
23+
.PARAMETER Proxy
24+
Use a proxy server for the request, rather than connecting directly. Provide the full url.
25+
26+
.PARAMETER ProxyCredential
27+
Credential of user who can access Proxy. If not provided, the current user will be used.
28+
2329
.PARAMETER ApiVersion
2430
Specific API version to use. The default is the latest.
2531
@@ -43,6 +49,10 @@ Create a session with an existing access token and save it to a script-scoped va
4349
$session = New-ServiceNowSession -Url tenant.domain.com -Credential $mycred -ClientCredential $myClientCred -PassThru
4450
Create a session using OAuth and save it as a local variable to be provided to functions directly
4551
52+
.EXAMPLE
53+
New-ServiceNowSession -Url tenant.domain.com -Credential $mycred -Proxy http://1.2.3.4
54+
Create a session utilizing a proxy to connect
55+
4656
.INPUTS
4757
None
4858
@@ -64,15 +74,29 @@ function New-ServiceNowSession {
6474

6575
[Parameter(Mandatory, ParameterSetName = 'BasicAuth')]
6676
[Parameter(Mandatory, ParameterSetName = 'OAuth')]
77+
[Parameter(Mandatory, ParameterSetName = 'BasicAuthProxy')]
78+
[Parameter(Mandatory, ParameterSetName = 'OAuthProxy')]
6779
[Alias('Credentials')]
6880
[System.Management.Automation.PSCredential] $Credential,
6981

7082
[Parameter(Mandatory, ParameterSetName = 'OAuth')]
83+
[Parameter(Mandatory, ParameterSetName = 'OAuthProxy')]
7184
[System.Management.Automation.PSCredential] $ClientCredential,
7285

7386
[Parameter(Mandatory, ParameterSetName = 'AccessToken')]
87+
[Parameter(Mandatory, ParameterSetName = 'AccessTokenProxy')]
7488
[string] $AccessToken,
7589

90+
[Parameter(Mandatory, ParameterSetName = 'BasicAuthProxy')]
91+
[Parameter(Mandatory, ParameterSetName = 'OAuthProxy')]
92+
[Parameter(Mandatory, ParameterSetName = 'AccessTokenProxy')]
93+
[string] $Proxy,
94+
95+
[Parameter(ParameterSetName = 'BasicAuthProxy')]
96+
[Parameter(ParameterSetName = 'OAuthProxy')]
97+
[Parameter(ParameterSetName = 'AccessTokenProxy')]
98+
[System.Management.Automation.PSCredential] $ProxyCredential,
99+
76100
[Parameter()]
77101
[int] $ApiVersion,
78102

@@ -93,8 +117,15 @@ function New-ServiceNowSession {
93117
BaseUri = ('https://{0}/api/now{1}' -f $Url, $version)
94118
}
95119

96-
switch ($PSCmdLet.ParameterSetName) {
97-
'OAuth' {
120+
if ( $PSBoundParameters.ContainsKey('Proxy') ) {
121+
$newSession.Add('Proxy', $Proxy)
122+
if ( $PSBoundParameters.ContainsKey('ProxyCredential') ) {
123+
$newSession.Add('ProxyCredential', $ProxyCredential)
124+
}
125+
}
126+
127+
switch -Wildcard ($PSCmdLet.ParameterSetName) {
128+
'OAuth*' {
98129
$params = @{
99130
Uri = 'https://{0}/oauth_token.do' -f $Url
100131
Body = @{
@@ -107,19 +138,29 @@ function New-ServiceNowSession {
107138
Method = 'Post'
108139
}
109140

141+
# need to add this manually here, in addition to above, since we're making a rest call before our session is created
142+
if ( $PSBoundParameters.ContainsKey('Proxy') ) {
143+
$params.Add('Proxy', $Proxy)
144+
if ( $PSBoundParameters.ContainsKey('ProxyCredential') ) {
145+
$params.Add('ProxyCredential', $ProxyCredential)
146+
} else {
147+
$params.Add('ProxyUseDefaultCredentials', $true)
148+
}
149+
}
150+
110151
$token = Invoke-RestMethod @params
111152
$newSession.Add('AccessToken', $token.access_token)
112153
$newSession.Add('RefreshToken', $token.refresh_token)
113154
}
114-
'AccessToken' {
155+
156+
'AccessToken*' {
115157
$newSession.Add('AccessToken', $AccessToken)
116158
}
117-
'BasicAuth' {
159+
160+
'BasicAuth*' {
118161
$newSession.Add('Credential', $Credential)
119162
}
120-
'SSO' {
121163

122-
}
123164
Default {
124165

125166
}

ServiceNow/ServiceNow.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
RootModule = 'ServiceNow.psm1'
66

77
# Version number of this module.
8-
ModuleVersion = '2.0.0'
8+
ModuleVersion = '2.1.0'
99

1010
# ID used to uniquely identify this module
1111
GUID = 'b90d67da-f8d0-4406-ad74-89d169cd0633'

0 commit comments

Comments
 (0)