Get OS Version using API in service-now

44 views Asked by At

I'm able to get server CLASS i.e [windows, linux etc] using Service Now API call as shown below marked with GREEN arrow.

I wish to retrive OS Version 7.9 as well from service now marked in RED arrow which I m unable to.

enter image description here

Powershell:

$osclass=C:\Temp\check_details.ps1 -SNuser snowuser -SNpass mysnowpass -ServerName MYSERVER1
Write-Host $osclass

C:\Temp\check_details.ps1

[CmdletBinding(DefaultParameterSetName = 'None')]
param
(
    [String] [Parameter(Mandatory = $true)]
    $SNuser,

    [String] [Parameter(Mandatory = $true)]
    $SNpass,
    
    [String] [Parameter(Mandatory = $true)]
    $ServerName,
    [String] [Parameter(Mandatory = $false)]
    $retobj = "sys_class_name"
)
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $SNuser, $SNpass)))
# Set proper headers
$HeaderAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $SNuser, $SNpass)))
$SNOWSessionHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$SNOWSessionHeader.Add('Authorization', ('Basic {0}' -f $HeaderAuth))
$SNOWSessionHeader.Add('Accept', 'application/json')
$Type = "application/json"

$srv = [uri]::EscapeDataString($ServerName)
$cmdburl = "https://mybank.service-now.com/api/now/table/cmdb_ci?sysparm_query=name=" + $srv + "&subcategory=Computer"

$surl = Invoke-RestMethod -Method GET -Uri $cmdburl  -TimeoutSec 100 -Headers $SNOWSessionHeader -ContentType "application/json"

foreach ($result in $surl.result) {
    Write-Host "SNOW-OUTPUTYY:"
    Write-Host $result
}

$cmdburl = "https://mybank.service-now.com/api/now/table/cmdb_ci?sysparm_query=name=" + $srv + "&subcategory=Computer&sysparm_fields=os_version"

Write-Host $cmdburl 

$response = Invoke-RestMethod -Method GET -Uri $cmdburl  -TimeoutSec 100 -Headers $SNOWSessionHeader -ContentType "application/json"

# Check if the request was successful
if ($response.result) {
    # Iterate over the results and extract the OS version
    foreach ($result in $response.result) {
        $osVersion = $result.os_version
        Write-Host "SNOW-OUTPUTX:"
        Write-Host $osVersion
    }
} else {
    Write-Host "Error: Failed to retrieve the OS version from the CMDB API."
}

$sretval = $surl.result.$retobj
return $sretval

Output:

PS C:\Users\meuser> $osclass=C:\Temp\check_details.ps1 -SNuser snowuser -SNpass mysnowpass -ServerName MYSERVER1
Write-Host $osclass
SNOW-OUTPUTYY:
@{attested_date=; skip_sync=false; operational_status=1; sys_updated_on=2024-03-14 07:41:31; attestation_score=; discovery_source=ServiceNow; first_discovered=2022-03-22 16:22:17; sys_updated_by=MIDServer.ASH.PROD; due_in=; sys_created_on=2022-03-22 16:22:17; sys_domain=; install_date=; gl_account=; invoice_number=; sys_created_by=MIDServer.User4; warranty_expiration=; u_business_function=; asset_tag=; fqdn=MYSERVER1.mgd.mrshmc.com; change_control=; owned_by=; checked_out=; sys_domain_path=/; 
business_unit=; u_escalation_support_group=; delivery_date=; maintenance_schedule=; install_status=1; cost_center=; attested_by=; supported_by=; dns_domain=mrshmc.com; name=MYSERVER1; assigned=; life_cycle_stage=; purchase_date=2019-04-26; subcategory=Computer; short_description=Web Server; assignment_group=; managed_by=; managed_by_group=; can_print=false; last_discovered=2024-03-14 07:41:31; sys_class_name=cmdb_ci_win_server; manufacturer=; sys_id=c00467039712c110a64cd124a253af71; po_number=; checked_in=; sys_class_path=/!!/!2/!(/!!/!#; life_cycle_stage_status=; mac_address=; vendor=; company=; justification=; model_number=; department=; x_nexsa_imc_nxt_last_seen=; assigned_to=; start_date=; x_nexsa_imc_nxt_platform=; comments=Pentaho; 
cost=; attestation_status=Not Yet Reviewed; sys_mod_count=100; u_assignment_group=; monitor=false; serial_number=7B5424C3-0099-4ACF-B4F9-4E75FD28E0BC; ip_address=10.14.28.78; model_id=; duplicate_of=; sys_tags=; cost_cc=USD; order_date=; schedule=; support_group=; environment=Test; due=; attested=false; correlation_id=0005fd9f-a494-ba27-32fa-b83fd26c27ee::7b5424c3-0099-4acf-b4f9-4e75fd28e0bc; unverified=false; attributes=; location=; asset=; category=Hardware; fault_count=0; lease_id=}
https://mybank.service-now.com/api/now/table/cmdb_ci?sysparm_query=name=MYSERVER1&subcategory=Computer&sysparm_fields=os_version
SNOW-OUTPUTX:

cmdb_ci_win_server

As you can see i was wanting to see OS version 7.9 in the output SNOW-OUTPUTX: which does not print.

Can you please help?

0

There are 0 answers