servicenow

Tip: Create Scripted REST API in servicenow and consume it in PowerShell

In this blog post let me walk through the simple steps to create a “Scripted REST API” which simply prints the hardcoded values. This is a beginners guide so let’s keep things easier! Navigate here to get some more information.

{
    "Name": "Chendrayan Venkatesan (Chen V)",
    "Role": "ITPro",
    "Department": "Information Technology",
    "Skills": [
        "PowerShell",
        "REST",
        "servicenow",
        "Python",
        "SharePoint",
        "Office 365"
    ]
}

Step 1: Login to servicenow

Step 2: Search for Scripted REST API – in Filter Navigator.

Step 3: Click New

Step 4: Name your API (In our case it’s aboutme). All other fields remains same.

Step 5: Scroll down to add resources. Click New

Step 6: Name as you wish (aboutme Demo)

That’s it! Check out the skeleton code

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here

})(request, response);

Copy paste the code in resource area.

    return {
        "Name": "Chendrayan Venkatesan (Chen V)",
        "Role": "ITPro",
        "Department": "Information Technology",
        "Skills": [
            "PowerShell",
            "REST",
            "servicenow",
            "Python",
            "SharePoint",
            "Office 365"
        ]
    };

Submit and open REST API Explorer! TO TEST or PLAY WITH PowerShell!

try {
    $admin = "admin"
    $password = "India890*" | ConvertTo-SecureString -AsPlainText -Force
    $Credential = New-Object pscredential -ArgumentList ($admin , $password)
    $Uri = "https://dev42835.service-now.com/api/253124/aboutme"
    $Result = Invoke-RestMethod -Uri $Uri -Method Get -Credential $Credential
    $Result.result
}
catch {
    $_.Exception
}

Output

Name                           Role  Department             Skills                                   
----                           ----  ----------             ------                                   
Chendrayan Venkatesan (Chen V) ITPro Information Technology {PowerShell, REST, servicenow, Python...}


 

Leave a Reply

Your email address will not be published. Required fields are marked *