从Invoke-RestMethod

时间:2018-01-11 17:08:53

标签: powershell servicenow-rest-api

我有这段代码在ServiceNow中创建一张新票证

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

输出存储在 $ Response 中,如下所示

@{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}

从输出中我想提取数字(事件ID)并生成ServiceNow事件的链接。

但是我无法提取号码(事件ID)。我尝试将输出转换为JSON

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

我试图通过节点导航它。我仍然无法检索 IncidentID 在上述问题中请求您的帮助。

1 个答案:

答案 0 :(得分:0)

不使用' ConvertTo-Json'

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

这很有效。

其他有用的参考:      Powershell ServiceNow API