获取ServiceNow记录Powershell-超过250 [英] Get ServiceNow Records Powershell - More than 250

查看:169
本文介绍了获取ServiceNow记录Powershell-超过250的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Powershell cmdlet Invoke-RestMethod从ServiceNow检索250多个记录.
有可以使用的Powershell脚本吗?

I am trying to retrieve more than 250 records from ServiceNow using Powershell cmdlet Invoke-RestMethod.
Is there a powershell script that I can use ?

推荐答案

旧话题,但也许这个答案还是有帮助的.

Old topic but perhaps this answer can still be helpful.

我发现,如果我请求的结果超过一定数量,似乎什么也不会返回.这是对我有用的方法(将300更改为您想要的任何数字,并删除&"号后的所有条件):

I found that if I requested more than a certain number of results, seemingly nothing would be returned. Here's what works for me (change 300 to whatever number you want and remove any of the conditions after the ampersand):

$restapiuri = "https://yourserver.service-now.com/api/now/table/incident?sysparm_limit=300&sysparm_query=active=true^ORDERBYDESCnumber"

使用您喜欢的任何方式作为凭据:

Use whatever method you prefer for credentials:

$SNowUser = "account username"
$SNowPass = ConvertTo-SecureString –String "Password" –AsPlainText -Force
$SNowCreds = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $SNowUser, $SNowPass

接下来应该相当熟悉.我们正在构建请求,调用它并将结果分配给变量($ completeticket).如果不添加"| out-string",您可能看不到任何结果.

Next should be fairly familiar. We're building the request, invoking it and assigning the results to a variable ($completeticket). Without adding " | out-string" you may see no results.

我还通过在结果的第一行中找到唯一的文本并将其分配给变量($ separatetickets),然后遍历每个变量($ separateticket),将结果分为多个单独的事件.

I'm also splitting the results into individual incidents by finding unique text in the first line of the results and assigning that to a variable ($separatetickets) and then iterating through each of them ($separateticket).

$i = 0
$headers = Get-HttpBasicHeader $Credentials 
Invoke-RestMethod -uri $restapiuri -Headers $headers -Method GET -ContentType "application/json" |
% {
$completeticket = $_.result | Out-String
$separatetickets = $completeticket -split "whatever the first line of your record is"
foreach ($separateticket in $separatetickets) {
    $i++
    Write-Host
    Write-Host "$i" -ForegroundColor White
    Write-Host "$separateticket" -ForegroundColor Magenta
    }
}

这篇关于获取ServiceNow记录Powershell-超过250的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆