问题
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 ?
回答1:
Old topic but perhaps this answer can still be helpful.
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
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.
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
}
}
来源:https://stackoverflow.com/questions/25729997/get-servicenow-records-powershell-more-than-250