-
Notifications
You must be signed in to change notification settings - Fork 171
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
base: master
Are you sure you want to change the base?
Changes from all commits
fe9b6b8
ba19c85
8869f17
86d60f2
ec233ed
1f61779
92530f7
4295689
38dc592
4f6bf2b
426dd67
3ca6dbc
7b0333b
fe37223
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "githubPullRequests.ignoredPullRequestBranches": [ | ||
| "master" | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,7 +17,7 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| [CmdletBinding(SupportsPaging)] | ||||||||||||||
| [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseBOMForUnicodeEncodedFile', '', Justification = 'issuees with *nix machines and no benefit')] | ||||||||||||||
|
|
||||||||||||||
| Param ( | ||||||||||||||
| param ( | ||||||||||||||
CATgwalker marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| [parameter()] | ||||||||||||||
| [ValidateSet('Get', 'Post', 'Patch', 'Delete')] | ||||||||||||||
| [string] $Method = 'Get', | ||||||||||||||
|
|
@@ -48,6 +48,9 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| [parameter()] | ||||||||||||||
| [string] $FilterString, | ||||||||||||||
|
|
||||||||||||||
| [parameter()] | ||||||||||||||
| [string] $Namespace, | ||||||||||||||
|
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. default to 'now'? |
||||||||||||||
|
|
||||||||||||||
| [parameter()] | ||||||||||||||
| [object[]] $Sort = @('opened_at', 'desc'), | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -74,7 +77,11 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # get header/body auth values | ||||||||||||||
| $params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession | ||||||||||||||
| if ($namespace) { | ||||||||||||||
| $params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession -N $namespace | ||||||||||||||
| } else { | ||||||||||||||
| $params = Get-ServiceNowAuth -C $Connection -S $ServiceNowSession | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+80
to
+84
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.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| $params.Method = $Method | ||||||||||||||
| $params.ContentType = 'application/json' | ||||||||||||||
|
|
@@ -93,8 +100,7 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| if ( $SysId ) { | ||||||||||||||
| $params.Uri += "/$SysId" | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| } else { | ||||||||||||||
| $params.Uri += $UriLeaf | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -153,8 +159,7 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| try { | ||||||||||||||
| $response = Invoke-WebRequest @params | ||||||||||||||
| Write-Debug $response | ||||||||||||||
| } | ||||||||||||||
| catch { | ||||||||||||||
| } catch { | ||||||||||||||
| $ProgressPreference = $oldProgressPreference | ||||||||||||||
| throw $_ | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -174,12 +179,10 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| $content = $response.content | ConvertFrom-Json | ||||||||||||||
| if ( $content.PSobject.Properties.Name -contains "result" ) { | ||||||||||||||
| $records = @($content | Select-Object -ExpandProperty result) | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| } else { | ||||||||||||||
| $records = @($content) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| } else { | ||||||||||||||
| # invoke-webrequest didn't throw an error per se, but we didn't get content back either | ||||||||||||||
| throw ('"{0} : {1}' -f $response.StatusCode, $response | Out-String ) | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -190,8 +193,7 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| if ( $response.Headers.'X-Total-Count' ) { | ||||||||||||||
| if ($PSVersionTable.PSVersion.Major -lt 6) { | ||||||||||||||
| $totalRecordCount = [int]$response.Headers.'X-Total-Count' | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| } else { | ||||||||||||||
| $totalRecordCount = [int]($response.Headers.'X-Total-Count'[0]) | ||||||||||||||
| } | ||||||||||||||
| Write-Verbose "Total number of records for this query: $totalRecordCount" | ||||||||||||||
|
|
@@ -215,25 +217,22 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
|
|
||||||||||||||
| $end = if ( $totalRecordCount -lt $setPoint ) { | ||||||||||||||
| $totalRecordCount | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| } else { | ||||||||||||||
| $setPoint | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Write-Verbose ('getting {0}-{1} of {2}' -f ($params.body.sysparm_offset + 1), $end, $totalRecordCount) | ||||||||||||||
| try { | ||||||||||||||
| $response = Invoke-WebRequest @params -Verbose:$false | ||||||||||||||
| } | ||||||||||||||
| catch { | ||||||||||||||
| } catch { | ||||||||||||||
| $ProgressPreference = $oldProgressPreference | ||||||||||||||
| throw $_ | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| $content = $response.content | ConvertFrom-Json | ||||||||||||||
| if ( $content.PSobject.Properties.Name -contains "result" ) { | ||||||||||||||
| $records += $content | Select-Object -ExpandProperty result | ||||||||||||||
| } | ||||||||||||||
| else { | ||||||||||||||
| } else { | ||||||||||||||
| $records += $content | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -249,18 +248,17 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| switch ($Method) { | ||||||||||||||
| 'Get' { | ||||||||||||||
| $ConvertToDateField = @('closed_at', 'expected_start', 'follow_up', 'opened_at', 'sys_created_on', 'sys_updated_on', 'work_end', 'work_start') | ||||||||||||||
| ForEach ($SNResult in $records) { | ||||||||||||||
| ForEach ($Property in $ConvertToDateField) { | ||||||||||||||
| If (-not [string]::IsNullOrEmpty($SNResult.$Property)) { | ||||||||||||||
| Try { | ||||||||||||||
| foreach ($SNResult in $records) { | ||||||||||||||
| foreach ($Property in $ConvertToDateField) { | ||||||||||||||
| if (-not [string]::IsNullOrEmpty($SNResult.$Property)) { | ||||||||||||||
| try { | ||||||||||||||
| # Extract the default Date/Time formatting from the local computer's "Culture" settings, and then create the format to use when parsing the date/time from Service-Now | ||||||||||||||
| $CultureDateTimeFormat = (Get-Culture).DateTimeFormat | ||||||||||||||
| $DateFormat = $CultureDateTimeFormat.ShortDatePattern | ||||||||||||||
| $TimeFormat = $CultureDateTimeFormat.LongTimePattern | ||||||||||||||
| $DateTimeFormat = [string[]]@("$DateFormat $TimeFormat", 'yyyy-MM-dd HH:mm:ss') | ||||||||||||||
| $SNResult.$Property = [DateTime]::ParseExact($($SNResult.$Property), $DateTimeFormat, [System.Globalization.DateTimeFormatInfo]::InvariantInfo, [System.Globalization.DateTimeStyles]::None) | ||||||||||||||
| } | ||||||||||||||
| Catch { | ||||||||||||||
| } catch { | ||||||||||||||
| # If the local culture and universal formats both fail keep the property as a string (Do nothing) | ||||||||||||||
| $null = 'Silencing a PSSA alert with this line' | ||||||||||||||
| } | ||||||||||||||
|
|
@@ -283,4 +281,4 @@ function Invoke-ServiceNowRestMethod { | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| $records | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
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. might be good to have a way to control when checkout occurs in case folks want to add multiple (different) items before submitting order
Contributor
Author
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. I added the CheckoutImmediately switch parameter as well as new function 'Submit-ServiceNowCatalogOrder' to support delayed checkout. I feel that most use cases will want to use CheckoutImmediately as the default. |
| 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 | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree and I tried to add the Namespace support functionality with as limited of breaking changes as I could. However, I didn't want to modify the New-ServiceNowSession function where BaseUri is originally declared into script scope variable $ServiceNowSession the due to a lack of familiarity with version specification and GraphQL use cases.