11function Get-ServiceNowTable {
22<#
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
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
1414#>
15- [OutputType ([Array ])]
15+
16+ [OutputType ([System.Management.Automation.PSCustomObject ])]
1617 Param (
1718 # Name of the table we're querying (e.g. incidents)
1819 [parameter (Mandatory )]
@@ -34,12 +35,12 @@ function Get-ServiceNowTable {
3435 [parameter (ParameterSetName = ' SetGlobalAuth' )]
3536 [int ]$Limit = 10 ,
3637
37- # Whether to return manipulated display values rather than actual database values.
38+ # Whether or not to show human readable display values instead of machine values
3839 [parameter (ParameterSetName = ' SpecifyConnectionFields' )]
3940 [parameter (ParameterSetName = ' UseConnectionObject' )]
4041 [parameter (ParameterSetName = ' SetGlobalAuth' )]
4142 [ValidateSet (" true" , " false" , " all" )]
42- [string ]$DisplayValues = ' false ' ,
43+ [string ]$DisplayValues = ' true ' ,
4344
4445 # Credential used to authenticate to ServiceNow
4546 [Parameter (ParameterSetName = ' SpecifyConnectionFields' )]
@@ -72,7 +73,6 @@ function Get-ServiceNowTable {
7273 elseif ((Test-ServiceNowAuthIsSet )) {
7374 $ServiceNowCredential = $Global :ServiceNowCredentials
7475 $ServiceNowURL = $global :ServiceNowRESTURL
75- $ServiceNowDateFormat = $Global :ServiceNowDateFormat
7676 }
7777 else {
7878 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"
@@ -89,27 +89,28 @@ function Get-ServiceNowTable {
8989 $Result = (Invoke-RestMethod - Uri $Uri - Credential $ServiceNowCredential - Body $Body - ContentType " application/json" ).Result
9090
9191 # Convert specific fields to DateTime format
92- $DefaultServiceNowDateFormat = ' yyyy-MM-dd HH:mm:ss'
9392 $ConvertToDateField = @ (' closed_at' , ' expected_start' , ' follow_up' , ' opened_at' , ' sys_created_on' , ' sys_updated_on' , ' work_end' , ' work_start' )
9493 ForEach ($SNResult in $Result ) {
9594 ForEach ($Property in $ConvertToDateField ) {
9695 If (-not [string ]::IsNullOrEmpty($SNResult .$Property )) {
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
96+ Try {
97+ # 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
98+ $CultureDateTimeFormat = (Get-Culture ).DateTimeFormat
99+ $DateFormat = $CultureDateTimeFormat.ShortDatePattern
100+ $TimeFormat = $CultureDateTimeFormat.LongTimePattern
101+ $DateTimeFormat = " $DateFormat $TimeFormat "
102+ $SNResult .$Property = [DateTime ]::ParseExact($ ($SNResult .$Property ), $DateTimeFormat , [System.Globalization.DateTimeFormatInfo ]::InvariantInfo, [System.Globalization.DateTimeStyles ]::None)
103+ }
104+ Catch {
99105 Try {
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)
106+ # Universal Format
107+ $DateTimeFormat = ' yyyy-MM-dd HH:mm:ss'
108+ $SNResult .$Property = [DateTime ]::ParseExact($ ($SNResult .$Property ), $DateTimeFormat , [System.Globalization.DateTimeFormatInfo ]::InvariantInfo, [System.Globalization.DateTimeStyles ]::None)
102109 }
103110 Catch {
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"
111+ # If the local culture and universal formats both fail keep the property as a string (Do nothing)
106112 }
107113 }
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- }
113114 }
114115 }
115116 }
0 commit comments