-
Notifications
You must be signed in to change notification settings - Fork 170
Add Additional Namespace & Catalog Request Support #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
fe9b6b8
ba19c85
8869f17
86d60f2
ec233ed
1f61779
92530f7
4295689
38dc592
4f6bf2b
426dd67
3ca6dbc
7b0333b
fe37223
642ece9
481e66c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
CATgwalker marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| <# | ||
| .SYNOPSIS | ||
| Submit a catalog request using Service Catalog API | ||
|
|
||
| .DESCRIPTION | ||
| 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 | ||
|
|
||
| .PARAMETER CatalogItem | ||
| Name or ID of the catalog item that will be created | ||
|
|
||
| .PARAMETER Variables | ||
| Key/value pairs of variable names and their values | ||
|
|
||
| .PARAMETER CheckoutImmediately | ||
| If provided, a second Post for cart checkout to submit_order API Endpoint will be sent | ||
|
|
||
| .PARAMETER PassThru | ||
| If provided, the new record will be returned | ||
|
|
||
| .PARAMETER Connection | ||
| Azure Automation Connection object containing username, password, and URL for the ServiceNow instance | ||
|
|
||
| .PARAMETER ServiceNowSession | ||
| ServiceNow session created by New-ServiceNowSession. Will default to script-level variable $ServiceNowSession. | ||
|
|
||
| .EXAMPLE | ||
| New-ServiceNowCatalogItem -CatalogItem "Standard Laptop" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' } | ||
|
|
||
| Raise a new catalog request using Item Name | ||
|
|
||
| .EXAMPLE | ||
| New-ServiceNowCatalogItem -CatalogItem "04b7e94b4f7b42000086eeed18110c7fd" -Variables @{'acrobat' = 'true'; 'photoshop' = 'true'; ' Additional_software_requirements' = 'Testing Service catalog API' } | ||
|
|
||
| Raise a new catalog request using Item ID | ||
|
|
||
| .INPUTS | ||
| InputData | ||
|
|
||
| .OUTPUTS | ||
| PSCustomObject if PassThru provided | ||
| #> | ||
| function New-ServiceNowCatalogItem { | ||
| [CmdletBinding(SupportsShouldProcess)] | ||
| param | ||
| ( | ||
| [Parameter(Mandatory)] | ||
| [string]$CatalogItem, | ||
| [Parameter(Mandatory)] | ||
| [Alias('Variables')] | ||
| [hashtable]$InputData, | ||
| [Parameter()][Hashtable]$Connection, | ||
| [Parameter()][hashtable]$ServiceNowSession = $script:ServiceNowSession, | ||
| [Parameter()][switch]$CheckoutImmediately, | ||
| [Parameter()][switch]$PassThru | ||
| ) | ||
|
|
||
| begin { | ||
| if (-not $PSBoundParameters.ContainsKey('CheckoutImmediately')) { | ||
| $CheckoutImmediately = $false | ||
| } | ||
| if ($CatalogItem -match '^[a-zA-Z0-9]{32}$') { | ||
| #Verify the sys_id of the Catalog Item | ||
| $CatalogItemID = Get-ServiceNowRecord -Table sc_cat_item -AsValue -ID $CatalogItem -Property sys_id | ||
| if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by ID '$($CatalogItem)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItem)'" } | ||
| } else { | ||
| #Lookup the sys_id of the Catalog Item | ||
| $CatalogItemID = Get-ServiceNowRecord -Table sc_cat_item -AsValue -Filter @('name', '-eq', $CatalogItem ) -Property sys_id | ||
| if ([string]::IsNullOrEmpty($CatalogItemID)) { throw "Unable to find catalog item by name '$($CatalogItem)'" } else { Write-Verbose "Found $($catalogitemid) via lookup from '$($CatalogItem)'" } | ||
| } | ||
| } | ||
| process { | ||
|
|
||
| $AddItemToCart = @{ | ||
| Method = 'Post' | ||
| UriLeaf = "/servicecatalog/items/{0}/add_to_cart" -f $CatalogItemID | ||
| Values = @{'sysparm_quantity' = 1; 'variables' = $InputData } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should make quantity a function parameter along with the other request body parameters, sysparm_also_request_for and sysparm_requested_for. https://www.servicenow.com/docs/bundle/zurich-api-reference/page/integrate/inbound-rest/concept/c_ServiceCatalogAPI.html#title_servicecat-POST-items-add_to_cart. add examples for these new parameters as well. |
||
| Namespace = 'sn_sc' | ||
| Connection = $Connection | ||
| ServiceNowSession = $ServiceNowSession | ||
| } | ||
|
|
||
| if ( $PSCmdlet.ShouldProcess($CatalogItemID, 'Create new catalog item request') ) { | ||
|
|
||
| $AddItemCartResponse = Invoke-ServiceNowRestMethod @AddItemToCart | ||
|
|
||
| if ($AddItemCartResponse.cart_id -and $CheckoutImmediately) { | ||
| $SubmitOrder = @{ | ||
| Method = 'Post' | ||
| UriLeaf = "/servicecatalog/cart/submit_order" | ||
| Namespace = 'sn_sc' | ||
| Connection = $Connection | ||
| ServiceNowSession = $ServiceNowSession | ||
| } | ||
|
|
||
| $SubmitOrderResponse = Invoke-ServiceNowRestMethod @SubmitOrder | ||
|
|
||
| if ($PassThru) { | ||
| $SubmitOrderResponse | Select-Object @{'n' = 'number'; 'e' = { $_.request_number } }, request_id | ||
| } | ||
| } | ||
| } else { | ||
| $AddItemToCart | Out-String | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.