In order to query the servicenow incident records and to list out the required fields use the sysparm_fields column which helps in optimization. Now, See the code, result and performance
All Fields
function Get-ServiceNowIncident { [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] $IncidentID, [Parameter(Mandatory)] [pscredential] $Credential ) try { $Uri = "https://dev42835.service-now.com/api/now/table/incident?sysparm_query=number=$($IncidentID)" $Result = Invoke-RestMethod -Uri $Uri -Method Get -Credential $Credential $Result.result | ConvertTo-Json } catch { $_.Exception } } Measure-Command -Expression {Get-ServiceNowIncident -IncidentID INC0000059 -Credential admin}
Here is the result! (8 seconds for one record)
Days : 0 Hours : 0 Minutes : 0 Seconds : 8 Milliseconds : 468 Ticks : 84680174 TotalDays : 9.80094606481481E-05 TotalHours : 0.00235222705555556 TotalMinutes : 0.141133623333333 TotalSeconds : 8.4680174 TotalMilliseconds : 8468.0174
Selected Fields!
function Get-ServiceNowIncident { [CmdletBinding()] param ( [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] $IncidentID, [Parameter(Mandatory)] [pscredential] $Credential ) try { $Uri = "https://dev42835.service-now.com/api/now/table/incident?sysparm_query=number=$($IncidentID)&sysparm_fields=number,state,opened_at&sysparm_limit=1" $Result = Invoke-RestMethod -Uri $Uri -Method Get -Credential $Credential $Result.result | ConvertTo-Json } catch { $_.Exception } } Measure-Command -Expression {Get-ServiceNowIncident -IncidentID INC0000059 -Credential admin}
Days : 0 Hours : 0 Minutes : 0 Seconds : 6 Milliseconds : 540 Ticks : 65405344 TotalDays : 7.57006296296296E-05 TotalHours : 0.00181681511111111 TotalMinutes : 0.109008906666667 TotalSeconds : 6.5405344 TotalMilliseconds : 6540.5344
It’s 6 seconds! Approximately we get 2 seconds per record. Good Sign! And see how it impacts if you select with pipe line for all fields.
Get-ServiceNowIncident -IncidentID INC0000059 -Credential admin | Select number , state , opened_at
Days : 0 Hours : 0 Minutes : 0 Seconds : 10 Milliseconds : 540 Ticks : 65405344 TotalDays : 7.57006296296296E-05 TotalHours : 0.00181681511111111 TotalMinutes : 0.109008906666667 TotalSeconds : 6.5405344 TotalMilliseconds : 6540.5344
Tip: Use the filter parameters as effective as possible.
3 thoughts on “TIP: Query servicenow Incident record using PowerShell and REST API”
Is there a way to do this without access to the API URL? My org is very restrictive yet I’d like to be able to manage my tickets through Powershell. They also use SSO
The User needs no roles to make REST API calls. However, in order to create records it depends upon the ACL for that table. Connect with your tools admin and get Web Service Role access or it_admin role (I am not sure).
Нi, yeah this pоst is in fact good and I have learned
lot of thingѕ from іt about ƅlogging.
thanks.