Skip to content

Conversation

CATgwalker
Copy link
Contributor

This PR adds a cmdlet for New-ServiceNowCatalogItem which uses the 'sn_sc' API namespace to add a catalog item request to a cart and then submits the cart as an order.

Extends Get-ServiceNowAuth and Invoke-ServiceNowRestMethod with a namespace parameter to support different Namespace endpoints; backwards compatibility is maintained by defaulting to the 'now' namespace if not specified.

CATgwalker and others added 8 commits April 10, 2024 15:53
Fixes error "Id must either be a SysId 32 character alphanumeric or Number with prefix and id." and retains ID from reference table for further lookup.
Additional fixes for reference table lookup where value is not in the 32 character format expected by SNOW.
Add Namespace parameter to support different API endpoints; defaults to 'now'
Versioned PSD with Minor increase for new functionality and backwards-compatible changes.
@gdbarron
Copy link
Collaborator

gdbarron commented Oct 5, 2025

Awesome stuff, I'll review this week!

Add New-ServiceNowCatalog declaration to FunctionsToExport.
Copy link
Collaborator

@gdbarron gdbarron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much, looks good, just some tweaks here and there :)

Comment on lines +46 to +49
[Parameter(Mandatory, ParameterSetName = 'Name')]
[string]$CatalogItemName,
[Parameter(Mandatory, ParameterSetName = 'ID')]
[string]$CatalogItemID,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about we combine this into one CatalogItem and check its format to know if its an ID or name?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take care of updating this file when I perform the release. Can you please undo these changes?

Comment on lines +37 to +41
if ($Namespace -ne 'now') {
$hashOut.Uri = $($ServiceNowSession.BaseUri -split ('api'))[0] + 'api/' + $Namespace
} else {
$hashOut.Uri = $ServiceNowSession.BaseUri
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we're better served by changing the baseuri to end with /api and just append the namespace. this will involve changes here and invoke-service nowrestmethod.

Comment on lines -84 to +89
elseif ( $Connection ) {
} elseif ( $Connection ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please try and keep formatting the same :)

[string] $FilterString,

[parameter()]
[string] $Namespace,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default to 'now'?

ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession.
.EXAMPLE
New-ServiceNowRecord -CatalogItemName "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
New-ServiceNowRecord -CatalogItemName "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }
New-ServiceNowCatalogItem -CatalogItem "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' }

Comment on lines +80 to +84
if ($namespace) {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace
} else {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ($namespace) {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace
} else {
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession
}
$params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace


$AddItemCartResponse = Invoke-ServiceNowRestMethod @AddItemToCart

if ($AddItemCartResponse.cart_id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why might we not get a value for cart_id that we need this 'if'?

Comment on lines +92 to +94
if ( $PassThru ) {
$SubmitOrderResponse
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are going to keep the check for cart_id, this needs to move inside the 'if' block otherwise $SubmitOrderResponse would be null

$SubmitOrderResponse = Invoke-ServiceNowRestMethod @SubmitOrder
}
if ( $PassThru ) {
$SubmitOrderResponse
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$SubmitOrderResponse
$SubmitOrderResponse | Select-Object @{'n'='number';'e'={$_.request_number}}, request_id

consider updating the output to something like the above so it can be piped directly into the other functions, eg. Get-ServiceNowRecord

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be good to have a way to control when checkout occurs in case folks want to add multiple (different) items before submitting order

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants