Skip to content

Commit 0edf4d3

Browse files
committed
id, name functionality
1 parent 1182ee7 commit 0edf4d3

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

ServiceNow/Public/Add-ServiceNowAttachment.ps1

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@ Function Add-ServiceNowAttachment {
4040
#>
4141

4242
[OutputType([PSCustomObject[]])]
43-
[CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess)]
43+
[CmdletBinding(SupportsShouldProcess)]
4444
Param(
4545
# Table containing the entry
46-
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
46+
[Parameter(ValueFromPipelineByPropertyName)]
4747
[Alias('sys_class_name')]
4848
[string] $Table,
4949

50-
[Parameter(ParameterSetName = 'AutomationSysId', Mandatory, ValueFromPipelineByPropertyName)]
51-
[Parameter(ParameterSetName = 'SessionSysId', Mandatory, ValueFromPipelineByPropertyName)]
52-
[Alias('sys_id')]
53-
[string] $SysId,
50+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
51+
[Alias('sys_id', 'SysId', 'number')]
52+
[string] $Id,
5453

55-
[Parameter(ParameterSetName = 'AutomationNumber', Mandatory)]
56-
[Parameter(ParameterSetName = 'SessionNumber', Mandatory)]
57-
[string] $Number,
54+
# [Parameter(ParameterSetName = 'AutomationNumber', Mandatory)]
55+
# [Parameter(ParameterSetName = 'SessionNumber', Mandatory)]
56+
# [string] $Number,
5857

5958
[Parameter(Mandatory)]
6059
[ValidateScript( {
@@ -66,39 +65,41 @@ Function Add-ServiceNowAttachment {
6665
[Parameter()]
6766
[string] $ContentType,
6867

68+
# Allow the results to be shown
69+
[Parameter()]
70+
[switch] $PassThru,
71+
6972
# Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
70-
[Parameter(ParameterSetName = 'AutomationSysId', Mandatory)]
71-
[Parameter(ParameterSetName = 'AutomationNumber', Mandatory)]
72-
[ValidateNotNullOrEmpty()]
73+
[Parameter()]
74+
# [ValidateNotNullOrEmpty()]
7375
[Hashtable] $Connection,
7476

75-
[Parameter(ParameterSetName = 'SessionSysId')]
76-
[Parameter(ParameterSetName = 'SessionNumber')]
77-
[ValidateNotNullOrEmpty()]
78-
[hashtable] $ServiceNowSession = $script:ServiceNowSession,
79-
80-
# Allow the results to be shown
8177
[Parameter()]
82-
[switch] $PassThru
78+
# [ValidateNotNullOrEmpty()]
79+
[hashtable] $ServiceNowSession = $script:ServiceNowSession
8380
)
8481

8582
begin {}
8683

8784
process {
8885

89-
90-
$getSysIdParams = @{
91-
Table = $Table
92-
Filter = @('number', '-eq', $number)
93-
Properties = 'sys_id'
86+
$getParams = @{
87+
Id = $Id
88+
Property = 'sys_class_name', 'sys_id', 'number'
9489
Connection = $Connection
9590
ServiceNowSession = $ServiceNowSession
9691
}
92+
if ( $Table ) {
93+
$getParams.Table = $Table
94+
}
95+
$tableRecord = Get-ServiceNowRecord @getParams
9796

98-
# Use the number and table to determine the sys_id
99-
$sysId = Invoke-ServiceNowRestMethod @getSysIdParams | Select-Object -ExpandProperty sys_id
97+
if ( -not $tableRecord ) {
98+
Write-Error "Record not found for Id '$Id'"
99+
continue
100+
}
100101

101-
$auth = Get-ServiceNowAuth -C $Connection -S ServiceNowSession
102+
$auth = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
102103

103104
ForEach ($Object in $File) {
104105
$FileData = Get-ChildItem $Object -ErrorAction Stop
@@ -114,15 +115,15 @@ Function Add-ServiceNowAttachment {
114115
# POST: https://instance.service-now.com/api/now/attachment/file?table_name=incident&table_sys_id=d71f7935c0a8016700802b64c67c11c6&file_name=Issue_screenshot
115116
# $Uri = "{0}/file?table_name={1}&table_sys_id={2}&file_name={3}" -f $ApiUrl, $Table, $TableSysID, $FileData.Name
116117
$invokeRestMethodSplat = $auth
117-
$invokeRestMethodSplat.Uri += '/attachment/file?table_name={0}&table_sys_id={1}&file_name={2}' -f $Table, $sysId, $FileData.Name
118+
$invokeRestMethodSplat.Uri += '/attachment/file?table_name={0}&table_sys_id={1}&file_name={2}' -f $tableRecord.sys_class_name, $tableRecord.sys_id, $FileData.Name
118119
$invokeRestMethodSplat.Headers += @{'Content-Type' = $ContentType }
119120
$invokeRestMethodSplat.UseBasicParsing = $true
120121
$invokeRestMethodSplat += @{
121122
Method = 'POST'
122123
InFile = $FileData.FullName
123124
}
124125

125-
If ($PSCmdlet.ShouldProcess("$Table $Number", 'Add attachment')) {
126+
If ($PSCmdlet.ShouldProcess(('{0} {1}' -f $tableRecord.sys_class_name, $tableRecord.number), ('Add attachment {0}' -f $FileData.FullName))) {
126127
Write-Verbose ($invokeRestMethodSplat | ConvertTo-Json)
127128
$response = Invoke-WebRequest @invokeRestMethodSplat
128129

0 commit comments

Comments
 (0)