Skip to content

Commit 7c1f3ab

Browse files
committed
cred-url cleanup
1 parent e360826 commit 7c1f3ab

13 files changed

+206
-242
lines changed

ServiceNow/Public/Export-ServiceNowAttachment.ps1

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
1-
Function Export-ServiceNowAttachment {
2-
<#
3-
.SYNOPSIS
4-
Save a ServiceNow attachment identified by its sys_id property and saved as the filename specified.
1+
<#
2+
.SYNOPSIS
3+
Save a ServiceNow attachment identified by its sys_id property and saved as the filename specified.
4+
5+
.DESCRIPTION
6+
Save a ServiceNow attachment identified by its sys_id property and saved as the filename specified.
57
6-
.DESCRIPTION
7-
Save a ServiceNow attachment identified by its sys_id property and saved as the filename specified.
8+
.PARAMETER SysID
9+
The ServiceNow sys_id of the file
810
9-
.PARAMETER SysID
10-
The ServiceNow sys_id of the file
11+
.PARAMETER FileName
12+
File name the file is saved as. Do not include the path.
1113
12-
.PARAMETER FileName
13-
File name the file is saved as. Do not include the path.
14+
.PARAMETER Destination
15+
Path the file is saved to. Do not include the file name.
1416
15-
.PARAMETER Destination
16-
Path the file is saved to. Do not include the file name.
17+
.PARAMETER AllowOverwrite
18+
Allows the function to overwrite the existing file.
1719
18-
.PARAMETER AllowOverwrite
19-
Allows the function to overwrite the existing file.
20+
.PARAMETER AppendNameWithSysID
21+
Adds the SysID to the file name. Intended for use when a ticket has multiple files with the same name.
2022
21-
.PARAMETER AppendNameWithSysID
22-
Adds the SysID to the file name. Intended for use when a ticket has multiple files with the same name.
23+
.EXAMPLE
24+
Export-ServiceNowAttachment -SysID $SysID -FileName 'mynewfile.txt'
2325
24-
.EXAMPLE
25-
Export-ServiceNowAttachment -SysID $SysID -FileName 'mynewfile.txt'
26+
Save the attachment with the specified sys_id with a name of 'mynewfile.txt'
2627
27-
Save the attachment with the specified sys_id with a name of 'mynewfile.txt'
28+
.EXAMPLE
29+
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment
2830
29-
.EXAMPLE
30-
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment
31+
Save all attachments from the ticket. Filenames will be assigned from the attachment name.
3132
32-
Save all attachments from the ticket. Filenames will be assigned from the attachment name.
33+
.EXAMPLE
34+
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment -AppendNameWithSysID
3335
34-
.EXAMPLE
35-
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment -AppendNameWithSysID
36+
Save all attachments from the ticket. Filenames will be assigned from the attachment name and appended with the sys_id.
3637
37-
Save all attachments from the ticket. Filenames will be assigned from the attachment name and appended with the sys_id.
38+
.EXAMPLE
39+
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment -Destination $path -AllowOverwrite
3840
39-
.EXAMPLE
40-
Get-ServiceNowAttachment -Id INC1234567 | Export-ServiceNowAttachment -Destination $path -AllowOverwrite
41+
Save all attachments from the ticket to the destination allowing for overwriting the destination file.
42+
#>
43+
Function Export-ServiceNowAttachment {
4144

42-
Save all attachments from the ticket to the destination allowing for overwriting the destination file.
43-
#>
45+
[CmdletBinding(SupportsShouldProcess)]
4446

45-
[CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess = $true)]
4647
Param(
4748

4849
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
@@ -68,13 +69,10 @@ Function Export-ServiceNowAttachment {
6869
[parameter()]
6970
[switch] $AppendNameWithSysId,
7071

71-
# Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
72-
[Parameter(ParameterSetName = 'Automation', Mandatory)]
73-
[ValidateNotNullOrEmpty()]
72+
[Parameter()]
7473
[Hashtable] $Connection,
7574

76-
[Parameter(ParameterSetName = 'Session')]
77-
[ValidateNotNullOrEmpty()]
75+
[Parameter()]
7876
[hashtable] $ServiceNowSession = $script:ServiceNowSession
7977
)
8078

ServiceNow/Public/Get-ServiceNowRecord.ps1

Lines changed: 91 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<#
22
.SYNOPSIS
3-
Retrieves records for the specified table
3+
Retrieves records for any and all tables
44
55
.DESCRIPTION
6-
Retrieve records from any table with the option to filter, sort, and choose fields.
7-
Given you know the table name, you shouldn't need any other 'Get-' function.
6+
Retrieve records from any table with the option to filter, sort, choose fields, and more.
87
Paging is supported with -First, -Skip, and -IncludeTotalCount.
98
109
.PARAMETER Table
@@ -15,10 +14,18 @@
1514
Either the record sys_id or number.
1615
If providing just an Id, not with Table, the Id prefix will be looked up to find the table name.
1716
18-
.PARAMETER Name
17+
.PARAMETER ParentId
18+
The sys_id or number of the parent record.
19+
For example, to get catalog tasks for a requested item, provide the RITM number as ParentId.
20+
21+
.PARAMETER Description
22+
Filter results based on the 'description' field. The field will be different for each table.
23+
For many tables it will be short_description, but, for instance, the User table will be 'Name'.
24+
For unknown tables, the field will be 'short_description'.
25+
The comparison performed is a 'like'.
1926
2027
.PARAMETER Property
21-
Limit the fields returned to this list
28+
Return one or more specific fields
2229
2330
.PARAMETER Filter
2431
Array or multidimensional array of fields and values to filter on.
@@ -51,7 +58,19 @@
5158
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
5259
5360
.EXAMPLE
54-
Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1'), 'or', @('short_description','-like', 'powershell')
61+
Get-ServiceNowRecord RITM0010001
62+
Get a specific record by number
63+
64+
.EXAMPLE
65+
Get-ServiceNowRecord -Id RITM0010001 -Property 'short_description','sys_id'
66+
Get specific properties for a record
67+
68+
.EXAMPLE
69+
Get-ServiceNowRecord -Table 'Catalog Task' -ParentId 'RITM0010001'
70+
Get tasks for the parent requested item
71+
72+
.EXAMPLE
73+
Get-ServiceNowRecord -Table incident -Filter @('state', '-eq', '1') -Description 'powershell'
5574
Get incident records where state equals New or short description contains the word powershell
5675
5776
.EXAMPLE
@@ -61,7 +80,7 @@
6180
'-group',
6281
@('state', '-eq', '2')
6382
PS > Get-ServiceNowRecord -Table incident -Filter $filter
64-
Get incident records where state equals New and short description contains the word powershell or state equals In Progress.
83+
Get incident records where state is New and short description contains the word powershell or state is In Progress.
6584
The first 2 filters are combined and then or'd against the last.
6685
6786
.EXAMPLE
@@ -80,6 +99,10 @@
8099
Get-ServiceNowRecord -Table 'change request' -IncludeCustomVariable -First 5
81100
Get the first 5 change requests and retrieve custom variable info
82101
102+
.EXAMPLE
103+
gsnr RITM0010001
104+
Get a specific record by number using the function alias
105+
83106
.INPUTS
84107
None
85108
@@ -106,7 +129,10 @@ function Get-ServiceNowRecord {
106129
[string] $Id,
107130

108131
[Parameter()]
109-
[string] $Name,
132+
[string] $ParentId,
133+
134+
[Parameter()]
135+
[string] $Description,
110136

111137
[Parameter()]
112138
[Alias('Fields', 'Properties')]
@@ -134,52 +160,84 @@ function Get-ServiceNowRecord {
134160
[hashtable] $ServiceNowSession = $script:ServiceNowSession
135161
)
136162

137-
# it's easier this way to pass everything to invoke-servicenowrestmethod given paging params, etc
138-
$invokeParams = $PSBoundParameters
139-
$invokeParams.Remove('IncludeCustomVariable') | Out-Null
140-
$invokeParams.Remove('Id') | Out-Null
141-
$invokeParams.Remove('Name') | Out-Null
163+
$invokeParams = @{
164+
Table = $Table
165+
Filter = $Filter
166+
Property = $Property
167+
Sort = $Sort
168+
DisplayValue = $DisplayValue
169+
First = $PSCmdlet.PagingParameters.First
170+
Skip = $PSCmdlet.PagingParameters.Skip
171+
IncludeTotalCount = $PSCmdlet.PagingParameters.IncludeTotalCount
172+
Connection = $Connection
173+
ServiceNowSession = $ServiceNowSession
174+
}
142175

143176
if ( $Id ) {
144177
if ( $Id -match '[a-zA-Z0-9]{32}' ) {
145-
if ( $PSCmdlet.ParameterSetName -like '*Id' ) {
146-
throw 'Providing sys_id for -Id requires a value for -Table. Alternatively, provide an Id with a prefix, eg. INC1234567.'
178+
if ( $PSCmdlet.ParameterSetName -eq 'Id' ) {
179+
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.'
147180
}
148181

149182
$idFilter = @('sys_id', '-eq', $Id)
150183
}
151184
else {
152-
if ( $PSCmdlet.ParameterSetName -like '*Id' ) {
185+
if ( $PSCmdlet.ParameterSetName -eq 'Id' ) {
153186
# get table name from prefix if only Id was provided
154-
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $Id.ToLower().StartsWith($_.NumberPrefix) } | Select-Object -ExpandProperty Name
187+
$thisTable = $script:ServiceNowTable | Where-Object { $_.NumberPrefix -and $Id.ToLower().StartsWith($_.NumberPrefix) }
155188
if ( $thisTable ) {
156-
$invokeParams.Table = $thisTable
189+
$invokeParams.Table = $thisTable.Name
157190
}
158191
else {
159-
throw ('Prefix not found for Id ''{0}''. Known prefixes are {1}.' -f $Id, ($ServiceNowTable.NumberPrefix.Where( { $_ }) -join ', '))
192+
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 ', '))
160193
}
161194
}
162195
$idFilter = @('number', '-eq', $Id)
163196
}
164197

165-
if ( $invokeParmas.Filter ) {
198+
if ( $invokeParams.Filter ) {
166199
$invokeParams.Filter = $invokeParams.Filter, 'and', $idFilter
167200
}
168201
else {
169202
$invokeParams.Filter = $idFilter
170203
}
171204
}
205+
else {
206+
# table name was provided, get the config entry if there is one
207+
$thisTable = $script:ServiceNowTable | Where-Object { $_.Name.ToLower() -eq $Table.ToLower() -or $_.ClassName.ToLower() -eq $Table.ToLower() }
208+
}
172209

173-
if ( $Name ) {
174-
# determine the field we should compare for 'name' and add the filter
175-
$thisNameField = $script:ServiceNowTable | Where-Object { $_.Name.ToLower() -eq $Table.ToLower() -or $_.ClassName.ToLower() -eq $Table.ToLower() } | Select-Object -ExpandProperty TableNameField
176-
if ( $thisNameField ) {
177-
if ( $invokeParmas.Filter ) {
178-
$invokeParams.Filter = $invokeParams.Filter, 'and', @($thisNameField, '-like', $Name)
179-
}
180-
else {
181-
$invokeParams.Filter = @($thisNameField, '-like', $Name)
182-
}
210+
if ( $ParentId ) {
211+
if ( $ParentId -match '[a-zA-Z0-9]{32}' ) {
212+
$parentIdFilter = @('parent.sys_id', '-eq', $ParentId)
213+
}
214+
else {
215+
$parentIdFilter = @('parent.number', '-eq', $ParentId)
216+
}
217+
218+
if ( $invokeParams.Filter ) {
219+
$invokeParams.Filter = $invokeParams.Filter, 'and', $parentIdFilter
220+
}
221+
else {
222+
$invokeParams.Filter = $parentIdFilter
223+
}
224+
}
225+
226+
if ( $Description ) {
227+
# determine the field we should compare for 'description' and add the filter
228+
if ( $thisTable ) {
229+
$nameFilter = @($thisTable.DescriptionField, '-like', $Description)
230+
}
231+
else {
232+
Write-Warning ('We do not have a description field for table ''{0}''; short_description will be used' -f $Table)
233+
$nameFilter = @('short_description', '-like', $Description)
234+
}
235+
236+
if ( $invokeParams.Filter ) {
237+
$invokeParams.Filter = $invokeParams.Filter, 'and', $nameFilter
238+
}
239+
else {
240+
$invokeParams.Filter = $nameFilter
183241
}
184242
}
185243

@@ -193,6 +251,7 @@ function Get-ServiceNowRecord {
193251
}
194252
}
195253

254+
# should use Get-ServiceNowAttachment, but put this here for ease of access
196255
if ( $Table -eq 'attachment' ) {
197256
$invokeParams.Remove('Table') | Out-Null
198257
$invokeParams.UriLeaf = '/attachment'
@@ -246,9 +305,8 @@ function Get-ServiceNowRecord {
246305

247306
# format the results
248307
if ( -not $Property ) {
249-
$type = $script:ServiceNowTable | Where-Object { $_.Name -eq $Table -or $_.ClassName -eq $Table } | Select-Object -ExpandProperty Type
250-
if ($type) {
251-
$result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $type) }
308+
if ($thisTable.Type) {
309+
$result | ForEach-Object { $_.PSObject.TypeNames.Insert(0, $thisTable.Type) }
252310
}
253311
}
254312
$result

ServiceNow/Public/New-ServiceNowChangeRequest.ps1

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@ function New-ServiceNowChangeRequest {
3333
.PARAMETER CustomFields
3434
Custom fields as hashtable
3535
36-
.PARAMETER ServiceNowCredential
37-
Credential used to authenticate to ServiceNow
38-
39-
.PARAMETER ServiceNowURL
40-
The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
41-
4236
.PARAMETER Connection
4337
Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
4438
@@ -73,7 +67,7 @@ function New-ServiceNowChangeRequest {
7367
New-ServiceNowChangeRequest @newServiceNowChangeRequestSplat
7468
#>
7569

76-
[CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess)]
70+
[CmdletBinding(SupportsShouldProcess)]
7771

7872
Param(
7973
[parameter(Mandatory)]
@@ -103,20 +97,10 @@ function New-ServiceNowChangeRequest {
10397
[parameter()]
10498
[hashtable]$CustomFields,
10599

106-
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory)]
107-
[ValidateNotNullOrEmpty()]
108-
[PSCredential]$ServiceNowCredential,
109-
110-
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory)]
111-
[ValidateNotNullOrEmpty()]
112-
[string]$ServiceNowURL,
113-
114-
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)]
115-
[ValidateNotNullOrEmpty()]
116-
[Hashtable]$Connection,
100+
[Parameter()]
101+
[Hashtable] $Connection,
117102

118-
[Parameter(ParameterSetName = 'Session')]
119-
[ValidateNotNullOrEmpty()]
103+
[Parameter()]
120104
[hashtable] $ServiceNowSession = $script:ServiceNowSession,
121105

122106
[Parameter()]

ServiceNow/Public/New-ServiceNowIncident.ps1

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Generate an Incident by "Splatting" all fields used in the 1st example plus some
2828
#>
2929
function New-ServiceNowIncident {
3030

31-
[CmdletBinding(DefaultParameterSetName = 'Session', SupportsShouldProcess)]
31+
[CmdletBinding(SupportsShouldProcess)]
3232

3333
Param(
3434

@@ -69,24 +69,10 @@ function New-ServiceNowIncident {
6969
[Alias('CustomFields')]
7070
[hashtable] $CustomField,
7171

72-
# Credential used to authenticate to ServiceNow
73-
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory)]
74-
[ValidateNotNullOrEmpty()]
75-
[Alias('ServiceNowCredential')]
76-
[PSCredential] $Credential,
77-
78-
# The URL for the ServiceNow instance being used (eg: instancename.service-now.com)
79-
[Parameter(ParameterSetName = 'SpecifyConnectionFields', Mandatory)]
80-
[ValidateNotNullOrEmpty()]
81-
[string] $ServiceNowURL,
82-
83-
#Azure Automation Connection object containing username, password, and URL for the ServiceNow instance
84-
[Parameter(ParameterSetName = 'UseConnectionObject', Mandatory)]
85-
[ValidateNotNullOrEmpty()]
72+
[Parameter()]
8673
[Hashtable] $Connection,
8774

88-
[Parameter(ParameterSetName = 'Session')]
89-
[ValidateNotNullOrEmpty()]
75+
[Parameter()]
9076
[hashtable] $ServiceNowSession = $script:ServiceNowSession,
9177

9278
[Parameter()]

0 commit comments

Comments
 (0)