Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
FROM mcr.microsoft.com/powershell:latest
# LTS image
FROM mcr.microsoft.com/powershell:7.4-alpine-3.20 AS installer

RUN pwsh -Command 'Set-PSRepository PSGallery -InstallationPolicy Trusted; Install-Module ServiceNow -ErrorAction Stop'
# Opt-out of telemetry
ENV POWERSHELL_TELEMETRY_OPTOUT=1

# Install the ServiceNow module to a specific path
# We use -Scope CurrentUser to keep the path predictable for the 'COPY' step later
RUN pwsh -Command "Install-Module -Name 'ServiceNow' -Force -AllowClobber -Scope CurrentUser"

# -----------------------------------------------------------------------------

ENV SNOW_SERVER=${SNOW_SERVER}
ENV SNOW_TOKEN=${SNOW_TOKEN}
ENV SNOW_USER=${SNOW_USER}
ENV SNOW_PASS=${SNOW_PASS}
FROM mcr.microsoft.com/powershell:7.4-alpine-3.20

RUN apk add --no-cache icu-libs
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENV POWERSHELL_TELEMETRY_OPTOUT=1

SHELL ["pwsh"]
# Copy only the installed module from the builder stage
COPY --from=installer /root/.local/share/powershell/Modules/ServiceNow /usr/local/share/powershell/Modules/ServiceNow

# Create a non-root user
RUN adduser -D psworker
USER psworker
WORKDIR /home/psworker

ENTRYPOINT ["pwsh"]
6 changes: 0 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ $params = @{
New-ServiceNowIncident @params
```

### Azure Connection Object (Automation Integration Module Support)

The module can use the `Connection` parameter in conjunction with the included `ServiceNow-Automation.json` file for use as an Azure automation integration module. Details of the process is available at [Authoring Integration Modules for Azure Automation](https://azure.microsoft.com/en-us/blog/authoring-integration-modules-for-azure-automation).

The `Connection` parameter accepts a hashtable object that requires a username, password, and ServiceNowURL.

## Scope & Contributing

Contributions are gratefully received, so please feel free to submit a pull request with additional features or amendments.
Expand Down
18 changes: 7 additions & 11 deletions ServiceNow/Private/Get-ServiceNowAuth.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ function Get-ServiceNowAuth {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingConvertToSecureStringWithPlainText', '', Justification = 'requirement of azure automation')]

param (
[Parameter()]
[Alias('C')]
[hashtable] $Connection,

[Parameter()]
[Alias('N')]
[string] $Namespace = 'now',
Expand Down Expand Up @@ -83,12 +79,12 @@ function Get-ServiceNowAuth {
$hashOut.ProxyUseDefaultCredentials = $true
}
}
} elseif ( $Connection ) {
Write-Verbose 'connection'
# issue 248
$pair = '{0}:{1}' -f $Connection.Username, $Connection.Password
$hashOut.Headers = @{ Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) }
$hashOut.Uri = 'https://{0}/api/{1}/v1' -f $Connection.ServiceNowUri, $Namespace
# } elseif ( $Connection ) {
# Write-Verbose 'connection'
# # issue 248
# $pair = '{0}:{1}' -f $Connection.Username, $Connection.Password
# $hashOut.Headers = @{ Authorization = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair)) }
# $hashOut.Uri = 'https://{0}/api/{1}/v1' -f $Connection.ServiceNowUri, $Namespace
} elseif ( $env:SNOW_SERVER ) {
$hashOut.Uri = 'https://{0}/api/{1}' -f $env:SNOW_SERVER, $Namespace
if ( $env:SNOW_TOKEN ) {
Expand All @@ -101,7 +97,7 @@ function Get-ServiceNowAuth {
throw 'A ServiceNow server environment variable has been set, but authentication via SNOW_TOKEN or SNOW_USER/SNOW_PASS was not found'
}
} else {
throw "You must authenticate by either calling the New-ServiceNowSession cmdlet or passing in an Azure Automation connection object"
throw "You must authenticate by calling the New-ServiceNowSession cmdlet"
}
}

Expand Down
5 changes: 1 addition & 4 deletions ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@ function Invoke-ServiceNowRestMethod {
[Alias('DisplayValues')]
[string] $DisplayValue = 'true',

[Parameter()]
[hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)

# get header/body auth values
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace
$params = Get-ServiceNowAuth -S $ServiceNowSession -N $namespace

$params.Method = $Method
$params.ContentType = 'application/json'
Expand Down
7 changes: 1 addition & 6 deletions ServiceNow/Private/Invoke-TableIdLookup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ function Invoke-TableIdLookup {
[Alias('AS')]
[switch] $AsSysId,

[Parameter(ParameterSetName = 'IdSysId')]
[Parameter(ParameterSetName = 'TableIdSysId')]
[Alias('C')]
[hashtable] $Connection,

[Parameter(ParameterSetName = 'IdSysId')]
[Parameter(ParameterSetName = 'TableIdSysId')]
[Alias('S')]
Expand Down Expand Up @@ -85,7 +80,7 @@ function Invoke-TableIdLookup {
Table = $thisTable.Name
Filter = @('number', '-eq', $ID)
Property = 'sys_class_name', 'sys_id', 'number'
Connection = $Connection

ServiceNowSession = $ServiceNowSession
}

Expand Down
10 changes: 2 additions & 8 deletions ServiceNow/Public/Add-ServiceNowAttachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ Function Add-ServiceNowAttachment {
.PARAMETER PassThru
Return the newly created attachment details

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER ServiceNowSession
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.

Expand Down Expand Up @@ -104,23 +101,20 @@ Function Add-ServiceNowAttachment {
[Parameter()]
[switch] $PassThru,

[Parameter()]
[Hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)

begin {
$auth = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
$auth = Get-ServiceNowAuth -S $ServiceNowSession
$params = $auth
$params.UseBasicParsing = $true
$params.Method = 'POST'
}

process {

$thisTable, $thisID = Invoke-TableIdLookup -T $Table -I $ID -AsSysId -C $Connection -S $ServiceNowSession
$thisTable, $thisID = Invoke-TableIdLookup -T $Table -I $ID -AsSysId -S $ServiceNowSession

foreach ($thisFile in $File) {

Expand Down
5 changes: 1 addition & 4 deletions ServiceNow/Public/Export-ServiceNowAttachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,12 @@ Function Export-ServiceNowAttachment {
[Parameter(ParameterSetName = 'ToPipeline', Mandatory)]
[switch] $AsValue,

[Parameter()]
[Hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)

begin {
$authParams = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
$authParams = Get-ServiceNowAuth -S $ServiceNowSession
}

process {
Expand Down
9 changes: 1 addition & 8 deletions ServiceNow/Public/Get-ServiceNowAttachment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ Function Get-ServiceNowAttachment {
Array or multidimensional array of fields to sort on.
Each array should be of the format @(field, asc/desc).

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER ServiceNowSession
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.

Expand Down Expand Up @@ -91,9 +88,6 @@ Function Get-ServiceNowAttachment {
[ValidateNotNullOrEmpty()]
[object[]] $Sort,

[Parameter()]
[Hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)
Expand All @@ -104,14 +98,13 @@ Function Get-ServiceNowAttachment {
First = $PSCmdlet.PagingParameters.First
Skip = $PSCmdlet.PagingParameters.Skip
IncludeTotalCount = $PSCmdlet.PagingParameters.IncludeTotalCount
Connection = $Connection
ServiceNowSession = $ServiceNowSession
}
}

process {

$thisTable, $thisID = Invoke-TableIdLookup -T $Table -I $ID -AsSysId -C $Connection -S $ServiceNowSession
$thisTable, $thisID = Invoke-TableIdLookup -T $Table -I $ID -AsSysId -S $ServiceNowSession

$params.Filter = @('table_name', '-eq', $thisTable.Name), 'and', @('table_sys_id', '-eq', $thisID)

Expand Down
11 changes: 2 additions & 9 deletions ServiceNow/Public/Get-ServiceNowRecord.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@
Only valid when the Property parameter is set to 1 item.
Helpful when retrieving sys_id for example.

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER ServiceNowSession
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.

Expand Down Expand Up @@ -233,9 +230,6 @@ function Get-ServiceNowRecord {
[Parameter()]
[switch] $AsValue,

[Parameter()]
[hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)
Expand All @@ -253,7 +247,6 @@ function Get-ServiceNowRecord {
First = $PSCmdlet.PagingParameters.First
Skip = $PSCmdlet.PagingParameters.Skip
IncludeTotalCount = $PSCmdlet.PagingParameters.IncludeTotalCount
Connection = $Connection
ServiceNowSession = $ServiceNowSession
}

Expand Down Expand Up @@ -332,7 +325,7 @@ function Get-ServiceNowRecord {
$thisParams.UriLeaf = '/attachment'
}

[array]$result = Invoke-ServiceNowRestMethod @thisParams
[array]$result = Invoke-ServiceNowRestMethoduest @thisParams

if ( -not $result ) {
return
Expand Down Expand Up @@ -395,7 +388,7 @@ function Get-ServiceNowRecord {
$newVar.Value = $refValue
}
}

}

if ( $var.'sc_item_option.item_option_new.name' ) {
Expand Down
8 changes: 1 addition & 7 deletions ServiceNow/Public/Invoke-ServiceNowGraphQL.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
.PARAMETER Raw
Provide the server response as is instead of parsing out the application, schema, and service names.

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER ServiceNowSession
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.

Expand Down Expand Up @@ -68,9 +65,6 @@ function Invoke-ServiceNowGraphQL {
[Parameter()]
[switch] $Raw,

[Parameter()]
[hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)
Expand All @@ -85,7 +79,7 @@ function Invoke-ServiceNowGraphQL {
$fullQuery = ('{0} {{ {1} {{ {2} {{ {3} }}}}}}' -f $Operation, $Application, $Schema, $Query)
}

$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
$params = Get-ServiceNowAuth -S $ServiceNowSession

$params.Method = 'Post'
$params.ContentType = 'application/json'
Expand Down
7 changes: 0 additions & 7 deletions ServiceNow/Public/New-ServiceNowChangeRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
.PARAMETER ServiceNowSession
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER PassThru
If provided, the new record will be returned

Expand Down Expand Up @@ -112,9 +109,6 @@ function New-ServiceNowChangeRequest {
[Alias('CustomFields', 'InputData')]
[hashtable] $CustomField,

[Parameter()]
[Hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession,

Expand Down Expand Up @@ -159,7 +153,6 @@ function New-ServiceNowChangeRequest {
$params = @{
Table = 'change_request'
Values = $values
Connection = $Connection
ServiceNowSession = $ServiceNowSession
PassThru = $true
}
Expand Down
9 changes: 1 addition & 8 deletions ServiceNow/Public/New-ServiceNowChangeTask.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ function New-ServiceNowChangeTask {
.PARAMETER CustomField
Other key/value pairs to create the task which are not one of the existing parameters

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER PassThru
Return the newly created item details

Expand Down Expand Up @@ -63,9 +60,6 @@ function New-ServiceNowChangeTask {
[Parameter()]
[hashtable] $CustomField,

[Parameter()]
[Hashtable] $Connection,

[Parameter()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession,

Expand Down Expand Up @@ -114,12 +108,11 @@ function New-ServiceNowChangeTask {
Method = 'Post'
Table = 'change_task'
Values = $createValues
Connection = $Connection
ServiceNowSession = $ServiceNowSession
}

If ( $PSCmdlet.ShouldProcess($ShortDescription, 'Create new change task') ) {
$response = Invoke-ServiceNowRestMethod @params
$response = Invoke-ServiceNowRestMethoduest @params
If ( $PassThru ) {
$response.PSObject.TypeNames.Insert(0, "ServiceNow.ChangeTask")
$response
Expand Down
19 changes: 3 additions & 16 deletions ServiceNow/Public/New-ServiceNowConfigurationItem.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
.PARAMETER PassThru
Return the newly created CI

.PARAMETER Connection
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance

.PARAMETER ServiceNowSession
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.

Expand All @@ -39,7 +36,7 @@
#>
function New-ServiceNowConfigurationItem {

[CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess)]
[CmdletBinding(SupportsShouldProcess)]

Param(

Expand All @@ -61,11 +58,7 @@ function New-ServiceNowConfigurationItem {
[Parameter()]
[switch] $PassThru,

[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)]
[ValidateNotNullOrEmpty()]
[Hashtable] $Connection,

[Parameter(ParameterSetName = 'Session')]
[Parameter()]
[ValidateNotNullOrEmpty()]
[hashtable] $ServiceNowSession = $script:ServiceNowSession
)
Expand Down Expand Up @@ -109,13 +102,7 @@ function New-ServiceNowConfigurationItem {
Table = 'cmdb_ci'
Values = $TableEntryValues
PassThru = $true
}

if ($ServiceNowSession) {
$params.ServiceNowSession = $ServiceNowSession
}
else {
$params.Connection = $Connection
ServiceNowSession = $ServiceNowSession
}

If ( $PSCmdlet.ShouldProcess($Name, 'Create new configuration item') ) {
Expand Down
Loading