Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

The REST API allows users to query the iQSonar results directly using the web client protocol. The results are returned in JSON format.

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.

Pre-requisites

  • You need to know the URL for you iQSonar install - in this example our host is vm-mike-2k12a
  • You need credentials for the iQSonar install - in this example we use the default login admin / password
  • This should work on PowerShell v3 or later

Step One - connect to the host

Our server is using HTTP Authentication not Windows Authentication, so we need to build a plain-text credentials object.

The http header response contains a field "X-fetch-count" which tells us how many devices are available. We will use the Invoke-WebRequest cmdlet to get access to these headers.

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.com/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'

Step Two - get each device record one by one, 

By default the REST API "/api/v1/devices" page will return 200 devices at a time. We can increase or decrease this using the "fetch_size" parameter. The PowerShell invoke-restmethod cmdlet will automatically parse the JSON result into a PowerShell object for us. We will grab the details for one host at a time so as not to use too much memory, and also to make the JSON object easier (no need for a nested loop).

There are two fields in the result we want, the first is "host_name", and the second is "self" which contains a link to the full details of the device.
We enclose the output of each field in quotes in case the data in the field from the server contains a comma. (Some CPU names do contain commas)

We will use some PowerShell built in features to build the CSV file for us and save it to output.csv.


For each device, get hostname, ram, cpu
$count = 0
$uri_a = 'http://vm-mike-2k12a.iqdomain.com/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
}

Step Three - save the output

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

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://vm-mike-2k12a.iqdomain.com/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://vm-mike-2k12a.iqdomain.com/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





  • No labels