Skip to content

Commit 0318e43

Browse files
committed
Reformatted function with splatting, updated/expanded format.ps1xml in all Get functions
1 parent e4fbd18 commit 0318e43

File tree

7 files changed

+342
-149
lines changed

7 files changed

+342
-149
lines changed

ServiceNow/Public/Get-ServiceNowChangeRequest.ps1

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,36 @@ function Get-ServiceNowChangeRequest {
6060
$Connection
6161
)
6262

63-
$private:Query = New-ServiceNowQuery -OrderBy $private:OrderBy -OrderDirection $private:OrderDirection -MatchExact $private:MatchExact -MatchContains $private:MatchContains
63+
# Query Splat
64+
$newServiceNowQuerySplat = @{
65+
OrderBy = $OrderBy
66+
MatchExact = $MatchExact
67+
OrderDirection = $OrderDirection
68+
MatchContains = $MatchContains
69+
}
70+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
6471

65-
66-
if ($Connection -ne $null) {
67-
$private:result = Get-ServiceNowTable -Table 'change_request' -Query $private:Query -Limit $private:Limit -DisplayValues $private:DisplayValues -Connection $Connection
72+
# Table Splat
73+
$getServiceNowTableSplat = @{
74+
Table = 'change_request'
75+
Query = $Query
76+
Limit = $Limit
77+
DisplayValues = $DisplayValues
6878
}
69-
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null) {
70-
$private:result = Get-ServiceNowTable -Table 'change_request' -Query $private:Query -Limit $private:Limit -DisplayValues $private:DisplayValues -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
79+
80+
# Update the Table Splat if the parameters have values
81+
if ($null -ne $PSBoundParameters.Connection)
82+
{
83+
$getServiceNowTableSplat.Add('Connection',$Connection)
7184
}
72-
else {
73-
$private:result = Get-ServiceNowTable -Table 'change_request' -Query $private:Query -Limit $private:Limit -DisplayValues $private:DisplayValues
85+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
86+
{
87+
$getServiceNowTableSplat.Add('ServiceNowCredential',$ServiceNowCredential)
88+
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7489
}
7590

76-
# Add the custom type to the change request to enable a view
77-
$private:result | ForEach-Object {$_.psobject.TypeNames.Insert(0, "ServiceNow.ChangeRequest")}
78-
return $private:result
79-
}
91+
# Perform query and return each object in the format.ps1xml format
92+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
93+
$Result | ForEach-Object{$_.PSObject.TypeNames.Insert(0,"ServiceNow.ChangeRequest")}
94+
$Result
95+
}

ServiceNow/Public/Get-ServiceNowConfigurationItem.ps1

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,36 @@ function Get-ServiceNowConfigurationItem {
6060
$Connection
6161
)
6262

63-
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
63+
# Query Splat
64+
$newServiceNowQuerySplat = @{
65+
OrderBy = $OrderBy
66+
MatchExact = $MatchExact
67+
OrderDirection = $OrderDirection
68+
MatchContains = $MatchContains
69+
}
70+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
6471

65-
if ($Connection -ne $null) {
66-
$result = Get-ServiceNowTable -Table 'cmdb_ci' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -Connection $Connection
72+
# Table Splat
73+
$getServiceNowTableSplat = @{
74+
Table = 'cmdb_ci'
75+
Query = $Query
76+
Limit = $Limit
77+
DisplayValues = $DisplayValues
6778
}
68-
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null) {
69-
$result = Get-ServiceNowTable -Table 'cmdb_ci' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
79+
80+
# Update the Table Splat if the parameters have values
81+
if ($null -ne $PSBoundParameters.Connection)
82+
{
83+
$getServiceNowTableSplat.Add('Connection',$Connection)
7084
}
71-
else {
72-
$result = Get-ServiceNowTable -Table 'cmdb_ci' -Query $Query -Limit $Limit -DisplayValues $DisplayValues
85+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
86+
{
87+
$getServiceNowTableSplat.Add('ServiceNowCredential',$ServiceNowCredential)
88+
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7389
}
7490

75-
76-
# Set the default property set for the table view
77-
$DefaultProperties = @('name', 'category', 'subcategory')
78-
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultProperties)
79-
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
80-
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
81-
return $result
91+
# Perform query and return each object in the format.ps1xml format
92+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
93+
$Result | ForEach-Object{$_.PSObject.TypeNames.Insert(0,"ServiceNow.ConfigurationItem")}
94+
$Result
8295
}

ServiceNow/Public/Get-ServiceNowIncident.ps1

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,36 @@ function Get-ServiceNowIncident{
6363
$Connection
6464
)
6565

66-
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
66+
# Query Splat
67+
$newServiceNowQuerySplat = @{
68+
OrderBy = $OrderBy
69+
OrderDirection = $OrderDirection
70+
MatchExact = $MatchExact
71+
MatchContains = $MatchContains
72+
}
73+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
74+
75+
# Table Splat
76+
$getServiceNowTableSplat = @{
77+
Table = 'incident'
78+
Query = $Query
79+
Limit = $Limit
80+
DisplayValues = $DisplayValues
81+
}
6782

68-
if ($Connection -ne $null)
83+
# Update the splat if the parameters have values
84+
if ($null -ne $PSBoundParameters.Connection)
6985
{
70-
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -Connection $Connection
86+
$getServiceNowTableSplat.Add('Connection',$Connection)
7187
}
72-
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
88+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
7389
{
74-
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
90+
$getServiceNowTableSplat.Add('ServiceNowCredential',$ServiceNowCredential)
91+
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7592
}
76-
else
77-
{
78-
$result = Get-ServiceNowTable -Table 'incident' -Query $Query -Limit $Limit -DisplayValues $DisplayValues
79-
}
80-
81-
# Set the default property set for the table view
82-
$DefaultProperties = @('number', 'short_description', 'opened_at')
83-
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultProperties)
84-
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
85-
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
8693

87-
# Return that result!
88-
return $result
94+
# Perform query and return each object in the format.ps1xml format
95+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
96+
$Result | ForEach-Object{$_.PSObject.TypeNames.Insert(0,"ServiceNow.Incident")}
97+
$Result
8998
}

ServiceNow/Public/Get-ServiceNowTable.ps1

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

ServiceNow/Public/Get-ServiceNowUser.ps1

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,36 @@ function Get-ServiceNowUser{
6363
$Connection
6464
)
6565

66-
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
67-
68-
if ($Connection -ne $null)
69-
{
70-
$result = Get-ServiceNowTable -Table 'sys_user' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -Connection $Connection
66+
# Query Splat
67+
$newServiceNowQuerySplat = @{
68+
OrderBy = $OrderBy
69+
OrderDirection = $OrderDirection
70+
MatchExact = $MatchExact
71+
MatchContains = $MatchContains
7172
}
72-
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
73-
{
74-
$result = Get-ServiceNowTable -Table 'sys_user' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
73+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
74+
75+
# Table Splat
76+
$getServiceNowTableSplat = @{
77+
Table = 'sys_user'
78+
Query = $Query
79+
Limit = $Limit
80+
DisplayValues = $DisplayValues
81+
}
82+
83+
# Update the splat if the parameters have values
84+
if ($null -ne $PSBoundParameters.Connection)
85+
{
86+
$getServiceNowTableSplat.Add('Connection',$Connection)
7587
}
76-
else
88+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
7789
{
78-
$result = Get-ServiceNowTable -Table 'sys_user' -Query $Query -Limit $Limit -DisplayValues $DisplayValues
90+
$getServiceNowTableSplat.Add('ServiceNowCredential',$ServiceNowCredential)
91+
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
7992
}
8093

81-
# Set the default property set for the table view
82-
$DefaultProperties = @('name', 'email', 'sys_id')
83-
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultProperties)
84-
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
85-
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
86-
return $result
94+
# Perform query and return each object in the format.ps1xml format
95+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
96+
$Result | ForEach-Object{$_.PSObject.TypeNames.Insert(0,"ServiceNow.UserAndUserGroup")}
97+
$Result
8798
}

ServiceNow/Public/Get-ServiceNowUserGroup.ps1

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,36 @@ function Get-ServiceNowUserGroup{
6363
$Connection
6464
)
6565

66+
# Query Splat
67+
$newServiceNowQuerySplat = @{
68+
OrderBy = $OrderBy
69+
OrderDirection = $OrderDirection
70+
MatchExact = $MatchExact
71+
MatchContains = $MatchContains
72+
}
73+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
6674

67-
$Query = New-ServiceNowQuery -OrderBy $OrderBy -OrderDirection $OrderDirection -MatchExact $MatchExact -MatchContains $MatchContains
68-
69-
70-
if ($Connection -ne $null)
71-
{
72-
$result = Get-ServiceNowTable -Table 'sys_user_group' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -Connection $Connection
75+
# Table Splat
76+
$getServiceNowTableSplat = @{
77+
Table = 'sys_user_group'
78+
Query = $Query
79+
Limit = $Limit
80+
DisplayValues = $DisplayValues
7381
}
74-
elseif ($ServiceNowCredential -ne $null -and $ServiceNowURL -ne $null)
75-
{
7682

77-
$result = Get-ServiceNowTable -Table 'sys_user_group' -Query $Query -Limit $Limit -DisplayValues $DisplayValues -ServiceNowCredential $ServiceNowCredential -ServiceNowURL $ServiceNowURL
83+
# Update the splat if the parameters have values
84+
if ($null -ne $PSBoundParameters.Connection)
85+
{
86+
$getServiceNowTableSplat.Add('Connection',$Connection)
7887
}
79-
else
88+
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL)
8089
{
81-
$result = Get-ServiceNowTable -Table 'sys_user_group' -Query $Query -Limit $Limit -DisplayValues $DisplayValues
90+
$getServiceNowTableSplat.Add('ServiceNowCredential',$ServiceNowCredential)
91+
$getServiceNowTableSplat.Add('ServiceNowURL',$ServiceNowURL)
8292
}
8393

84-
# Set the default property set for the table view
85-
$DefaultProperties = @('name', 'email', 'sys_id')
86-
$DefaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$DefaultProperties)
87-
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($DefaultDisplayPropertySet)
88-
$Result | Add-Member MemberSet PSStandardMembers $PSStandardMembers
89-
return $result
94+
# Perform query and return each object in the format.ps1xml format
95+
$Result = Get-ServiceNowTable @getServiceNowTableSplat
96+
$Result | ForEach-Object{$_.PSObject.TypeNames.Insert(0,"ServiceNow.UserAndUserGroup")}
97+
$Result
9098
}

0 commit comments

Comments
 (0)