Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This is the second in a series of articles giving extended example of how to use the RestAPI. This article will demonstrate how to generate a report equivalent to the Version 3 "OutputApplications" view using PowerShell. For a shorter example that returns a simpler dataset, you can see the article Use RestAPI and PHP to generate a list of Applications.

...

Build the CSV file header row

PowerShell has a number of built in libraries for handling output to various file formats. For this example we will be saving the results in a CSV file that can be viewed directly in EXCEL or imported into other databases.


Code Block
languagepowershell
themeMidnight
firstlineOutputApplications fields required
# Build the CSV File header row
$csv = @()
$row = New-Object System.Object

$row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "FQDN" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "SoftwareName" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "SoftwareVersion" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "SoftwareVendor" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "SoftwareEdition" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "InstanceIdentifier" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "ClusterInformation" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "UserCount" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "LastScanDate" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Location" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Language" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "ApplicationID" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DeviceID" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DNSHostname" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "DNSFQDN" -Value $null
$row | Add-Member -MemberType NoteProperty -Name "Evidence" -Value $null

...

Code Block
languagepowershell
themeMidnight
titleInner and Outer loops
$offset = 1				# offset
$seen   = 1;			# first offset is 1, not 0
while ( $seen -lt $appCount)
{
	$url = -join ("http://", $sonar, "/api/v1/applications/?offset=", $offset,  "&fetch_size=", $fs)
	$applications = Invoke-RestMethod $url -Credential $credential
	$i = 1
	while ($i -lt $applications.count)
	{
		# Process $applications[$i]
		
		$csv += $row
		$i = $i + 1;		# keep track for inner loop
		$seen = $seen + 1;  # keep track for outer loop
		if ( $seen % 10 -eq 0) {
			# progress indicator - display a "." every 10 devices
			write-host "." -nonewline
		}
	}
	# Finished this batch
	$offset = $seen
}
write-host " Done. Saving output to OutputApplications.csv now."
$csv | Export-csv OutputApplications.csv -NoTypeInformation		# We will further refine this line later

Process each application

Code Block
languagepowershell
themeMidnight
titleInner loop - process the application
#
# Incomplete Code
#
		# Process the $applications[$i]
		$thisApplication = $applications[$i]
		$currDevice = Invoke-RestMethod $thisApplication.self -Credential $credential
		
		$row = New-Object System.Object
		
		# Hostname, FQDN and DeviceID are defined by the devices subsection if present. 
		
		if ( $thisApplication.devices.count -eq 0)
		{
			# If we have no devices, we do not have this info
			$row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $null
			$row | Add-Member -MemberType NoteProperty -Name "FQDN" -Value $null
			$row | Add-Member -MemberType NoteProperty -Name "DeviceID" -Value $null
			$row | Add-Member -MemberType NoteProperty -Name "DNSHostname" -Value $null
			$row | Add-Member -MemberType NoteProperty -Name "DNSFQDN" -Value $null
		}
		else
		{
			# one or more devices. So we will list the details for devices[0] - other devices will get listed in the cluster info section
			$row | Add-Member -MemberType NoteProperty -Name "Hostname" -Value $thisApplication.devices[0].host_name			
			$row | Add-Member -MemberType NoteProperty -Name "DeviceID" -Value $thisApplication.devices[0].device_id
			$row | Add-Member -MemberType NoteProperty -Name "DNSHostname" -Value $thisApplication.devices[0].host_name
			$k=0
			while ($k -lt $thisApplication.devices[0].qualified_name.count)
			{
				if ($thisApplication.devices[0].qualified_name[$k].name_type -eq "DNSFQDN")
				{
					$row | Add-Member -MemberType NoteProperty -Name "FQDN" -Value $null
					$row | Add-Member -MemberType NoteProperty -Name "DNSFQDN" -Value $null
				}
				$k = $k + 1
			}
		}
		if ()
		# Device name was complicated. Software details are more simple
		$row | Add-Member -MemberType NoteProperty -Name "SoftwareName" -Value $thisApplication.product.name
		$row | Add-Member -MemberType NoteProperty -Name "SoftwareVersion" -Value $thisApplication.version
		$row | Add-Member -MemberType NoteProperty -Name "SoftwareVendor" -Value $thisApplication.product.vendor
		$row | Add-Member -MemberType NoteProperty -Name "SoftwareEdition" -Value $thisApplication.edition
		$row | Add-Member -MemberType NoteProperty -Name "InstanceIdentifier" -Value $thisApplication.name
		# UserCount is optional
		if ( (!test-var variable:\$currDevice.users) )
		{
			$row | Add-Member -MemberType NoteProperty -Name "UserCount" -Value $currDevice.users.count
		}
		$row | Add-Member -MemberType NoteProperty -Name "LastScanDate" -Value $thisApplication.last_scan
		$row | Add-Member -MemberType NoteProperty -Name "ApplicationID" -Value $thisApplication.application_id
		
		# Deal with clusters -- this gets hairy, finish it tomorrow