@@ -40,22 +40,21 @@ function Get-ServiceNowTable
4040 [string ]
4141 $ServiceNowURL ,
4242
43- # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
43+ # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
4444 [Parameter (ParameterSetName = ' UseConnectionObject' , Mandatory = $True )]
4545 [ValidateNotNullOrEmpty ()]
4646 [Hashtable ]
4747 $Connection
4848 )
4949
50- # Get credential and ServiceNow REST URL
51- if ($Connection -ne $null )
50+ # Get credential and ServiceNow REST URL
51+ if ($null -ne $Connection )
5252 {
5353 $SecurePassword = ConvertTo-SecureString $Connection.Password - AsPlainText - Force
5454 $ServiceNowCredential = New-Object System.Management.Automation.PSCredential ($Connection.Username , $SecurePassword )
5555 $ServiceNowURL = ' https://' + $Connection.ServiceNowUri + ' /api/now/v1'
56-
5756 }
58- elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null )
57+ elseif ($null -ne $ServiceNowCredential -and $null -ne $ServiceNowURL )
5958 {
6059 $ServiceNowURL = ' https://' + $ServiceNowURL + ' /api/now/v1'
6160 }
@@ -64,7 +63,7 @@ function Get-ServiceNowTable
6463 $ServiceNowCredential = $Global :ServiceNowCredentials
6564 $ServiceNowURL = $global :ServiceNowRESTURL
6665 }
67- else
66+ else
6867 {
6968 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"
7069 }
@@ -75,8 +74,20 @@ function Get-ServiceNowTable
7574 $Body.sysparm_query = $Query
7675 }
7776
78- # Fire and return
77+ # Perform table query and capture results
7978 $Uri = $ServiceNowURL + " /table/$Table "
79+ $Result = (Invoke-RestMethod - Uri $Uri - Credential $ServiceNowCredential - Body $Body - ContentType " application/json" ).Result
80+
81+ # Convert specific fields to DateTime format
82+ $ConvertToDateField = @ (' closed_at' , ' expected_start' , ' follow_up' , ' opened_at' , ' sys_created_on' , ' sys_updated_on' , ' work_end' , ' work_start' )
83+ ForEach ($SNResult in $Result ) {
84+ ForEach ($Property in $ConvertToDateField ) {
85+ If (-not [string ]::IsNullOrEmpty($SNResult .$Property )) {
86+ $SNResult .$Property = [datetime ]$SNResult .$Property
87+ }
88+ }
89+ }
8090
81- return (Invoke-RestMethod - Uri $Uri - Credential $ServiceNowCredential - Body $Body - ContentType " application/json" ).result
82- }
91+ # Return the results
92+ $Result
93+ }
0 commit comments