Skip to content

Commit cfd37b4

Browse files
authored
Add AsValue parameter to Export-ServiceNowAttachment (#178)
1 parent b592760 commit cfd37b4

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

RELEASE.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
- Add table name translation to `Get-ServiceNowAttachment` where sys_class_name does not match table name
2-
- Change .endswith operator from % to ENDSWITH as % was not working
1+
- Add `AsValue` parameter to `Export-ServiceNowAttachment` to return attachment contents instead of writing to a file

ServiceNow/Public/Export-ServiceNowAttachment.ps1

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Allows the function to overwrite the existing file.
2020
.PARAMETER AppendNameWithSysID
2121
Adds the SysID to the file name. Intended for use when a ticket has multiple files with the same name.
2222
23+
.PARAMETER AsValue
24+
Instead of writing to a file, return the attachment contents
25+
2326
.EXAMPLE
2427
Export-ServiceNowAttachment -SysID $SysID -FileName 'mynewfile.txt'
2528
@@ -39,36 +42,44 @@ Save all attachments from the ticket. Filenames will be assigned from the attac
3942
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment -Destination $path -AllowOverwrite
4043
4144
Save all attachments from the ticket to the destination allowing for overwriting the destination file.
45+
46+
.EXAMPLE
47+
Export-ServiceNowAttachment -SysId $SysId -AsValue
48+
Return the contents of the attachment instead of writing to a file
49+
4250
#>
4351
Function Export-ServiceNowAttachment {
4452

45-
[CmdletBinding(SupportsShouldProcess)]
53+
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ToFile')]
4654

4755
Param(
48-
56+
4957
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
5058
[Alias('sys_id')]
5159
[string] $SysId,
5260

53-
[Parameter(ValueFromPipelineByPropertyName)]
61+
[Parameter(ParameterSetName = 'ToFile', ValueFromPipelineByPropertyName)]
5462
[Alias('file_name')]
5563
[string] $FileName,
5664

5765
# Out path to download files
58-
[parameter()]
66+
[parameter(ParameterSetName = 'ToFile')]
5967
[ValidateScript( {
6068
Test-Path $_
6169
})]
6270
[string] $Destination = $PWD.Path,
6371

6472
# Options impacting downloads
65-
[parameter()]
73+
[parameter(ParameterSetName = 'ToFile')]
6674
[switch] $AllowOverwrite,
6775

6876
# Options impacting downloads
69-
[parameter()]
77+
[parameter(ParameterSetName = 'ToFile')]
7078
[switch] $AppendNameWithSysId,
7179

80+
[Parameter(ParameterSetName = 'ToPipeline', Mandatory)]
81+
[switch] $AsValue,
82+
7283
[Parameter()]
7384
[Hashtable] $Connection,
7485

@@ -86,20 +97,22 @@ Function Export-ServiceNowAttachment {
8697

8798
$params.Uri += '/attachment/' + $SysId + '/file'
8899

89-
$thisFileName = $FileName
90-
If ( $AppendNameWithSysId.IsPresent ) {
91-
$thisFileName = "{0}_{1}{2}" -f [io.path]::GetFileNameWithoutExtension($thisFileName), $SysId, [io.path]::GetExtension($thisFileName)
92-
}
93-
$outFile = Join-Path $Destination $thisFileName
100+
if ( $PSCmdlet.ParameterSetName -eq 'ToFile' ) {
101+
$thisFileName = $FileName
102+
If ( $AppendNameWithSysId.IsPresent ) {
103+
$thisFileName = "{0}_{1}{2}" -f [io.path]::GetFileNameWithoutExtension($thisFileName), $SysId, [io.path]::GetExtension($thisFileName)
104+
}
105+
$outFile = Join-Path $Destination $thisFileName
94106

95-
If ((Test-Path $outFile) -and -not $AllowOverwrite.IsPresent) {
96-
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)
97-
}
107+
If ((Test-Path $outFile) -and -not $AllowOverwrite.IsPresent) {
108+
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)
109+
}
98110

99-
$params.OutFile = $outFile
111+
$params.OutFile = $outFile
112+
}
100113

101114
If ($PSCmdlet.ShouldProcess($outFile, "Save attachment")) {
102-
Invoke-WebRequest @params
115+
Invoke-RestMethod @params
103116
}
104117
}
105118
end {}

0 commit comments

Comments
 (0)