Skip to content

Commit 7b9a8eb

Browse files
committed
Code read improvements, added error handling, and CBH example fix
1 parent 98c53af commit 7b9a8eb

File tree

1 file changed

+43
-55
lines changed

1 file changed

+43
-55
lines changed

ServiceNow/Public/Get-ServiceNowTableEntry.ps1

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,109 +9,97 @@ function Get-ServiceNowTableEntry {
99
1010
Returns one request item (RITM) from the sc_req_item table
1111
.EXAMPLE
12-
$Record = Get-ServiceNowTableEntry -Table u_customtable -MatchExact {number=$Number}
13-
Update-ServiceNowTableEntry -SysID $Record.sys_id -Table u_customtable -Values {comments='Ticket updated'}
12+
$Record = Get-ServiceNowTableEntry -Table u_customtable -MatchExact @{number=$Number}
13+
Update-ServiceNowTableEntry -SysID $Record.sys_id -Table u_customtable -Values @{comments='Ticket updated'}
1414
1515
Utilize the returned object data with to provide the sys_id property required for updates and removals
1616
.OUTPUTS
1717
System.Management.Automation.PSCustomObject
1818
.NOTES
1919
2020
#>
21+
22+
[CmdletBinding(DefaultParameterSetName)]
2123
param(
2224
# Table containing the entry we're deleting
2325
[parameter(mandatory=$true)]
24-
[parameter(ParameterSetName='SpecifyConnectionFields')]
25-
[parameter(ParameterSetName='UseConnectionObject')]
26-
[parameter(ParameterSetName='SetGlobalAuth')]
2726
[string]$Table,
2827

2928
# Machine name of the field to order by
3029
[parameter(mandatory = $false)]
31-
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
32-
[parameter(ParameterSetName = 'UseConnectionObject')]
33-
[parameter(ParameterSetName = 'SetGlobalAuth')]
3430
[string]$OrderBy = 'opened_at',
3531

3632
# Direction of ordering (Desc/Asc)
3733
[parameter(mandatory = $false)]
38-
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
39-
[parameter(ParameterSetName = 'UseConnectionObject')]
40-
[parameter(ParameterSetName = 'SetGlobalAuth')]
4134
[ValidateSet("Desc", "Asc")]
4235
[string]$OrderDirection = 'Desc',
4336

4437
# Maximum number of records to return
4538
[parameter(mandatory = $false)]
46-
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
47-
[parameter(ParameterSetName = 'UseConnectionObject')]
48-
[parameter(ParameterSetName = 'SetGlobalAuth')]
4939
[int]$Limit = 10,
5040

5141
# Hashtable containing machine field names and values returned must match exactly (will be combined with AND)
5242
[parameter(mandatory = $false)]
53-
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
54-
[parameter(ParameterSetName = 'UseConnectionObject')]
55-
[parameter(ParameterSetName = 'SetGlobalAuth')]
5643
[hashtable]$MatchExact = @{},
5744

5845
# Hashtable containing machine field names and values returned rows must contain (will be combined with AND)
5946
[parameter(mandatory = $false)]
60-
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
61-
[parameter(ParameterSetName = 'UseConnectionObject')]
62-
[parameter(ParameterSetName = 'SetGlobalAuth')]
6347
[hashtable]$MatchContains = @{},
6448

6549
# Whether or not to show human readable display values instead of machine values
6650
[parameter(mandatory = $false)]
67-
[parameter(ParameterSetName = 'SpecifyConnectionFields')]
68-
[parameter(ParameterSetName = 'UseConnectionObject')]
69-
[parameter(ParameterSetName = 'SetGlobalAuth')]
7051
[ValidateSet("true", "false", "all")]
7152
[string]$DisplayValues = 'true',
7253

7354
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
7455
[ValidateNotNullOrEmpty()]
75-
[PSCredential]
76-
$ServiceNowCredential,
56+
[Alias('ServiceNowCredential')]
57+
[PSCredential]$Credential,
7758

7859
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)]
7960
[ValidateNotNullOrEmpty()]
80-
[string]
81-
$ServiceNowURL,
61+
[string]$ServiceNowURL,
8262

8363
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory = $True)]
8464
[ValidateNotNullOrEmpty()]
85-
[Hashtable]
86-
$Connection
65+
[hashtable]$Connection
8766
)
8867

89-
# Query Splat
90-
$newServiceNowQuerySplat = @{
91-
OrderBy = $OrderBy
92-
MatchExact = $MatchExact
93-
OrderDirection = $OrderDirection
94-
MatchContains = $MatchContains
95-
}
96-
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
97-
98-
# Table Splat
99-
$getServiceNowTableSplat = @{
100-
Table = $Table
101-
Query = $Query
102-
Limit = $Limit
103-
DisplayValues = $DisplayValues
68+
Try {
69+
# Query Splat
70+
$newServiceNowQuerySplat = @{
71+
OrderBy = $OrderBy
72+
MatchExact = $MatchExact
73+
OrderDirection = $OrderDirection
74+
MatchContains = $MatchContains
75+
ErrorAction = 'Stop'
76+
}
77+
$Query = New-ServiceNowQuery @newServiceNowQuerySplat
78+
79+
# Table Splat
80+
$getServiceNowTableSplat = @{
81+
Table = $Table
82+
Query = $Query
83+
Limit = $Limit
84+
DisplayValues = $DisplayValues
85+
ErrorAction = 'Stop'
86+
}
87+
88+
# Update the Table Splat if an applicable parameter set name is in use
89+
Switch ($PSCmdlet.ParameterSetName) {
90+
'SpecifyConnectionFields' {
91+
$getServiceNowTableSplat.Add('ServiceNowCredential', $Credential)
92+
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
93+
}
94+
'UseConnectionObject' {
95+
$getServiceNowTableSplat.Add('Connection', $Connection)
96+
}
97+
}
98+
99+
# Perform table query and return each object. No fancy formatting here as this can pull tables with unknown default properties
100+
Get-ServiceNowTable @getServiceNowTableSplat
104101
}
105-
106-
# Update the Table Splat if the parameters have values
107-
if ($null -ne $PSBoundParameters.Connection) {
108-
$getServiceNowTableSplat.Add('Connection', $Connection)
102+
Catch {
103+
Write-Error $PSItem
109104
}
110-
elseif ($null -ne $PSBoundParameters.ServiceNowCredential -and $null -ne $PSBoundParameters.ServiceNowURL) {
111-
$getServiceNowTableSplat.Add('ServiceNowCredential', $ServiceNowCredential)
112-
$getServiceNowTableSplat.Add('ServiceNowURL', $ServiceNowURL)
113-
}
114-
115-
# Perform table query and return each object. No fancy formatting here as this can pull tables with unknown default properties
116-
Get-ServiceNowTable @getServiceNowTableSplat
117105
}

0 commit comments

Comments
 (0)