The REST API allows users to query the iQSonar results directly using the web client protocol. The results are returned in JSON format. This article is part of a series on how to make use of the Rest API.
This worked example uses PowerShell to produce a CSV file containing details of devices directly from the scan results. We look for the host name, the total installed RAM and the CPU type. The device name will be listed in the "device" results. For the CPU Type and the installed RAM we need to then go to the details page for the device.
...
Code Block |
---|
language | powershell |
---|
title | Connect to iQSonar, get number of devices |
---|
|
# We use simple authentication, default login/password
$user = "admin"
$pass = "password"
$secpass = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user,$secpass)
#
# To find out how many devices there are, ask for the first device and look at the header for the device count
#
# offset=1 means start with first device. fetch_size=1 means only return one record.
$uri = 'http://vm-mike-2k12a.iqdomain.comiqsonar-host/api/v1/devices/?offset=1&fetch_size=1'
$r = Invoke-WebRequest $uri -Credential $credential
# $r.headers has HTML headers, $r.content has text content
$deviceCount = $r.headers.'X-fetch-count' |
...
Code Block |
---|
language | powershell |
---|
title | For each device, get hostname, ram, cpu |
---|
|
$count = 01
$uri_a = 'http://vm-mike-2k12a.iqdomain.comiqsonar-host/api/v1/devices/?offset='
$uri_b = '&fetch_size=1'
# This is going to hold the CSV file, let's build the header row
$csv = @()
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "Host" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "RAM" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "CPU Type" -Value $null
while ($count -le $deviceCount)
{
# Build the URL
$count = $count
+ 1
$url = -join ($uri_a, $count, $uri_b )
$dev = Invoke-RestMethod $url -Credential $credential
$row = New-Object System.Object
# Put the hostname in to the row
$row | Add-Member -MemberType NoteProperty -Name "Host" -Value $dev.host_name ;
# Get the link to the rest of the details
$url2 = $dev.self
$device = Invoke-RestMethod $url2 -Credential $credential
$row | Add-Member -MemberType NoteProperty -Name "RAM" -Value device.total_memory_mb
$row | Add-Member -MemberType NoteProperty -Name "CPU Type" -Value $device.cpu.cpu_model
$csv += $row
# display progress
write-host "." -nonewline
$count = $count + 1
} |
Step Three - save the output
Code Block |
---|
language | powershell |
---|
title | Save the output |
---|
|
#
# Now the CSV is finished, let's save it as output.csv
#
$csv | Export-csv output.csv -NoTypeInformation |
Here is a slightly longer example which produces Hostname, Serial No, RAM, cpu details and operating system details
Code Block |
---|
language | powershell |
---|
title | Full example - Host, CPU and OS details |
---|
|
#
# PowerShell Script to get list of computers from iQSonar database using REST API
#
# We use simple authentication, default login/password
$user = "admin"
$pass = "password"
$secpass = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user,$secpass)
#
# To find out how many devices there are, ask for the first device and look at the header for the device count
#
# offset=0 means start with first device. fetch_size=1 means only return one record.
$uri = 'http://iqsonar-host/api/v1/devices/?offset=0&fetch_size=1'
# We use Invoke-WebRequest to get access to the header info. Later we will use Invoke-RestMethod to have the JSON parsed automatically
$r = Invoke-WebRequest $uri -Credential $credential
# $r.headers has HTML headers, $r.content has text content
$deviceCount = $r.headers.'X-fetch-count'
# We want to iterate over the list, and look at operating system :-)
$count = 0
$uri_a = 'http://iqsonar-host/api/v1/devices/?offset='
$uri_b = '&fetch_size=1'
# This is going to hold the CSV file
$csv = @()
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "Host" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Serial No" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "RAM" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "CPU Count" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Core Count" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "CPU Type" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OS Name" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OS Version" -Value $null
$csv += $row
while ($count -le $deviceCount)
# while ($count -le 10)
{
# Build the URL
$count = $count + 1
$url = -join ($uri_a, $count, $uri_b )
$dev = Invoke-RestMethod $url -Credential $credential
$row = New-Object System.Object
# Put the hostname in to the row
$row | Add-Member -MemberType NoteProperty -Name "Host" -Value $dev.host_name ;
# Get the link to the rest of the details
$url2 = $dev.self
$device = Invoke-RestMethod $url2 -Credential $credential
$row | Add-Member -MemberType NoteProperty -Name "RAM" -Value $device.total_memory_mb
$row | Add-Member -MemberType NoteProperty -Name "CPU Count" -Value $device.cpu_count
$row | Add-Member -MemberType NoteProperty -Name "Core Count" -Value $device.core_count
if ( $device.cpu.cpu_model -is [array] )
{
$row | Add-Member -MemberType NoteProperty -Name "CPU Type" -Value $device.cpu[0].cpu_model
}
else
{
$row | Add-Member -MemberType NoteProperty -Name "CPU Type" -Value $device.cpu.cpu_model
}
$row | Add-Member -MemberType NoteProperty -Name "OS Name" -Value $device.operating_system.name
$row | Add-Member -MemberType NoteProperty -Name "OS Version" -Value $device.operating_system.version
$csv += $row
# display progress
write-host "." -nonewline
}
#
# Now the CSV is finished, let's save it as output.csv
#
$csv | Export-csv output.csv -NoTypeInformation |
...