Skip to content

Commit f4e1dbf

Browse files
admcSHx - Simon Heather ADMadmcSHx - Simon Heather ADM
authored andcommitted
modify datetime field processing and add comment based help
1 parent 9c0347d commit f4e1dbf

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

ServiceNow/Public/Get-ServiceNowTable.ps1

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function Get-ServiceNowTable {
2-
[OutputType([Array])]
2+
<#
3+
.SYNOPSIS
4+
Retrieves multiple records for the specified table
5+
.DESCRIPTION
6+
The Get-ServiceNowTable function retrieves multiple 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+
[OutputType([Array])]
316
Param (
417
# Name of the table we're querying (e.g. incidents)
518
[parameter(Mandatory)]
@@ -21,7 +34,7 @@ function Get-ServiceNowTable {
2134
[parameter(ParameterSetName = 'SetGlobalAuth')]
2235
[int]$Limit = 10,
2336

24-
# Whether or not to show human readable display values instead of machine values
37+
# Whether to return manipulated display values rather than actual database values.
2538
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
2639
[parameter(ParameterSetName = 'UseConnectionObject')]
2740
[parameter(ParameterSetName = 'SetGlobalAuth')]
@@ -59,6 +72,7 @@ function Get-ServiceNowTable {
5972
elseif ((Test-ServiceNowAuthIsSet)) {
6073
$ServiceNowCredential = $Global:ServiceNowCredentials
6174
$ServiceNowURL = $global:ServiceNowRESTURL
75+
$ServiceNowDateFormat = $Global:ServiceNowDateFormat
6276
}
6377
else {
6478
throw "Exception: You must do one of the following to authenticate: `n 1. Call the Set-ServiceNowAuth cmdlet `n 2. Pass in an Azure Automation connection object `n 3. Pass in an endpoint and credential"
@@ -75,28 +89,27 @@ function Get-ServiceNowTable {
7589
$Result = (Invoke-RestMethod -Uri $Uri -Credential $ServiceNowCredential -Body $Body -ContentType "application/json").Result
7690

7791
# Convert specific fields to DateTime format
92+
$DefaultServiceNowDateFormat = 'yyyy-MM-dd HH:mm:ss'
7893
$ConvertToDateField = @('closed_at', 'expected_start', 'follow_up', 'opened_at', 'sys_created_on', 'sys_updated_on', 'work_end', 'work_start')
7994
ForEach ($SNResult in $Result) {
8095
ForEach ($Property in $ConvertToDateField) {
8196
If (-not [string]::IsNullOrEmpty($SNResult.$Property)) {
82-
Try {
83-
# 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
84-
$CultureDateTimeFormat = (Get-Culture).DateTimeFormat
85-
$DateFormat = $CultureDateTimeFormat.ShortDatePattern
86-
$TimeFormat = $CultureDateTimeFormat.LongTimePattern
87-
$DateTimeFormat = "$DateFormat $TimeFormat"
88-
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None)
89-
}
90-
Catch {
97+
If ($DisplayValues -eq $True) {
98+
# DateTime fields returned in the Service-Now instance system date format need converting to the local computers "culture" setting based upon the format specified
9199
Try {
92-
# Universal Format
93-
$DateTimeFormat = 'yyyy-MM-dd HH:mm:ss'
94-
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None)
100+
Write-Debug "Date Parsing field: $Property, value: $($SNResult.$Property) against global format $Global:ServiceNowDateFormat"
101+
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $Global:ServiceNowDateFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo)
95102
}
96103
Catch {
97-
# If the local culture and universal formats both fail keep the property as a string (Do nothing)
104+
Throw "Problem parsing date-time field $Property with value $($SNResult.$Property) against format $Global:ServiceNowDateFormat. " +
105+
"Please verify the DateFormat parameter matches the glide.sys.date_format property of the Service-Now instance"
98106
}
99107
}
108+
Else {
109+
# DateTime fields always returned as yyyy-MM-dd hh:mm:ss when sysparm_display_value is set to false
110+
Write-Debug "Date Parsing field: $Property, value: $($SNResult.$Property) against default format $DefaultServiceNowDateFormat"
111+
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DefaultServiceNowDateFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo)
112+
}
100113
}
101114
}
102115
}

0 commit comments

Comments
 (0)