Skip to content

Commit 89a5c2d

Browse files
authored
Merge pull request #36 from X-Guardian/master
Modify DateTime field processing Depending Upon DisplayValues
2 parents 848423d + 5f25bf2 commit 89a5c2d

File tree

7 files changed

+55
-26
lines changed

7 files changed

+55
-26
lines changed

ServiceNow/Public/Get-ServiceNowChangeRequest.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ function Get-ServiceNowChangeRequest {
3636
[parameter(ParameterSetName='SetGlobalAuth')]
3737
[hashtable]$MatchContains=@{},
3838

39-
# Whether or not to show human readable display values instead of machine values
39+
# Whether to return manipulated display values rather than actual database values.
4040
[parameter(mandatory=$false)]
4141
[parameter(ParameterSetName='SpecifyConnectionFields')]
4242
[parameter(ParameterSetName='UseConnectionObject')]
4343
[parameter(ParameterSetName='SetGlobalAuth')]
4444
[ValidateSet("true","false", "all")]
45-
[string]$DisplayValues='true',
45+
[string]$DisplayValues='false',
4646

4747
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
4848
[ValidateNotNullOrEmpty()]

ServiceNow/Public/Get-ServiceNowConfigurationItem.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ function Get-ServiceNowConfigurationItem {
3636
[parameter(ParameterSetName='SetGlobalAuth')]
3737
[hashtable]$MatchContains=@{},
3838

39-
# Whether or not to show human readable display values instead of machine values
39+
# Whether to return manipulated display values rather than actual database values.
4040
[parameter(mandatory=$false)]
4141
[parameter(ParameterSetName='SpecifyConnectionFields')]
4242
[parameter(ParameterSetName='UseConnectionObject')]
4343
[parameter(ParameterSetName='SetGlobalAuth')]
4444
[ValidateSet("true","false", "all")]
45-
[string]$DisplayValues='true',
45+
[string]$DisplayValues='false',
4646

4747
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
4848
[ValidateNotNullOrEmpty()]

ServiceNow/Public/Get-ServiceNowIncident.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ function Get-ServiceNowIncident{
3636
[parameter(ParameterSetName='SetGlobalAuth')]
3737
[hashtable]$MatchContains=@{},
3838

39-
# Whether or not to show human readable display values instead of machine values
39+
# Whether to return manipulated display values rather than actual database values.
4040
[parameter(mandatory=$false)]
4141
[parameter(ParameterSetName='SpecifyConnectionFields')]
4242
[parameter(ParameterSetName='UseConnectionObject')]
4343
[parameter(ParameterSetName='SetGlobalAuth')]
4444
[ValidateSet("true","false", "all")]
45-
[string]$DisplayValues='true',
45+
[string]$DisplayValues='false',
4646

4747
# Credential used to authenticate to ServiceNow
4848
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]

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
}

ServiceNow/Public/Get-ServiceNowUser.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ function Get-ServiceNowUser{
3636
[parameter(ParameterSetName='SetGlobalAuth')]
3737
[hashtable]$MatchContains=@{},
3838

39-
# Whether or not to show human readable display values instead of machine values
39+
# Whether to return manipulated display values rather than actual database values.
4040
[parameter(mandatory=$false)]
4141
[parameter(ParameterSetName='SpecifyConnectionFields')]
4242
[parameter(ParameterSetName='UseConnectionObject')]
4343
[parameter(ParameterSetName='SetGlobalAuth')]
4444
[ValidateSet("true","false", "all")]
45-
[string]$DisplayValues='true',
45+
[string]$DisplayValues='false',
4646

4747
# Credential used to authenticate to ServiceNow
4848
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]

ServiceNow/Public/Get-ServiceNowUserGroup.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ function Get-ServiceNowUserGroup{
3636
[parameter(ParameterSetName='SetGlobalAuth')]
3737
[hashtable]$MatchContains=@{},
3838

39-
# Whether or not to show human readable display values instead of machine values
39+
# Whether to return manipulated display values rather than actual database values.
4040
[parameter(mandatory=$false)]
4141
[parameter(ParameterSetName='SpecifyConnectionFields')]
4242
[parameter(ParameterSetName='UseConnectionObject')]
4343
[parameter(ParameterSetName='SetGlobalAuth')]
4444
[ValidateSet("true","false", "all")]
45-
[string]$DisplayValues='true',
45+
[string]$DisplayValues='false',
4646

4747
# Credential used to authenticate to ServiceNow
4848
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]

ServiceNow/Public/Set-ServiceNowAuth.ps1

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ The URL of your Service-Now instance
1111
.PARAMETER Credentials
1212
Credentials to authenticate you to the Service-Now instance provided in the Url parameter
1313
14+
.PARAMETER DateFormat
15+
The date format specified in the Service-Now Instance, sys_properties table, property glide.sys.data_format. This is required
16+
to correctly convert datetime fields to the local computer locale format when the DisplayValues parameter of the Get-* functions
17+
is set to true
18+
1419
.EXAMPLE
1520
Set-ServiceNowAuth -Url tenant.service-now.com
1621
1722
.NOTES
1823
The URL should be the instance name portion of the FQDN for your instance. If you browse to https://yourinstance.service-now.com the URL required for the module is yourinstance.service-now.com
24+
25+
.LINK
26+
Service-Now Kingston Release REST API Reference: https://docs.servicenow.com/bundle/kingston-application-development/page/build/applications/concept/api-rest.html
27+
Service-Now Table API FAQ: https://hi.service-now.com/kb_view.do?sysparm_article=KB0534905
1928
#>
2029
function Set-ServiceNowAuth {
2130
[CmdletBinding()]
@@ -36,10 +45,17 @@ function Set-ServiceNowAuth {
3645
[Parameter(Mandatory = $true)]
3746
[ValidateNotNullOrEmpty()]
3847
[System.Management.Automation.PSCredential]
39-
$Credentials
48+
$Credentials,
49+
50+
[parameter(mandatory = $false)]
51+
[String]
52+
$DateFormat = (Get-Culture).DateTimeFormat.ShortDatePattern+' '+(Get-Culture).DateTimeFormat.LongTimePattern
4053
)
54+
4155
$Global:serviceNowUrl = 'https://' + $Url
4256
$Global:serviceNowRestUrl = $serviceNowUrl + '/api/now/v1'
4357
$Global:serviceNowCredentials = $Credentials
58+
$Global:ServiceNowDateFormat = $DateFormat
59+
4460
return $true
4561
}

0 commit comments

Comments
 (0)