@@ -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 {}
0 commit comments