|
| 1 | +function Invoke-ServiceNowRestMethod { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Retrieves records for the specified table |
| 5 | + .DESCRIPTION |
| 6 | + The Get-ServiceNowTable function retrieves records for the specified table |
| 7 | + .INPUTS |
| 8 | + None |
| 9 | + .OUTPUTS |
| 10 | + System.Management.Automation.PSCustomObject |
| 11 | + .LINK |
| 12 | + Service-Now Kingston REST Table API: https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/inbound-rest/concept/c_TableAPI.html |
| 13 | + Service-Now Table API FAQ: https://hi.service-now.com/kb_view.do?sysparm_article=KB0534905 |
| 14 | +#> |
| 15 | + |
| 16 | + [OutputType([System.Management.Automation.PSCustomObject])] |
| 17 | + [CmdletBinding(SupportsPaging)] |
| 18 | + Param ( |
| 19 | + [parameter()] |
| 20 | + [ValidateSet('Get', 'Post', 'Patch', 'Delete')] |
| 21 | + [string] $Method = 'Get', |
| 22 | + |
| 23 | + # Name of the table we're querying (e.g. incidents) |
| 24 | + [parameter(Mandatory, ParameterSetName = 'Table')] |
| 25 | + [ValidateNotNullOrEmpty()] |
| 26 | + [string] $Table, |
| 27 | + |
| 28 | + [parameter(ParameterSetName = 'Table')] |
| 29 | + [ValidateNotNullOrEmpty()] |
| 30 | + [string] $SysId, |
| 31 | + |
| 32 | + [parameter(ParameterSetName = 'Uri')] |
| 33 | + [ValidateNotNullOrEmpty()] |
| 34 | + [string] $UriLeaf, |
| 35 | + |
| 36 | + # [parameter()] |
| 37 | + # [hashtable] $Header, |
| 38 | + |
| 39 | + [parameter()] |
| 40 | + [hashtable] $Values, |
| 41 | + |
| 42 | + # sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings) |
| 43 | + [Parameter()] |
| 44 | + [string] $Query, |
| 45 | + |
| 46 | + # Maximum number of records to return |
| 47 | + [Parameter()] |
| 48 | + [int] $Limit, |
| 49 | + |
| 50 | + # Fields to return |
| 51 | + [Parameter()] |
| 52 | + [Alias('Fields')] |
| 53 | + [string[]] $Properties, |
| 54 | + |
| 55 | + # Whether or not to show human readable display values instead of machine values |
| 56 | + [Parameter()] |
| 57 | + [ValidateSet('true', 'false', 'all')] |
| 58 | + [string] $DisplayValues = 'true', |
| 59 | + |
| 60 | + [Parameter()] |
| 61 | + [PSCredential] $Credential, |
| 62 | + |
| 63 | + [Parameter()] |
| 64 | + [string] $ServiceNowUrl, |
| 65 | + |
| 66 | + [Parameter()] |
| 67 | + [hashtable] $Connection, |
| 68 | + |
| 69 | + [Parameter()] |
| 70 | + [hashtable] $ServiceNowSession = $script:ServiceNowSession |
| 71 | + ) |
| 72 | + |
| 73 | + $getAuth = @{ |
| 74 | + Credential = $Credential |
| 75 | + ServiceNowUrl = $ServiceNowUrl |
| 76 | + Connection = $Connection |
| 77 | + ServiceNowSession = $ServiceNowSession |
| 78 | + } |
| 79 | + $params = Get-ServiceNowAuth @getAuth |
| 80 | + |
| 81 | + $params.Method = $Method |
| 82 | + $params.ContentType = 'application/json' |
| 83 | + |
| 84 | + if ( $Table ) { |
| 85 | + $params.Uri += "/table/$Table" |
| 86 | + if ( $SysId ) { |
| 87 | + $params.Uri += "/$SysId" |
| 88 | + } |
| 89 | + } else { |
| 90 | + $params.Uri += $UriLeaf |
| 91 | + } |
| 92 | + |
| 93 | + if ( $Method -eq 'Get') { |
| 94 | + $Body = @{ |
| 95 | + 'sysparm_display_value' = $DisplayValues |
| 96 | + } |
| 97 | + |
| 98 | + # Handle paging parameters |
| 99 | + # If -Limit was provided, write a warning message, but prioritize it over -First. |
| 100 | + # The value of -First defaults to [uint64]::MaxValue if not specified. |
| 101 | + # If no paging information was provided, default to the legacy behavior, which was to return 10 records. |
| 102 | + |
| 103 | + if ($PSBoundParameters.ContainsKey('Limit')) { |
| 104 | + Write-Warning "The -Limit parameter is deprecated, and may be removed in a future release. Use the -First parameter instead." |
| 105 | + $Body['sysparm_limit'] = $Limit |
| 106 | + } elseif ($PSCmdlet.PagingParameters.First -ne [uint64]::MaxValue) { |
| 107 | + $Body['sysparm_limit'] = $PSCmdlet.PagingParameters.First |
| 108 | + } else { |
| 109 | + $Body['sysparm_limit'] = 10 |
| 110 | + } |
| 111 | + |
| 112 | + if ($PSCmdlet.PagingParameters.Skip) { |
| 113 | + $Body['sysparm_offset'] = $PSCmdlet.PagingParameters.Skip |
| 114 | + } |
| 115 | + |
| 116 | + if ($PSCmdlet.PagingParameters.IncludeTotalCount) { |
| 117 | + # Accuracy is a double between 0.0 and 1.0 representing an estimated percentage accuracy. |
| 118 | + # 0.0 means we have no idea and 1.0 means the number is exact. |
| 119 | + |
| 120 | + # ServiceNow does return this information in the X-Total-Count response header, |
| 121 | + # but we're currently using Invoke-RestMethod to perform the API call, and Invoke-RestMethod |
| 122 | + # does not provide the response headers, so we can't capture this info. |
| 123 | + |
| 124 | + # To properly support this parameter, we'd need to fall back on Invoke-WebRequest, read the |
| 125 | + # X-Total-Count header of the response, and update this parameter after performing the API |
| 126 | + # call. |
| 127 | + |
| 128 | + # Reference: |
| 129 | + # https://developer.servicenow.com/app.do#!/rest_api_doc?v=jakarta&id=r_TableAPI-GET |
| 130 | + |
| 131 | + [double] $accuracy = 0.0 |
| 132 | + $PSCmdlet.PagingParameters.NewTotalCount($PSCmdlet.PagingParameters.First, $accuracy) |
| 133 | + } |
| 134 | + |
| 135 | + # Populate the query |
| 136 | + if ($Query) { |
| 137 | + $Body.sysparm_query = $Query |
| 138 | + } |
| 139 | + |
| 140 | + if ($Properties) { |
| 141 | + $Body.sysparm_fields = ($Properties -join ',').ToLower() |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if ( $Values ) { |
| 146 | + $Body = $Values | ConvertTo-Json |
| 147 | + |
| 148 | + #Convert to UTF8 array to support special chars such as the danish "�","�","�" |
| 149 | + $body = [System.Text.Encoding]::UTf8.GetBytes($Body) |
| 150 | + } |
| 151 | + |
| 152 | + if ( $Body ) { |
| 153 | + $params.Body = $Body |
| 154 | + } |
| 155 | + |
| 156 | + Write-Verbose ($params | ConvertTo-Json) |
| 157 | + |
| 158 | + $response = Invoke-RestMethod @params |
| 159 | + |
| 160 | + switch ($Method) { |
| 161 | + 'Get' { |
| 162 | + if ( $response.result ) { |
| 163 | + |
| 164 | + $result = $response | Select-Object -ExpandProperty result |
| 165 | + $ConvertToDateField = @('closed_at', 'expected_start', 'follow_up', 'opened_at', 'sys_created_on', 'sys_updated_on', 'work_end', 'work_start') |
| 166 | + ForEach ($SNResult in $Result) { |
| 167 | + ForEach ($Property in $ConvertToDateField) { |
| 168 | + If (-not [string]::IsNullOrEmpty($SNResult.$Property)) { |
| 169 | + Try { |
| 170 | + # Extract the default Date/Time formatting from the local computer's "Culture" settings, and then create the format to use when parsing the date/time from Service-Now |
| 171 | + $CultureDateTimeFormat = (Get-Culture).DateTimeFormat |
| 172 | + $DateFormat = $CultureDateTimeFormat.ShortDatePattern |
| 173 | + $TimeFormat = $CultureDateTimeFormat.LongTimePattern |
| 174 | + $DateTimeFormat = "$DateFormat $TimeFormat" |
| 175 | + $SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None) |
| 176 | + } Catch { |
| 177 | + Try { |
| 178 | + # Universal Format |
| 179 | + $DateTimeFormat = 'yyyy-MM-dd HH:mm:ss' |
| 180 | + $SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None) |
| 181 | + } Catch { |
| 182 | + # If the local culture and universal formats both fail keep the property as a string (Do nothing) |
| 183 | + $null = 'Silencing a PSSA alert with this line' |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } else { |
| 190 | + $response |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + { $_ -in 'Post', 'Patch' } { |
| 195 | + $result = $response | Select-Object -ExpandProperty result |
| 196 | + } |
| 197 | + |
| 198 | + 'Delete' { |
| 199 | + # nothing to do |
| 200 | + } |
| 201 | + |
| 202 | + Default { |
| 203 | + # we should never get here given the list of methods is set |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + $result |
| 208 | + # Invoke-RestMethod -Uri $Uri -Credential $Credential -Body $Body -ContentType "application/json" | Select-Object -ExpandProperty Result |
| 209 | +} |
0 commit comments