Skip to content

Commit 05026fa

Browse files
authored
Merge pull request #16 from Rick-2CA/master
v1.37 - Return missing functions & Some Tweaks
2 parents de60db8 + de3639c commit 05026fa

12 files changed

+560
-210
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: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

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)