Introduction
The REST API allows users to query the iQSonar results directly using the web client protocol. The results are returned in JSON format. The long-term plan is that Datahub will be deprecated and eventually retired, and development of third party applications which rely on data from iQSonar will use the RestAPI.
...
Code Block |
---|
language | powershell |
---|
title | Process each device |
---|
|
$count = 1;
$uri_a = -join('http://', $apihost, '/api/v1/devices/?offset=')
$uri_b = '&fetch_size=1'
# while ( $count -le $deviceCount )
while ( $count -le 30 )
{
# Process each device
# Build the URL
$url = -join ($uri_a, $count, $uri_b )
$dev = Invoke-RestMethod $url -Credential $credential
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "DeviceID" -Value $dev.device_id
$row | Add-Member -MemberType NoteProperty -Name "LastSeenDate" -Value $dev.last_scan
$url2 = $dev.self
$device = Invoke-RestMethod $url2 -Credential $credential
# Product (i.e. how did we scan it?)
$row | Add-Member -MemberType NoteProperty -Name "Product" -Value $device.operating_system.product.name
#
# Do more work
#
$count = $count + 1;
$csv += $row
# display progress
if ( $count % 10 -eq 0) {
write-host "." -nonewline
}
} |
...
Code Block |
---|
language | powershell |
---|
title | Completed Script |
---|
collapse | true |
---|
|
#
# Powershell Script to attempt to replicate the Summary_v1.Device report out of DataHub using Rest API
#
# Define the RestAPI target host and credentials -- Define these to suit your site location
# We use simple authentication, default login/password
$apihost = "iqsonar-host" # UPDATE THIS for your location
$user = "admin" # UPDATE THIS for your location
$pass = "password" # UPDATE THIS for your location
$secpass = ConvertTo-SecureString $pass -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($user,$secpass)
$uribase = -join ( 'http://', $apihost, '/api/v1/devices')
# Get a count of all devices
$uri = -join($uribase, '?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'
#
# We want to produce a CSV file, so let us build the structure
$csv = @()
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "DeviceID" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Product" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DNSHostname" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DeviceIsVirtualized" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "VirtualizationMethod" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "PhysicalDeviceID" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "PhysicalHostname" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemName" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemVersion" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemEdition" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemRelease" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemInstallDate" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "BiosName" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "BiosVersion" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "BiosVendor" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DeviceSerialNumber" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DeviceVendor" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DeviceModel" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "ProcessorCount" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "ProcessorCoresPerProcessor" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "MotherboardProcessorSocketCount" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "TotalMemory" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "OSReportedMemory" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "ScanStatus" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "LastSeenDate" -Value $null
$csv += $row
$count = 1;
$uri_a = -join('http://', $apihost, '/api/v1/devices/?offset=')
$uri_b = '&fetch_size=1'
#
while ( $count -le $deviceCount )
while ( $count -le 30 )
{
# Process each device
# Build the URL
$url = -join ($uri_a, $count, $uri_b )
$dev = Invoke-RestMethod $url -Credential $credential
$row = New-Object System.Object
$row | Add-Member -MemberType NoteProperty -Name "DeviceID" -Value $dev.device_id
$row | Add-Member -MemberType NoteProperty -Name "LastSeenDate" -Value $dev.last_scan
$url2 = $dev.self
$device = Invoke-RestMethod $url2 -Credential $credential
# Product (i.e. how did we scan it?)
$row | Add-Member -MemberType NoteProperty -Name "Product" -Value $device.operating_system.product.name
$row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $dev.host_name
# DNS Hostname. Use FQDN if available, if not use Hostname
if ($device.qualified_name -is [array]) {
$row | Add-Member -MemberType NoteProperty -Name "DNSHostname" -Value $device.qualified_name[0].name
}
else {
$row | Add-Member -MemberType NoteProperty -Name "DNSHostname" -Value $dev.host_name
}
# Device Is Virtual, will contain "true" or "false"
$row | Add-Member -MemberType NoteProperty -Name "DeviceIsVirtualized" -Value $device.is_virtual
if ( $device.is_virtual -eq "true" )
{
# list info about virtualization and host
$row | Add-Member -MemberType NoteProperty -Name "VirtualizationMethod" -Value $device.virtualization_platform
$row | Add-Member -MemberType NoteProperty -Name "PhysicalDeviceID" -Value $device.virtual_host.device_id
$row | Add-Member -MemberType NoteProperty -Name "PhysicalHostname" -Value $device.virtual_host.host_name
}
#
# Device Operating System columns available in UI do not directly correlate with the fields in the DataHub report
# because the DataHub output is normalized - this is not done to the data stored in iQSonar itself, but is left to
# the API clients to normalize according to their own criteria.
#
# Best match with the data is as follows
#
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemName" -Value $device.operating_system.description.name
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemVersion" -Value $device.operating_system.name
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemEdition" -Value $device.operating_system.name
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemRelease" -Value $device.operating_system.build_number
$row | Add-Member -MemberType NoteProperty -Name "OperatingSystemInstallDate" -Value $device.os_install_date
$row | Add-Member -MemberType NoteProperty -Name "BiosName" -Value $device.bios.name
$row | Add-Member -MemberType NoteProperty -Name "BiosVersion" -Value $device.bios.version
$row | Add-Member -MemberType NoteProperty -Name "BiosVendor" -Value $device.bios.manufecturer
$row | Add-Member -MemberType NoteProperty -Name "DeviceSerialNumber" -Value $device.serial_number
$row | Add-Member -MemberType NoteProperty -Name "DeviceVendor" -Value $device.manufacturer
$row | Add-Member -MemberType NoteProperty -Name "DeviceModel" -Value $device.model
$row | Add-Member -MemberType NoteProperty -Name "ProcessorCount" -Value $device.cpu_count
if ($device.cpu -is [array])
{
$row | Add-Member -MemberType NoteProperty -Name "ProcessorCoresPerProcessor" -Value $device.cpu[0].core_count
}
else {
$row | Add-Member -MemberType NoteProperty -Name "ProcessorCoresPerProcessor" -Value "0"
}
$row | Add-Member -MemberType NoteProperty -Name "MotherboardProcessorSocketCount" -Value "n/a"
$row | Add-Member -MemberType NoteProperty -Name "TotalMemory" -Value "n/a"
$row | Add-Member -MemberType NoteProperty -Name "OSReportedMemory" -Value $device.total_memory_mb
$row | Add-Member -MemberType NoteProperty -Name "ScanStatus" -Value "n/a"
$count = $count + 1;
$csv += $row
# display progress
if ( $count % 10 -eq 0) {
write-host "." -nonewline
}
}
$csv | Export-csv output.csv -NoTypeInformation |
...