Skip to content

Commit c441b3d

Browse files
committed
add Id, Name functionality
1 parent 868ca16 commit c441b3d

File tree

2 files changed

+171
-65
lines changed

2 files changed

+171
-65
lines changed

ServiceNow/Public/Get-ServiceNowAttachment.ps1

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,107 +3,153 @@ Function Get-ServiceNowAttachment {
33
<#
44
55
.SYNOPSIS
6-
List details for ServiceNow attachments associated with a ticket number.
6+
Retrieve attachment details
77
88
.DESCRIPTION
9-
List details for ServiceNow attachments associated with a ticket number.
10-
11-
.PARAMETER Number
12-
ServiceNow ticket number
9+
Retrieve attachment details via table record or by advanced filtering.
1310
1411
.PARAMETER Table
15-
ServiceNow ticket table name
12+
Name of the table to be queried, by either table name or class name. Use tab completion for list of known tables.
13+
You can also provide any table name ad hoc.
14+
15+
.PARAMETER Id
16+
Either the record sys_id or number.
17+
If providing just an Id, not with Table, the Id prefix will be looked up to find the table name.
1618
1719
.PARAMETER FileName
18-
Filter for one or more file names. Works like a 'match' where partial file names are valid.
20+
Filter for a specific file name or part of a file name.
21+
22+
.PARAMETER Filter
23+
Array or multidimensional array of fields and values to filter on.
24+
Each array should be of the format @(field, comparison operator, value) separated by a join, either 'and', 'or', or 'group'.
25+
For a complete list of comparison operators, see $script:ServiceNowOperator and use Name in your filter.
26+
See the examples.
27+
Also, see https://docs.servicenow.com/bundle/quebec-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html
28+
for how to represent date values with javascript.
29+
30+
.PARAMETER Sort
31+
Array or multidimensional array of fields to sort on.
32+
Each array should be of the format @(field, asc/desc).
33+
34+
.PARAMETER Connection
35+
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
36+
37+
.PARAMETER ServiceNowSession
38+
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
39+
40+
.EXAMPLE
41+
Get-ServiceNowAttachment -Id 'INC1234567'
42+
43+
Get attachment details for a specific record
1944
2045
.EXAMPLE
21-
Get-ServiceNowAttachmentDetail -Number $Number -Table $Table
46+
Get-ServiceNowAttachment -Id 'INC1234567' -FileName image.jpg
2247
23-
List attachment details
48+
Get attachment details for a specific record where file names match all or part of image.jpg
2449
2550
.EXAMPLE
26-
Get-ServiceNowAttachmentDetail -Number $Number -Table $Table -FileName filename.txt,report.csv
51+
Get-ServiceNowAttachment -Filter @('size_bytes', '-gt', '1000000')
2752
28-
List details for only filename.txt and report.csv (if they exist).
53+
Get attachment details where size is greater than 1M.
2954
55+
.INPUTS
56+
Table, Id
57+
3058
.OUTPUTS
3159
System.Management.Automation.PSCustomObject
3260
#>
3361

3462
[OutputType([System.Management.Automation.PSCustomObject[]])]
35-
[CmdletBinding(DefaultParameterSetName = 'BySysId')]
63+
[CmdletBinding(DefaultParameterSetName = 'Filter', SupportsPaging)]
3664

3765
Param(
38-
# Table containing the entry
39-
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
66+
[Parameter(ParameterSetName = 'Table', Mandatory, ValueFromPipelineByPropertyName)]
4067
[Alias('sys_class_name')]
4168
[string] $Table,
4269

43-
# Object number
44-
[Parameter(ParameterSetName = 'ByNumber', Mandatory)]
45-
[string] $Number,
70+
[Parameter(ParameterSetName = 'Id', Mandatory, ValueFromPipelineByPropertyName)]
71+
[Parameter(ParameterSetName = 'Table', Mandatory, ValueFromPipelineByPropertyName)]
72+
[Alias('sys_id', 'SysId', 'number')]
73+
[string] $Id,
4674

47-
[Parameter(ParameterSetName = 'BySysId', Mandatory, ValueFromPipelineByPropertyName)]
48-
[Alias('sys_id')]
49-
[string] $SysId,
50-
51-
# Filter results by file name
5275
[parameter()]
53-
[string[]] $FileName,
76+
[string] $FileName,
77+
78+
[Parameter()]
79+
[System.Collections.ArrayList] $Filter,
5480

55-
# Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
56-
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)]
81+
[parameter()]
5782
[ValidateNotNullOrEmpty()]
83+
[System.Collections.ArrayList] $Sort,
84+
85+
[Parameter()]
5886
[Hashtable] $Connection,
5987

60-
[Parameter(ParameterSetName = 'Session')]
61-
[ValidateNotNullOrEmpty()]
88+
[Parameter()]
6289
[hashtable] $ServiceNowSession = $script:ServiceNowSession
6390
)
6491

6592
begin {}
6693

6794
process {
68-
$params = Get-ServiceNowAuth -C $Connection -S ServiceNowSession
69-
70-
# URI format: https://tenant.service-now.com/api/now/attachment/{sys_id}/file
71-
$params.Uri += '/attachment/' + $SysID + '/file'
95+
$params = @{
96+
UriLeaf = '/attachment'
97+
First = $PSCmdlet.PagingParameters.First
98+
Skip = $PSCmdlet.PagingParameters.Skip
99+
IncludeTotalCount = $PSCmdlet.PagingParameters.IncludeTotalCount
100+
Connection = $Connection
101+
ServiceNowSession = $ServiceNowSession
102+
}
72103

73-
if ( $PSCmdlet.ParameterSetName -eq 'ByNumber' ) {
74-
$getSysIdParams = @{
75-
Table = $Table
76-
Filter = @('number', '-eq', $number)
77-
Properties = 'sys_id'
104+
if ( $PSCmdlet.ParameterSetName -in 'Table', 'Id' ) {
105+
$getParams = @{
106+
Id = $Id
107+
Property = 'sys_class_name', 'sys_id'
78108
Connection = $Connection
79109
ServiceNowSession = $ServiceNowSession
80110
}
111+
if ( $Table ) {
112+
$getParams.Table = $Table
113+
}
114+
$tableRecord = Get-ServiceNowRecord @getParams
81115

82-
# Use the number and table to determine the sys_id
83-
$sysId = Invoke-ServiceNowRestMethod @getSysIdParams | Select-Object -ExpandProperty sys_id
84-
}
85-
86-
$params = @{
87-
Uri = '/attachment'
88-
Filter = @(
89-
@('table_name', '-eq', $Table),
116+
if ( -not $tableRecord ) {
117+
Write-Error "Record not found for Id '$Id'"
118+
continue
119+
}
120+
121+
$params.Filter = @(
122+
@('table_name', '-eq', $tableRecord.sys_class_name),
90123
'and',
91-
@('table_sys_id', '-eq', $sysId)
124+
@('table_sys_id', '-eq', $tableRecord.sys_id)
92125
)
93-
Connection = $Connection
94-
ServiceNowSession = $ServiceNowSession
95126
}
96-
$response = Invoke-ServiceNowRestMethod @params
97127

98128
if ( $FileName ) {
99-
# TODO: move into query
100-
$response | Where-Object { $_.file_name -in $FileName }
129+
if ( $params.Filter ) {
130+
$params.Filter += 'and', @('file_name', '-like', $FileName)
131+
}
132+
else {
133+
$params.Filter = @('file_name', '-like', $FileName)
134+
}
101135
}
102-
else {
136+
137+
if ( $Filter ) {
138+
if ( $params.Filter ) {
139+
$params.Filter += 'and', $Filter
140+
}
141+
else {
142+
$params.Filter = $Filter
143+
}
144+
}
145+
146+
$response = Invoke-ServiceNowRestMethod @params
147+
148+
if ( $response ) {
149+
$response | ForEach-Object { $_.PSObject.TypeNames.Insert(0, 'ServiceNow.Attachment') }
103150
$response
104151
}
105152

106-
# $response | Update-ServiceNowDateTimeField
107153
}
108154

109155
end {}

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,30 @@
8686
function Get-ServiceNowRecord {
8787

8888
[OutputType([System.Management.Automation.PSCustomObject])]
89-
[CmdletBinding(DefaultParameterSetName = 'SessionFilter', SupportsPaging)]
89+
[CmdletBinding(DefaultParameterSetName = 'Id', SupportsPaging)]
90+
[Alias('gsnr')]
9091

9192
Param (
92-
[parameter(Mandatory)]
93+
[Parameter(ParameterSetName = 'Table', Mandatory)]
9394
[Alias('sys_class_name')]
9495
[string] $Table,
9596

97+
[Parameter(ParameterSetName = 'Id', Mandatory, Position = 0)]
98+
[Parameter(ParameterSetName = 'Table')]
99+
[Alias('sys_id', 'number')]
100+
[string] $Id,
101+
102+
[Parameter()]
103+
[string] $Name,
104+
96105
[Parameter()]
97106
[Alias('Fields', 'Properties')]
98107
[string[]] $Property,
99108

100-
[parameter(ParameterSetName = 'AutomationFilter')]
101-
[parameter(ParameterSetName = 'SessionFilter')]
109+
[Parameter()]
102110
[System.Collections.ArrayList] $Filter,
103111

104-
[parameter(ParameterSetName = 'AutomationFilter')]
105-
[parameter(ParameterSetName = 'SessionFilter')]
112+
[parameter()]
106113
[ValidateNotNullOrEmpty()]
107114
[System.Collections.ArrayList] $Sort,
108115

@@ -114,22 +121,70 @@ function Get-ServiceNowRecord {
114121
[Parameter()]
115122
[switch] $IncludeCustomVariable,
116123

117-
[Parameter(Mandatory, ParameterSetName = 'AutomationQuery')]
118-
[parameter(Mandatory, ParameterSetName = 'AutomationFilter')]
119-
[ValidateNotNullOrEmpty()]
124+
# [Parameter(Mandatory, ParameterSetName = 'AutomationTable')]
125+
# [Parameter(Mandatory, ParameterSetName = 'AutomationId')]
126+
# [ValidateNotNullOrEmpty()]
127+
# [AllowNull()]
128+
[Parameter()]
120129
[hashtable] $Connection,
121130

122-
[Parameter(ParameterSetName = 'SessionQuery')]
123-
[Parameter(ParameterSetName = 'SessionFilter')]
124-
[ValidateNotNullOrEmpty()]
131+
# [Parameter(ParameterSetName = 'SessionTable')]
132+
# [Parameter(ParameterSetName = 'SessionId')]
133+
# [ValidateNotNullOrEmpty()]
134+
[Parameter()]
125135
[hashtable] $ServiceNowSession = $script:ServiceNowSession
126136
)
127137

138+
# it's easier this way to pass everything to invoke-servicenowrestmethod given paging params, etc
128139
$invokeParams = $PSBoundParameters
129140
$invokeParams.Remove('IncludeCustomVariable') | Out-Null
141+
$invokeParams.Remove('Id') | Out-Null
142+
$invokeParams.Remove('Name') | Out-Null
130143

131-
$addedSysIdProp = $false
144+
if ( $Id ) {
145+
if ( $Id -match '[a-zA-Z0-9]{32}' ) {
146+
if ( $PSCmdlet.ParameterSetName -like '*Id' ) {
147+
throw 'Providing sys_id for -Id requires a value for -Table. Alternatively, provide an Id with a prefix, eg. INC1234567.'
148+
}
132149

150+
$idFilter = @('sys_id', '-eq', $Id)
151+
}
152+
else {
153+
if ( $PSCmdlet.ParameterSetName -like '*Id' ) {
154+
# get table name from prefix if only Id was provided
155+
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $Id.ToLower().StartsWith($_.NumberPrefix) } | Select-Object -ExpandProperty Name
156+
if ( $thisTable ) {
157+
$invokeParams.Table = $thisTable
158+
}
159+
else {
160+
throw ('Prefix not found for Id ''{0}''. Known prefixes are {1}.' -f $Id, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
161+
}
162+
}
163+
$idFilter = @('number', '-eq', $Id)
164+
}
165+
166+
if ( $invokeParmas.Filter ) {
167+
$invokeParams.Filter = $invokeParams.Filter, 'and', $idFilter
168+
}
169+
else {
170+
$invokeParams.Filter = $idFilter
171+
}
172+
}
173+
174+
if ( $Name ) {
175+
# determine the field we should compare for 'name' and add the filter
176+
$thisNameField = $script:ServiceNowTable | Where-Object { $_.Name.ToLower() -eq $Table.ToLower() -or $_.ClassName.ToLower() -eq $Table.ToLower() } | Select-Object -ExpandProperty TableNameField
177+
if ( $thisNameField ) {
178+
if ( $invokeParmas.Filter ) {
179+
$invokeParams.Filter = $invokeParams.Filter, 'and', @($thisNameField, '-like', $Name)
180+
}
181+
else {
182+
$invokeParams.Filter = @($thisNameField, '-like', $Name)
183+
}
184+
}
185+
}
186+
187+
$addedSysIdProp = $false
133188
# we need the sys_id value in order to get custom var data
134189
# add it in if specific properties were requested and not part of the list
135190
if ( $IncludeCustomVariable.IsPresent ) {
@@ -139,6 +194,11 @@ function Get-ServiceNowRecord {
139194
}
140195
}
141196

197+
if ( $Table -eq 'attachment' ) {
198+
$invokeParams.Remove('Table') | Out-Null
199+
$invokeParams.UriLeaf = '/attachment'
200+
}
201+
142202
$result = Invoke-ServiceNowRestMethod @invokeParams
143203

144204
if ( $result ) {

0 commit comments

Comments
 (0)