通过 Powershell 从 ServiceNow 中提取特定的 INC 或 CR

Pull Specific INC or CR from ServiceNow via Powershell

我只是想通过 powershell 根据其 sys_id 或编号从 ServiceNow 中提取事件或变更请求及其详细信息。我什至不知道从哪里开始。任何帮助将不胜感激!

看看GitHub中的这个项目:

https://github.com/Sam-Martin/servicenow-powershell

ServiceNow 为平台中的每个 table 公开标准 REST API。这是 ServiceNow Table API 的文档。如果您想检索特定的事件记录,您可以向以下端点发出 GET 请求:

https://instancename.service-now.com/api/now/table/incident/{incident_sys_id}

您可以将其与 PowerShell 的 Invoke-RestMethod cmdlet 结合使用以实际提取详细信息。示例:

$cred = Get-Credential
$uri = 'https://instance.service-now.com/api/now/table/incident/{incident_sys_id}'
Invoke-RestMethod -Uri $uri -Credential $cred

通过更改 URL 的 table 部分,系统中的任何 table 都可以获得类似的结果。

我明白了。我最终需要按数字而不是 sys_id 提取,因为通过 INC 数字更容易获得最终用户输入。正因为如此,我需要做一些不同的事情。

$SNowUser = “YOURuserid”
$SNowPass = ConvertTo-SecureString –String “yourpasswordgoeshere”  –AsPlainText -Force
$SNowCreds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $SNowUser, $SNowPass
$headers = @{
Accept = “application/json”
}
$body = @{   #Create body of the POST request
number='INC1234567'
}
$bodyJson = $body | ConvertTo-Json
$go = Invoke-RestMethod -Credential $SNowCreds -Headers $headers -Method GET -Uri “https://yourenvironment.service-now.com/api/now/table/incident” -Body $body -ContentType “application/json”

对于像我这样的新手,我认为这可能会有所帮助。这就是导致我回答的原因:

当输入为 GET 请求且正文为 IDictionary(通常为散列 table)时,正文将作为查询参数添加到 URI。对于其他请求类型(如POST),body设置为标准name=value格式的请求body的值

希望这对某人有所帮助!