|
| 1 | +function Get-ServiceNowTableEntry { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Wraps Get-ServiceNowQuery & Get-ServiceNowTable for easier custom table queries |
| 5 | + .DESCRIPTION |
| 6 | + Wraps Get-ServiceNowQuery & Get-ServiceNowTable for easier custom table queries. No formatting is provided on output. Every property is returned by default. |
| 7 | + .EXAMPLE |
| 8 | + Get-ServiceNowTableEntry -Table sc_req_item -Limit 1 |
| 9 | +
|
| 10 | + Returns one request item (RITM) from the sc_req_item table |
| 11 | + .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'} |
| 14 | +
|
| 15 | + Utilize the returned object data with to provide the sys_id property required for updates and removals |
| 16 | + .OUTPUTS |
| 17 | + System.Management.Automation.PSCustomObject |
| 18 | + .NOTES |
| 19 | +
|
| 20 | + #> |
| 21 | + |
| 22 | + [CmdletBinding(DefaultParameterSetName)] |
| 23 | + param( |
| 24 | + # Table containing the entry we're deleting |
| 25 | + [parameter(mandatory=$true)] |
| 26 | + [string]$Table, |
| 27 | + |
| 28 | + # Machine name of the field to order by |
| 29 | + [parameter(mandatory = $false)] |
| 30 | + [string]$OrderBy = 'opened_at', |
| 31 | + |
| 32 | + # Direction of ordering (Desc/Asc) |
| 33 | + [parameter(mandatory = $false)] |
| 34 | + [ValidateSet("Desc", "Asc")] |
| 35 | + [string]$OrderDirection = 'Desc', |
| 36 | + |
| 37 | + # Maximum number of records to return |
| 38 | + [parameter(mandatory = $false)] |
| 39 | + [int]$Limit = 10, |
| 40 | + |
| 41 | + # Hashtable containing machine field names and values returned must match exactly (will be combined with AND) |
| 42 | + [parameter(mandatory = $false)] |
| 43 | + [hashtable]$MatchExact = @{}, |
| 44 | + |
| 45 | + # Hashtable containing machine field names and values returned rows must contain (will be combined with AND) |
| 46 | + [parameter(mandatory = $false)] |
| 47 | + [hashtable]$MatchContains = @{}, |
| 48 | + |
| 49 | + # Whether or not to show human readable display values instead of machine values |
| 50 | + [parameter(mandatory = $false)] |
| 51 | + [ValidateSet("true", "false", "all")] |
| 52 | + [string]$DisplayValues = 'true', |
| 53 | + |
| 54 | + [Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)] |
| 55 | + [ValidateNotNullOrEmpty()] |
| 56 | + [Alias('ServiceNowCredential')] |
| 57 | + [PSCredential]$Credential, |
| 58 | + |
| 59 | + [Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory = $True)] |
| 60 | + [ValidateNotNullOrEmpty()] |
| 61 | + [string]$ServiceNowURL, |
| 62 | + |
| 63 | + [Parameter(ParameterSetName = 'UseConnectionObject', Mandatory = $True)] |
| 64 | + [ValidateNotNullOrEmpty()] |
| 65 | + [hashtable]$Connection |
| 66 | + ) |
| 67 | + |
| 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 | + Default {} |
| 98 | + } |
| 99 | + |
| 100 | + # Perform table query and return each object. No fancy formatting here as this can pull tables with unknown default properties |
| 101 | + Get-ServiceNowTable @getServiceNowTableSplat |
| 102 | + } |
| 103 | + Catch { |
| 104 | + Write-Error $PSItem |
| 105 | + } |
| 106 | +} |
0 commit comments