Skip to content

Instantly share code, notes, and snippets.

@mtcoffee
Created December 27, 2024 14:39
Show Gist options
  • Select an option

  • Save mtcoffee/b43cf995a7dcd05e55d3010bd2900b09 to your computer and use it in GitHub Desktop.

Select an option

Save mtcoffee/b43cf995a7dcd05e55d3010bd2900b09 to your computer and use it in GitHub Desktop.
# Reference: https://www.servicenow.com/docs/csh?topicname=client-credentials.html&version=latest
# Must set property glide.oauth.inbound.client.credential.grant_type.enabled
# Configure user in the oauth_entity.user field and set oauth_entity.default_grant_type to client_credential
$clientId = "MyClientID"
$clientSecret = "MySecret"
$serviceNowInstance = "https://dev1234567.service-now.com"
$apiQuery = "/api/now/table/incident?sysparm_query=active=true"
# Get count of active incidents
# Prepare the request body for token request
$requestBody = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
}
# Obtain OAuth token
$token = Invoke-RestMethod -Uri "$serviceNowInstance/oauth_token.do" -Method Post -Body $requestBody -ContentType "application/x-www-form-urlencoded"
$token
# Get a valid token from the response
$validToken = $token.access_token
# Prepare the headers for querying incidents
$headers = @{
Authorization = "Bearer $validToken"
Accept = "application/json"
}
# Query active incidents
$response = Invoke-RestMethod -Uri "$serviceNowInstance$apiQuery" -Method Get -Headers $headers
$incCount = $response.result.count
Write-Output "Incident Count $incCount"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment