Dive into deep insights and technical expertise 😎

Friday, June 20, 2025

Getting Started – PowerShell + ServiceNow Table API with Authentication

 

PowerShell + ServiceNow Integration

🚀 Introduction

ServiceNow’s Table API provides full CRUD access to any record in the platform. Combine that with PowerShell, and you unlock the ability to automate ticketing, compliance tracking, CMDB updates, and more — right from the command line.

In this article, you’ll learn how to:

  • Authenticate using Basic Auth and OAuth2

  • Make your first Table API call from PowerShell

  • Parse and handle JSON responses

  • Set the stage for advanced integration in later posts


🔐 1. Basic Authentication Setup

This is the simplest approach, but not recommended for production.

powershell

# Replace with your instance and credentials $instance = "dev12345" $user = "admin" $pass = "your_password" $base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$user`:$pass")) # Define headers and URL $headers = @{ "Authorization" = "Basic $base64Auth" "Accept" = "application/json" } $url = "https://$instance.service-now.com/api/now/table/incident?sysparm_limit=1" # Call the API $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers # Output the result $response.result

⚠️ Tip: Avoid hardcoding passwords. We’ll cover secure handling in Part 4.


🔑 2. OAuth2 (Recommended for Secure Use)

OAuth2 gives you token-based access, ideal for enterprise environments.

📌 Prerequisites:

  • OAuth enabled in ServiceNow

  • A registered app with Client ID and Client Secret

  • A user account with API access

powershell

# Credentials and endpoint $clientId = "your_client_id" $clientSecret = "your_client_secret" $username = "admin" $password = "your_password" $instance = "dev12345" $tokenUrl = "https://$instance.service-now.com/oauth_token.do" # Build request body $body = @{ grant_type = "password" client_id = $clientId client_secret = $clientSecret username = $username password = $password } # Get token $response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $body -ContentType "application/x-www-form-urlencoded" $accessToken = $response.access_token # Make Table API request $headers = @{ "Authorization" = "Bearer $accessToken" "Accept" = "application/json" } $url = "https://$instance.service-now.com/api/now/table/incident?sysparm_limit=1" $data = Invoke-RestMethod -Uri $url -Method Get -Headers $headers $data.result

🔎 3. What You Should See

A single incident record, structured in JSON:

json

{ "result": [ { "number": "INC0010001", "short_description": "Sample Incident", "state": "1", "sys_id": "abc123..." } ] }

🧭 Conclusion

Connecting PowerShell to ServiceNow via the Table API is a powerful step toward automation. Whether you're managing incidents, risks, or CMDB items, understanding authentication methods is key.

In future articles, we’ll make this integration enterprise-grade with error handling, secure storage, and better performance.

Share:

0 comments:

Post a Comment

InformativeTechnicalContent.com