Extract Response of Invoke-RESTMethod

nottyheadedboss
Kilo Contributor

I have this piece of code in Powershell which creates a new ticket in ServiceNow as part of an URL Monitoring Service, every 5 minutes. If there is no ping from the URL, I raise an incident. I am successfully able to create an incident, but I now need to retrieve the IncidentID to share it in an email to the team or for further modifications.

The variable body contains the data to be inserted in an XML format.

$Response = Invoke-RestMethod -Uri $URI   -Credential $SNowCreds -Method Post -Body $body -ContentType 'application/xml'
Write-IntoLog $Response.result

The output is stored in $Response and is as below

@{parent=; made_sla=true; caused_by=; watch_list=; upon_reject=Cancel all future Tasks; sys_updated_on=2018-01-11 08:49:50; child_incidents=0; hold_reason=; approval_history=; number=INC0010079; resolved_by=; sys_updated_by=Admin; opened_by=; user_input=; sys_created_on=2018-01-11 08:49:50; sys_domain=; state=In Progress; sys_created_by=Admin; knowledge=false; order=; calendar_stc=; closed_at=; cmdb_ci=; delivery_plan=; impact=2 - Medium; active=true; work_notes_list=; business_service=; priority=2 - High; sys_domain_path=/; rfc=; time_worked=; expected_start=; opened_at=2018-01-11 08:49:50; business_duration=; group_list=; work_end=; caller_id=; reopened_time=; resolved_at=; approval_set=; subcategory=Internal Application; work_notes=; short_description=Issues with Accessing Web URL; close_code=; correlation_display=; delivery_task=; work_start=; assignment_group=; additional_assignee_list=; business_stc=; description=We are facing issues with accessing the portal https://www.inmotion.com. The error Received is a 403 Error and Access to the same; calendar_duration=; close_notes=; notify=Do Not Notify; sys_class_name=Incident; closed_by=; follow_up=; parent_incident=; sys_id=2c40df9edb234300479a7e7dbf96198f; contact_type=; reopened_by=; incident_state=In Progress; urgency=1 - High; problem_id=; company=; reassignment_count=0; activity_due=UNKNOWN; assigned_to=; severity=3 - Low; comments=; approval=Not Yet Requested; sla_due=UNKNOWN; comments_and_work_notes=; due_date=; sys_mod_count=0; reopen_count=0; sys_tags=; escalation=Normal; upon_approval=Proceed to Next Task; correlation_id=; location=; category=Network}

From the output I want to extract the number(Incident ID) and also generate a link to the ServiceNow Incident.

However I am unable to extract the number(Incident ID). I tried converting the piece of output to JSON

$Response = Invoke-RestMethod -Uri $URI   -Credential $SNowCreds -Method Post -Body $body -ContentType 'application/xml' | ConvertTo-Json

I tried to navigate it through the nodes. I am still not able to retrieve the IncidentID Request your assistance in the above issue


					
				
			
			
				
			
			
				
			
			
			
			
			
			
		
3 REPLIES 3

Bryan Tay3
Mega Guru

hi Arvind,



Without using the 'ConvertTo-Json',


have u try


        Write-host "The number is   $($response.result.number)"



Other useful reference:


        Powershell ServiceNow API



Hope this helps.


Thanks for the answer Bryan Tay



On a Friday morning, your help was just how I needed to start my day..


Santhosh48
Kilo Explorer

Hello Sir,

 

Today I got the similar case what you have explained above. Do you get chance to identify the fix for this?

I want to capture the incident ID after creating an incident in SNOW through invoke-restmethod. Looking forward for your response.

Thank you