Skip to content

Commit e84c5f8

Browse files
authored
Update Get-ServiceNowTable.ps1
Updated with Rick's additional changes.
1 parent 5912af3 commit e84c5f8

File tree

1 file changed

+49
-42
lines changed

1 file changed

+49
-42
lines changed

ServiceNow/Public/Get-ServiceNowTable.ps1

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,73 @@
1-
function Get-ServiceNowTable
2-
{
1+
function Get-ServiceNowTable {
32
[OutputType([Array])]
43
Param
54
(
65
# Name of the table we're querying (e.g. incidents)
76
[parameter(Mandatory)]
8-
[parameter(ParameterSetName='SpecifyConnectionFields')]
9-
[parameter(ParameterSetName='UseConnectionObject')]
10-
[parameter(ParameterSetName='SetGlobalAuth')]
7+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
8+
[parameter(ParameterSetName = 'UseConnectionObject')]
9+
[parameter(ParameterSetName = 'SetGlobalAuth')]
1110
[ValidateNotNullOrEmpty()]
1211
[string]$Table,
1312

1413
# sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
15-
[parameter(ParameterSetName='SpecifyConnectionFields')]
16-
[parameter(ParameterSetName='UseConnectionObject')]
17-
[parameter(ParameterSetName='SetGlobalAuth')]
14+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
15+
[parameter(ParameterSetName = 'UseConnectionObject')]
16+
[parameter(ParameterSetName = 'SetGlobalAuth')]
1817
[string]$Query,
1918

2019
# Maximum number of records to return
21-
[parameter(ParameterSetName='SpecifyConnectionFields')]
22-
[parameter(ParameterSetName='UseConnectionObject')]
23-
[parameter(ParameterSetName='SetGlobalAuth')]
24-
[int]$Limit=10,
20+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
21+
[parameter(ParameterSetName = 'UseConnectionObject')]
22+
[parameter(ParameterSetName = 'SetGlobalAuth')]
23+
[int]$Limit = 10,
2524

2625
# Whether or not to show human readable display values instead of machine values
27-
[parameter(ParameterSetName='SpecifyConnectionFields')]
28-
[parameter(ParameterSetName='UseConnectionObject')]
29-
[parameter(ParameterSetName='SetGlobalAuth')]
30-
[ValidateSet("true","false","all")]
31-
[string]$DisplayValues='false',
26+
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
27+
[parameter(ParameterSetName = 'UseConnectionObject')]
28+
[parameter(ParameterSetName = 'SetGlobalAuth')]
29+
[ValidateSet("true", "false", "all")]
30+
[string]$DisplayValues = 'false',
3231

3332
# Credential used to authenticate to ServiceNow
34-
[Parameter(ParameterSetName='SpecifyConnectionFields')]
33+
[Parameter(ParameterSetName = 'SpecifyConnectionFields')]
3534
[ValidateNotNullOrEmpty()]
3635
[PSCredential]
3736
$ServiceNowCredential,
3837

3938
# The URL for the ServiceNow instance being used
40-
[Parameter(ParameterSetName='SpecifyConnectionFields', Mandatory=$True)]
39+
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
4140
[ValidateNotNullOrEmpty()]
4241
[string]
4342
$ServiceNowURL,
4443

4544
# Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
46-
[Parameter(ParameterSetName='UseConnectionObject', Mandatory=$True)]
45+
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory = $True)]
4746
[ValidateNotNullOrEmpty()]
4847
[Hashtable]
4948
$Connection
5049
)
5150

5251
# Get credential and ServiceNow REST URL
53-
if ($null -ne $Connection)
54-
{
52+
if ($null -ne $Connection) {
5553
$SecurePassword = ConvertTo-SecureString $Connection.Password -AsPlainText -Force
5654
$ServiceNowCredential = New-Object System.Management.Automation.PSCredential ($Connection.Username, $SecurePassword)
5755
$ServiceNowURL = 'https://' + $Connection.ServiceNowUri + '/api/now/v1'
5856
}
59-
elseif ($null -ne $ServiceNowCredential -and $null -ne $ServiceNowURL)
60-
{
57+
elseif ($null -ne $ServiceNowCredential -and $null -ne $ServiceNowURL) {
6158
$ServiceNowURL = 'https://' + $ServiceNowURL + '/api/now/v1'
6259
}
63-
elseif((Test-ServiceNowAuthIsSet))
64-
{
60+
elseif ((Test-ServiceNowAuthIsSet)) {
6561
$ServiceNowCredential = $Global:ServiceNowCredentials
6662
$ServiceNowURL = $global:ServiceNowRESTURL
6763
}
68-
else
69-
{
64+
else {
7065
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"
7166
}
7267

7368
# Populate the query
74-
$Body = @{'sysparm_limit'=$Limit;'sysparm_display_value'=$DisplayValues}
75-
if($Query){
69+
$Body = @{'sysparm_limit' = $Limit; 'sysparm_display_value' = $DisplayValues}
70+
if ($Query) {
7671
$Body.sysparm_query = $Query
7772
}
7873

@@ -81,18 +76,30 @@ function Get-ServiceNowTable
8176
$Result = (Invoke-RestMethod -Uri $Uri -Credential $ServiceNowCredential -Body $Body -ContentType "application/json").Result
8277

8378
# Convert specific fields to DateTime format
84-
$ConvertToDateField = @('closed_at','expected_start','follow_up','opened_at','sys_created_on','sys_updated_on','work_end','work_start')
85-
ForEach ($SNResult in $Result) {
86-
ForEach ($Property in $ConvertToDateField) {
87-
If (-not [string]::IsNullOrEmpty($SNResult.$Property)) {
88-
# 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
89-
$CultureDateTimeFormat = (Get-Culture).DateTimeFormat
90-
$DateFormat = $CultureDateTimeFormat.ShortDatePattern
91-
$TimeFormat = $CultureDateTimeFormat.LongTimePattern
92-
$DateTimeFormat = "$DateFormat $TimeFormat"
93-
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None)
94-
}
95-
}
79+
$ConvertToDateField = @('closed_at', 'expected_start', 'follow_up', 'opened_at', 'sys_created_on', 'sys_updated_on', 'work_end', 'work_start')
80+
ForEach ($SNResult in $Result) {
81+
ForEach ($Property in $ConvertToDateField) {
82+
If (-not [string]::IsNullOrEmpty($SNResult.$Property)) {
83+
Try {
84+
# 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
85+
$CultureDateTimeFormat = (Get-Culture).DateTimeFormat
86+
$DateFormat = $CultureDateTimeFormat.ShortDatePattern
87+
$TimeFormat = $CultureDateTimeFormat.LongTimePattern
88+
$DateTimeFormat = "$DateFormat $TimeFormat"
89+
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None)
90+
}
91+
Catch {
92+
Try {
93+
# Universal Format
94+
$DateTimeFormat = 'yyyy-MM-dd HH:mm:ss'
95+
$SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None)
96+
}
97+
Catch {
98+
# If the local culture and universal formats both fail keep the property as a string (Do nothing)
99+
}
100+
}
101+
}
102+
}
96103
}
97104

98105
# Return the results

0 commit comments

Comments
 (0)