@@ -4,58 +4,59 @@ function Get-ServiceNowTable
44 Param
55 (
66 # Name of the table we're querying (e.g. incidents)
7- [parameter (ParameterSetName = ' SpecifyConnectionFields' , mandatory = $false )]
7+ [parameter (Mandatory )]
8+ [parameter (ParameterSetName = ' SpecifyConnectionFields' )]
89 [parameter (ParameterSetName = ' UseConnectionObject' )]
910 [parameter (ParameterSetName = ' SetGlobalAuth' )]
11+ [ValidateNotNullOrEmpty ()]
1012 [string ]$Table ,
1113
1214 # sysparm_query param in the format of a ServiceNow encoded query string (see http://wiki.servicenow.com/index.php?title=Encoded_Query_Strings)
13- [parameter (ParameterSetName = ' SpecifyConnectionFields' , mandatory = $false )]
15+ [parameter (ParameterSetName = ' SpecifyConnectionFields' )]
1416 [parameter (ParameterSetName = ' UseConnectionObject' )]
1517 [parameter (ParameterSetName = ' SetGlobalAuth' )]
1618 [string ]$Query ,
1719
1820 # Maximum number of records to return
19- [parameter (ParameterSetName = ' SpecifyConnectionFields' , mandatory = $false )]
21+ [parameter (ParameterSetName = ' SpecifyConnectionFields' )]
2022 [parameter (ParameterSetName = ' UseConnectionObject' )]
2123 [parameter (ParameterSetName = ' SetGlobalAuth' )]
2224 [int ]$Limit = 10 ,
2325
2426 # Whether or not to show human readable display values instead of machine values
25- [parameter (ParameterSetName = ' SpecifyConnectionFields' , mandatory = $false )]
27+ [parameter (ParameterSetName = ' SpecifyConnectionFields' )]
2628 [parameter (ParameterSetName = ' UseConnectionObject' )]
2729 [parameter (ParameterSetName = ' SetGlobalAuth' )]
28- [ValidateSet (" true" , " false" , " all" )]
30+ [ValidateSet (" true" , " false" , " all" )]
2931 [string ]$DisplayValues = ' false' ,
3032
3133 # Credential used to authenticate to ServiceNow
32- [Parameter (ParameterSetName = ' SpecifyConnectionFields' , Mandatory = $True )]
34+ [Parameter (ParameterSetName = ' SpecifyConnectionFields' )]
3335 [ValidateNotNullOrEmpty ()]
3436 [PSCredential ]
3537 $ServiceNowCredential ,
3638
37- # The URL for the ServiceNow instance being used
39+ # The URL for the ServiceNow instance being used
3840 [Parameter (ParameterSetName = ' SpecifyConnectionFields' , Mandatory = $True )]
3941 [ValidateNotNullOrEmpty ()]
4042 [string ]
4143 $ServiceNowURL ,
4244
43- # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
45+ # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
4446 [Parameter (ParameterSetName = ' UseConnectionObject' , Mandatory = $True )]
4547 [ValidateNotNullOrEmpty ()]
4648 [Hashtable ]
4749 $Connection
4850 )
4951
50- # Get credential and ServiceNow REST URL
51- if ($Connection -ne $null )
52+ # Get credential and ServiceNow REST URL
53+ if ($null -ne $Connection )
5254 {
5355 $SecurePassword = ConvertTo-SecureString $Connection.Password - AsPlainText - Force
5456 $ServiceNowCredential = New-Object System.Management.Automation.PSCredential ($Connection.Username , $SecurePassword )
5557 $ServiceNowURL = ' https://' + $Connection.ServiceNowUri + ' /api/now/v1'
56-
5758 }
58- elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null )
59+ elseif ($null -ne $ServiceNowCredential -and $null -ne $ServiceNowURL )
5960 {
6061 $ServiceNowURL = ' https://' + $ServiceNowURL + ' /api/now/v1'
6162 }
@@ -64,7 +65,7 @@ function Get-ServiceNowTable
6465 $ServiceNowCredential = $Global :ServiceNowCredentials
6566 $ServiceNowURL = $global :ServiceNowRESTURL
6667 }
67- else
68+ else
6869 {
6970 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"
7071 }
@@ -75,8 +76,20 @@ function Get-ServiceNowTable
7576 $Body.sysparm_query = $Query
7677 }
7778
78- # Fire and return
79+ # Perform table query and capture results
7980 $Uri = $ServiceNowURL + " /table/$Table "
81+ $Result = (Invoke-RestMethod - Uri $Uri - Credential $ServiceNowCredential - Body $Body - ContentType " application/json" ).Result
82+
83+ # 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+ $SNResult .$Property = [datetime ]$SNResult .$Property
89+ }
90+ }
91+ }
8092
81- return (Invoke-RestMethod - Uri $Uri - Credential $ServiceNowCredential - Body $Body - ContentType " application/json" ).result
82- }
93+ # Return the results
94+ $Result
95+ }
0 commit comments