Skip to content

Commit 30f259d

Browse files
authored
Fix 188 (#189)
1 parent 8142412 commit 30f259d

File tree

6 files changed

+184
-104
lines changed

6 files changed

+184
-104
lines changed
Lines changed: 95 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,88 @@
11
Function Add-ServiceNowAttachment {
22
<#
33
.SYNOPSIS
4-
Attaches a file to an existing ticket.
4+
Attaches a file to an existing record.
55
66
.DESCRIPTION
7-
Attaches a file to an existing ticket.
8-
9-
.PARAMETER Number
10-
ServiceNow ticket number
7+
Attaches a file to an existing record.
118
129
.PARAMETER Table
13-
ServiceNow ticket table name
10+
Name of the table to be queried, by either table name or class name. Use tab completion for list of known tables.
11+
You can also provide any table name ad hoc.
12+
If using pipeline and this is failing, most likely the table name and class do not match.
13+
In this case, provide this value directly.
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.
1418
1519
.PARAMETER File
16-
A valid path to the file to attach
20+
Path to one or more files to attach
21+
22+
.PARAMETER ContentType
23+
Content (MIME) type for the file being uploaded.
24+
This value will be automatically determined by default, but can be overridden with this parameter.
25+
26+
.PARAMETER PassThru
27+
Return the newly created attachment details
28+
29+
.PARAMETER Connection
30+
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
31+
32+
.PARAMETER ServiceNowSession
33+
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
1734
1835
.EXAMPLE
19-
Add-ServiceNowAttachment -Number $Number -Table $Table -File .\File01.txt, .\File02.txt
20-
21-
Upload one or more files to a ServiceNow ticket by specifing the number and table
36+
Add-ServiceNowAttachment -Id INC0000010 -File @('.\File01.txt', '.\File02.txt')
37+
38+
Upload one or more files by record number
2239
2340
.EXAMPLE
24-
New-ServiceNowIncident @params -PassThru | Add-ServiceNowAttachment -File File01.txt
25-
41+
Add-ServiceNowAttachment -Table incident -Id 2306c37c1bafc9100774ebd1b24bcb6d -File @('.\File01.txt', '.\File02.txt')
42+
43+
Upload one or more files by record sys_id
44+
45+
.EXAMPLE
46+
New-ServiceNowIncident @params -PassThru | Add-ServiceNowAttachment -File file01.txt
47+
2648
Create a new incident and add an attachment
2749
2850
.EXAMPLE
29-
Add-ServiceNowAttachment -Number $Number -Table $Table -File .\File01.txt -ContentType 'text/plain'
30-
31-
Upload a file and specify the MIME type (content type). Should only be required if the function cannot automatically determine the type.
51+
Add-ServiceNowAttachment -Id INC0000010 -File file01.txt -ContentType 'text/plain'
52+
53+
Upload a file and specify the MIME type (content type).
54+
Only required if the function cannot automatically determine the type.
3255
3356
.EXAMPLE
34-
Add-ServiceNowAttachment -Number $Number -Table $Table -File .\File01.txt -PassThru
57+
Add-ServiceNowAttachment -Id INC0000010 -File file01.txt -PassThru
58+
59+
Upload a file and receive back the file details
3560
36-
Upload a file and receive back the file details.
61+
.INPUTS
62+
Table, ID
3763
3864
.OUTPUTS
3965
System.Management.Automation.PSCustomObject if -PassThru provided
4066
#>
4167

4268
[OutputType([PSCustomObject[]])]
4369
[CmdletBinding(SupportsShouldProcess)]
70+
4471
Param(
45-
# Table containing the entry
4672
[Parameter(ValueFromPipelineByPropertyName)]
4773
[Alias('sys_class_name')]
4874
[string] $Table,
4975

50-
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
76+
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
77+
[ValidateScript( {
78+
if ($_ -match '^[a-zA-Z0-9]{32}$' -or $_ -match '^([a-zA-Z]+)[0-9]+$') {
79+
$true
80+
} else {
81+
throw 'Id must be either a 32 character alphanumeric, ServiceNow sysid, or prefix/id, ServiceNow number.'
82+
}
83+
})]
5184
[Alias('sys_id', 'SysId', 'number')]
52-
[string] $Id,
85+
[string] $ID,
5386

5487
[Parameter(Mandatory)]
5588
[ValidateScript( {
@@ -65,41 +98,59 @@ Function Add-ServiceNowAttachment {
6598
[Parameter()]
6699
[switch] $PassThru,
67100

68-
# Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
69101
[Parameter()]
70-
# [ValidateNotNullOrEmpty()]
71102
[Hashtable] $Connection,
72103

73104
[Parameter()]
74-
# [ValidateNotNullOrEmpty()]
75105
[hashtable] $ServiceNowSession = $script:ServiceNowSession
76106
)
77107

78108
begin {}
79109

80110
process {
81111

82-
$getParams = @{
83-
Id = $Id
84-
Property = 'sys_class_name', 'sys_id', 'number'
85-
Connection = $Connection
86-
ServiceNowSession = $ServiceNowSession
87-
}
88112
if ( $Table ) {
89-
$getParams.Table = $Table
113+
$thisTableName = $ServiceNowTable.Where{ $_.ClassName -eq $Table } | Select-Object -ExpandProperty Name
114+
if ( -not $thisTableName ) {
115+
$thisTableName = $Table
116+
}
90117
}
91-
$tableRecord = Get-ServiceNowRecord @getParams
92118

93-
if ( -not $tableRecord ) {
94-
Write-Error "Record not found for Id '$Id'"
95-
continue
96-
}
119+
if ( $ID -match '^[a-zA-Z0-9]{32}$' ) {
120+
if ( -not $thisTableName ) {
121+
Write-Error 'Providing sys_id for -Id requires a value for -Table. Alternatively, provide an -Id with a prefix, eg. INC1234567, and the table will be automatically determined.'
122+
Continue
123+
}
97124

98-
If (-not $Table) {
99-
$tableName = $tableRecord.sys_class_name
100-
}
101-
else {
102-
$tableName = $Table
125+
$thisSysId = $ID
126+
127+
} else {
128+
if ( -not $thisTableName ) {
129+
$thisTable = $ServiceNowTable.Where{ $_.NumberPrefix -and $ID.ToLower().StartsWith($_.NumberPrefix) }
130+
if ( $thisTable ) {
131+
$thisTableName = $thisTable.Name
132+
} else {
133+
Write-Error ('The prefix for Id ''{0}'' was not found and the appropriate table cannot be determined. Known prefixes are {1}. Please provide a value for -Table.' -f $ID, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
134+
Continue
135+
}
136+
}
137+
138+
$getParams = @{
139+
Table = $thisTableName
140+
Id = $ID
141+
Property = 'sys_id'
142+
Connection = $Connection
143+
ServiceNowSession = $ServiceNowSession
144+
}
145+
146+
$tableRecord = Get-ServiceNowRecord @getParams
147+
148+
if ( -not $tableRecord ) {
149+
Write-Error "Record not found for Id '$ID'"
150+
continue
151+
}
152+
153+
$thisSysId = $tableRecord.sys_id
103154
}
104155

105156
$auth = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
@@ -118,31 +169,28 @@ Function Add-ServiceNowAttachment {
118169
# POST: https://instance.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot
119170
# $Uri = "{0}/file?table_name={1}&table_sys_id={2}&file_name={3}" -f $ApiUrl, $Table, $TableSysID, $FileData.Name
120171
$invokeRestMethodSplat = $auth
121-
$invokeRestMethodSplat.Uri += '/attachment/file?table_name={0}&table_sys_id={1}&file_name={2}' -f $tableName, $tableRecord.sys_id, $FileData.Name
172+
$invokeRestMethodSplat.Uri += '/attachment/file?table_name={0}&table_sys_id={1}&file_name={2}' -f $thisTableName, $thisSysId, $FileData.Name
122173
$invokeRestMethodSplat.Headers += @{'Content-Type' = $ContentType }
123174
$invokeRestMethodSplat.UseBasicParsing = $true
124175
$invokeRestMethodSplat += @{
125176
Method = 'POST'
126177
InFile = $FileData.FullName
127178
}
128179

129-
If ($PSCmdlet.ShouldProcess(('{0} {1}' -f $tableName, $tableRecord.number), ('Add attachment {0}' -f $FileData.FullName))) {
180+
If ($PSCmdlet.ShouldProcess(('{0} {1}' -f $thisTableName, $thisSysId), ('Add attachment {0}' -f $FileData.FullName))) {
130181
Write-Verbose ($invokeRestMethodSplat | ConvertTo-Json)
131182
$response = Invoke-WebRequest @invokeRestMethodSplat
132183

133184
if ( $response.Content ) {
134-
if ( $PassThru.IsPresent ) {
185+
if ( $PassThru ) {
135186
$content = $response.content | ConvertFrom-Json
136187
$content.result
137188
}
138-
}
139-
else {
189+
} else {
140190
# invoke-webrequest didn't throw an error, but we didn't get content back either
141191
throw ('"{0} : {1}' -f $response.StatusCode, $response | Out-String )
142192
}
143193
}
144194
}
145195
}
146-
147-
end {}
148196
}

ServiceNow/Public/Export-ServiceNowRecord.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function Export-ServiceNowRecord {
8787
}
8888
})]
8989
[Alias('sys_id', 'number')]
90-
[string] $Id,
90+
[string] $ID,
9191

9292
[Parameter()]
9393
[Alias('Fields', 'Properties')]
@@ -123,24 +123,24 @@ function Export-ServiceNowRecord {
123123
$thisTable = $Table
124124
}
125125

126-
if ( $Id ) {
127-
if ( $Id -match '^[a-zA-Z0-9]{32}$' ) {
126+
if ( $ID ) {
127+
if ( $ID -match '^[a-zA-Z0-9]{32}$' ) {
128128
if ( -not $thisTable ) {
129129
throw 'Providing sys_id for -Id requires a value for -Table. Alternatively, provide an Id with a prefix, eg. INC1234567, and the table will be automatically determined.'
130130
}
131131

132-
$newFilter = @('sys_id', '-eq', $Id)
132+
$newFilter = @('sys_id', '-eq', $ID)
133133
} else {
134134
if ( -not $thisTable ) {
135135
# get table name from prefix if only Id was provided
136-
$idPrefix = ($Id | Select-String -Pattern '^([a-zA-Z]+)([0-9]+$)').Matches.Groups[1].Value.ToLower()
136+
$idPrefix = ($ID | Select-String -Pattern '^([a-zA-Z]+)([0-9]+$)').Matches.Groups[1].Value.ToLower()
137137
Write-Debug "Id prefix is $idPrefix"
138138
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $idPrefix -eq $_.NumberPrefix } | Select-Object -ExpandProperty Name
139139
if ( -not $thisTable ) {
140-
throw ('The prefix for Id ''{0}'' was not found and the appropriate table cannot be determined. Known prefixes are {1}. Please provide a value for -Table.' -f $Id, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
140+
throw ('The prefix for Id ''{0}'' was not found and the appropriate table cannot be determined. Known prefixes are {1}. Please provide a value for -Table.' -f $ID, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
141141
}
142142
}
143-
$newFilter = @('number', '-eq', $Id)
143+
$newFilter = @('number', '-eq', $ID)
144144
}
145145
}
146146

0 commit comments

Comments
 (0)