Skip to content

Commit 1f61779

Browse files
authored
Update Invoke-ServiceNowRestMethod.ps1
1 parent ec233ed commit 1f61779

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

ServiceNow/Private/Invoke-ServiceNowRestMethod.ps1

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function Invoke-ServiceNowRestMethod {
1717
[CmdletBinding(SupportsPaging)]
1818
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseBOMForUnicodeEncodedFile', '', Justification = 'issuees with *nix machines and no benefit')]
1919

20-
Param (
20+
param (
2121
[parameter()]
2222
[ValidateSet('Get', 'Post', 'Patch', 'Delete')]
2323
[string] $Method = 'Get',
@@ -48,6 +48,9 @@ function Invoke-ServiceNowRestMethod {
4848
[parameter()]
4949
[string] $FilterString,
5050

51+
[parameter()]
52+
[string] $Namespace,
53+
5154
[parameter()]
5255
[object[]] $Sort = @('opened_at', 'desc'),
5356

@@ -74,7 +77,11 @@ function Invoke-ServiceNowRestMethod {
7477
)
7578

7679
# get header/body auth values
77-
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
80+
if ($namespace) {
81+
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace
82+
} else {
83+
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
84+
}
7885

7986
$params.Method = $Method
8087
$params.ContentType = 'application/json'
@@ -93,8 +100,7 @@ function Invoke-ServiceNowRestMethod {
93100
if ( $SysId ) {
94101
$params.Uri += "/$SysId"
95102
}
96-
}
97-
else {
103+
} else {
98104
$params.Uri += $UriLeaf
99105
}
100106

@@ -153,8 +159,7 @@ function Invoke-ServiceNowRestMethod {
153159
try {
154160
$response = Invoke-WebRequest @params
155161
Write-Debug $response
156-
}
157-
catch {
162+
} catch {
158163
$ProgressPreference = $oldProgressPreference
159164
throw $_
160165
}
@@ -174,12 +179,10 @@ function Invoke-ServiceNowRestMethod {
174179
$content = $response.content | ConvertFrom-Json
175180
if ( $content.PSobject.Properties.Name -contains "result" ) {
176181
$records = @($content | Select-Object -ExpandProperty result)
177-
}
178-
else {
182+
} else {
179183
$records = @($content)
180184
}
181-
}
182-
else {
185+
} else {
183186
# invoke-webrequest didn't throw an error per se, but we didn't get content back either
184187
throw ('"{0} : {1}' -f $response.StatusCode, $response | Out-String )
185188
}
@@ -190,8 +193,7 @@ function Invoke-ServiceNowRestMethod {
190193
if ( $response.Headers.'X-Total-Count' ) {
191194
if ($PSVersionTable.PSVersion.Major -lt 6) {
192195
$totalRecordCount = [int]$response.Headers.'X-Total-Count'
193-
}
194-
else {
196+
} else {
195197
$totalRecordCount = [int]($response.Headers.'X-Total-Count'[0])
196198
}
197199
Write-Verbose "Total number of records for this query: $totalRecordCount"
@@ -215,25 +217,22 @@ function Invoke-ServiceNowRestMethod {
215217

216218
$end = if ( $totalRecordCount -lt $setPoint ) {
217219
$totalRecordCount
218-
}
219-
else {
220+
} else {
220221
$setPoint
221222
}
222223

223224
Write-Verbose ('getting {0}-{1} of {2}' -f ($params.body.sysparm_offset + 1), $end, $totalRecordCount)
224225
try {
225226
$response = Invoke-WebRequest @params -Verbose:$false
226-
}
227-
catch {
227+
} catch {
228228
$ProgressPreference = $oldProgressPreference
229229
throw $_
230230
}
231231

232232
$content = $response.content | ConvertFrom-Json
233233
if ( $content.PSobject.Properties.Name -contains "result" ) {
234234
$records += $content | Select-Object -ExpandProperty result
235-
}
236-
else {
235+
} else {
237236
$records += $content
238237
}
239238
}
@@ -249,18 +248,17 @@ function Invoke-ServiceNowRestMethod {
249248
switch ($Method) {
250249
'Get' {
251250
$ConvertToDateField = @('closed_at', 'expected_start', 'follow_up', 'opened_at', 'sys_created_on', 'sys_updated_on', 'work_end', 'work_start')
252-
ForEach ($SNResult in $records) {
253-
ForEach ($Property in $ConvertToDateField) {
254-
If (-not [string]::IsNullOrEmpty($SNResult.$Property)) {
255-
Try {
251+
foreach ($SNResult in $records) {
252+
foreach ($Property in $ConvertToDateField) {
253+
if (-not [string]::IsNullOrEmpty($SNResult.$Property)) {
254+
try {
256255
# 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
257256
$CultureDateTimeFormat = (Get-Culture).DateTimeFormat
258257
$DateFormat = $CultureDateTimeFormat.ShortDatePattern
259258
$TimeFormat = $CultureDateTimeFormat.LongTimePattern
260259
$DateTimeFormat = [string[]]@("$DateFormat $TimeFormat", 'yyyy-MM-dd HH:mm:ss')
261260
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None)
262-
}
263-
Catch {
261+
} catch {
264262
# If the local culture and universal formats both fail keep the property as a string (Do nothing)
265263
$null = 'Silencing a PSSA alert with this line'
266264
}
@@ -283,4 +281,4 @@ function Invoke-ServiceNowRestMethod {
283281
}
284282

285283
$records
286-
}
284+
}

0 commit comments

Comments
 (0)