Skip to content

Commit 426dd67

Browse files
committed
Adjust New-ServiceNowCatalogItem to reduce parameter complexity by supporting ID or name lookup for CatalogItem.
1 parent 4f6bf2b commit 426dd67

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

ServiceNow/Public/New-ServiceNowCatalogItem.ps1

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
.DESCRIPTION
66
Create a new catalog item request using Service Catalog API. Reference: https://www.servicenow.com/community/itsm-articles/submit-catalog-request-using-service-catalog-api/ta-p/2305836
77
8-
.PARAMETER CatalogItemName
9-
Name of the catalog item that will be created
10-
11-
.PARAMETER CatalogItemID
12-
SysID of the catalog item that will be created
8+
.PARAMETER CatalogItem
9+
Name or ID of the catalog item that will be created
1310
1411
.PARAMETER Variables
1512
Key/value pairs of variable names and their values
@@ -24,12 +21,12 @@
2421
ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
2522
2623
.EXAMPLE
27-
New-ServiceNowRecord -CatalogItemName "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }
24+
New-ServiceNowCatalogItem -CatalogItem "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }
2825
2926
Raise a new catalog request using Item Name
3027
3128
.EXAMPLE
32-
New-ServiceNowRecord -CatalogItemID "04b7e94b4f7b42000086eeed18110c7fd" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }
29+
New-ServiceNowCatalogItem -CatalogItem "04b7e94b4f7b42000086eeed18110c7fd" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }
3330
3431
Raise a new catalog request using Item ID
3532
@@ -40,15 +37,12 @@
4037
PSCustomObject if PassThru provided
4138
#>
4239
function New-ServiceNowCatalogItem {
43-
[CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'ID')]
40+
[CmdletBinding(SupportsShouldProcess)]
4441
param
4542
(
46-
[Parameter(Mandatory, ParameterSetName = 'Name')]
47-
[string]$CatalogItemName,
48-
[Parameter(Mandatory, ParameterSetName = 'ID')]
49-
[string]$CatalogItemID,
50-
[Parameter(Mandatory, ParameterSetName = 'Name')]
51-
[Parameter(Mandatory, ParameterSetName = 'ID')]
43+
[Parameter(Mandatory)]
44+
[string]$CatalogItem,
45+
[Parameter(Mandatory)]
5246
[Alias('Variables')]
5347
[hashtable]$InputData,
5448
[Parameter()][Hashtable]$Connection,
@@ -57,10 +51,13 @@ function New-ServiceNowCatalogItem {
5751
)
5852

5953
begin {
60-
if ($CatalogItemName) {
61-
#Lookup the sys_id of the Catalog Item name
62-
$CatalogItemID = (Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItemName )).sys_id
63-
if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by name '$($catalogitemname)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItemName)'" }
54+
if ($CatalogItem -match '^[a-zA-Z0-9]{32}$') {
55+
#Verify the sys_id of the Catalog Item
56+
$CatalogItemID = (Get-ServiceNowRecord -Table sc_cat_item -AsValue -ID $CatalogItem).sys_id
57+
} else {
58+
#Lookup the sys_id of the Catalog Item
59+
$CatalogItemID = (Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItem )).sys_id
60+
if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by name '$($CatalogItem)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItem)'" }
6461
}
6562
}
6663
process {

0 commit comments

Comments
 (0)