@@ -22,42 +22,34 @@ Function Export-ServiceNowAttachment {
2222 Adds the SysID to the file name. Intended for use when a ticket has multiple files with the same name.
2323
2424 . EXAMPLE
25- Get -ServiceNowAttachment -SysID $SysID -FileName 'mynewfile.txt'
25+ Export -ServiceNowAttachment -SysID $SysID -FileName 'mynewfile.txt'
2626
2727 Save the attachment with the specified sys_id with a name of 'mynewfile.txt'
2828
2929 . EXAMPLE
30- Get-ServiceNowAttachment -Number $Number -Table $Table | Get -ServiceNowAttachment
30+ Get-ServiceNowAttachment -Id INC1234567 | Export -ServiceNowAttachment
3131
3232 Save all attachments from the ticket. Filenames will be assigned from the attachment name.
3333
3434 . EXAMPLE
35- Get-ServiceNowAttachmentDetail -Number $Number -Table $Table | Get -ServiceNowAttachment -AppendNameWithSysID
35+ Get-ServiceNowAttachment -Id INC1234567 | Export -ServiceNowAttachment -AppendNameWithSysID
3636
3737 Save all attachments from the ticket. Filenames will be assigned from the attachment name and appended with the sys_id.
3838
3939 . EXAMPLE
40- Get-ServiceNowAttachmentDetail -Number $Number -Table $Table | Get -ServiceNowAttachment -Destination $Destination -AllowOverwrite
40+ Get-ServiceNowAttachment -Id INC1234567 | Export -ServiceNowAttachment -Destination $path -AllowOverwrite
4141
4242 Save all attachments from the ticket to the destination allowing for overwriting the destination file.
4343 #>
4444
45- [CmdletBinding (DefaultParameterSetName = ' Table ' , SupportsShouldProcess = $true )]
45+ [CmdletBinding (DefaultParameterSetName = ' Session ' , SupportsShouldProcess = $true )]
4646 Param (
4747
48- [Parameter (ParameterSetName = ' Table' , Mandatory , ValueFromPipelineByPropertyName )]
49- [Alias (' sys_class_name' )]
50- [string ] $Table ,
51-
52- [Parameter (ParameterSetName = ' Table' , Mandatory , ValueFromPipelineByPropertyName )]
53- [Alias (' sys_id' )]
54- [string ] $TableId ,
55-
56- [Parameter (ParameterSetName = ' Attachment' , Mandatory , ValueFromPipelineByPropertyName )]
48+ [Parameter (Mandatory , ValueFromPipeline , ValueFromPipelineByPropertyName )]
5749 [Alias (' sys_id' )]
5850 [string ] $SysId ,
5951
60- [Parameter (ParameterSetName = ' Attachment ' , Mandatory , ValueFromPipelineByPropertyName )]
52+ [Parameter (ValueFromPipelineByPropertyName )]
6153 [Alias (' file_name' )]
6254 [string ] $FileName ,
6355
@@ -77,7 +69,7 @@ Function Export-ServiceNowAttachment {
7769 [switch ] $AppendNameWithSysId ,
7870
7971 # Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
80- [Parameter (ParameterSetName = ' UseConnectionObject ' , Mandatory )]
72+ [Parameter (ParameterSetName = ' Automation ' , Mandatory )]
8173 [ValidateNotNullOrEmpty ()]
8274 [Hashtable ] $Connection ,
8375
@@ -87,48 +79,28 @@ Function Export-ServiceNowAttachment {
8779 )
8880
8981 begin {
90- $authParams = Get-ServiceNowAuth - C $Connection - S ServiceNowSession
82+ $authParams = Get-ServiceNowAuth - C $Connection - S $ ServiceNowSession
9183 }
9284
9385 process {
9486
95- # URI format: https://tenant.service-now.com/api/now/attachment/{sys_id}/file
9687 $params = $authParams.Clone ()
9788
98- # if table record provided, get the attachment details
99- if ( $PSCmdlet.ParameterSetName -eq ' Table' ) {
100-
101- $attachmentListParams = @ {
102- Table = $Table
103- Connection = $Connection
104- ServiceNowSession = $ServiceNowSession
105- }
106-
107- # determine if tableid is the number or sysid
108- try {
109- [guid ] $TableId
110- $attachmentListParams.SysId = $TableId
111- }
112- catch {
113- $attachmentListParams.Number = $TableId
114- }
115- }
116-
117- $params.Uri += ' /attachment/' + $SysID + ' /file'
89+ $params.Uri += ' /attachment/' + $SysId + ' /file'
11890
119- If ($AppendNameWithSysId.IsPresent ) {
120- $FileName = " {0}_{1}{2}" -f [io.path ]::GetFileNameWithoutExtension($FileName ), $SysID , [io.path ]::GetExtension($FileName )
91+ $thisFileName = $FileName
92+ If ( $AppendNameWithSysId.IsPresent ) {
93+ $thisFileName = " {0}_{1}{2}" -f [io.path ]::GetFileNameWithoutExtension($thisFileName ), $SysId , [io.path ]::GetExtension($thisFileName )
12194 }
122- $OutFile = $Null
123- $OutFile = Join-Path $Destination $FileName
95+ $outFile = Join-Path $Destination $thisFileName
12496
125- If ((Test-Path $OutFile ) -and -not $AllowOverwrite.IsPresent ) {
97+ If ((Test-Path $outFile ) -and -not $AllowOverwrite.IsPresent ) {
12698 throw (' The file '' {0}'' already exists. Please choose a different name, use the -AppendNameWithSysID switch parameter, or use the -AllowOverwrite switch parameter to overwrite the file.' -f $OutFile )
12799 }
128100
129- $params.OutFile = $OutFile
101+ $params.OutFile = $outFile
130102
131- If ($PSCmdlet.ShouldProcess (" SysId $SysId " , " Save attachment to file $OutFile " )) {
103+ If ($PSCmdlet.ShouldProcess ($outFile , " Save attachment" )) {
132104 Invoke-WebRequest @params
133105 }
134106 }
0 commit comments