PowerShellservicenow

Tip: Retrieve servicenow CI item values using REST API (Turn on Display Value)

In my last blog post I shared about ‘Scripted REST API’ getDisplayValue() tip. Now, here lets see Out of the Box option. When we query the CI with a query “sysparm_query=name=$($ConfigurationItem)&sysparm_limit=1”

param (
    $ConfigurationItem = 'Car-3'
)

try {
    $Uri = "https://dev42835.service-now.com/api/now/table/cmdb_ci?sysparm_query=name=$($ConfigurationItem)&sysparm_limit=1"
    $admin = "admin"
    $password = "admin" | ConvertTo-SecureString -AsPlainText -Force
    $Credential = New-Object pscredential -ArgumentList ($admin , $password)
    $Result = Invoke-RestMethod -Uri $Uri -Method Get -Credential $Credential
    [pscustomobject]@{
        Name      = $Result.result.name
        ManagedBy = $Result.result.managed_by.value
        Vendor    = $Result.result.vendor.value
    }
}
catch {
    $_.Exception
}

We get the output illustrated below.

Now, add sysparm_display_value=true in the URL and see the results as actual values instead of ID

$Uri = "https://dev42835.service-now.com/api/now/table/cmdb_ci?sysparm_query=name=$($ConfigurationItem)&sysparm_limit=1&sysparm_display_value=true"
$admin = "admin"
$password = "admin" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object pscredential -ArgumentList ($admin , $password)
$Result = Invoke-RestMethod -Uri $Uri -Method Get -Credential $Credential
[pscustomobject]@{
    Name      = $Result.result.name
    ManagedBy = $Result.result.managed_by.display_value
    Vendor    = $Result.result.vendor.display_value
}

After adding the sysparm_display_value=true we see the display_value property! This helps to get readable output. (sysparm_query=name=$($ConfigurationItem)&sysparm_limit=1&sysparm_display_value=true)

Enjoy servicenow and PowerShell!

 

Leave a Reply

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