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 | ||||||
---|---|---|---|---|---|---|
| ||||||
# 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 | ||||||
---|---|---|---|---|---|---|
| ||||||
# Deal with clusters -- this gets hairy, finish it tomorrow
if ($thisApplication.product.name -eq "VMware Cluster")
{
# VMWare cluster - will need to generate and follow links to nodes to get device names
}
elseif ( ( $thisApplication.product.name -eq 'Oracle Database Cluster Server' ) -or
( $thisApplication.product.name -eq 'WebSphere AS Cluster') -or
( $thisApplication.product.name -eq 'Windows Server Clustering') -or
( $thisApplication.product.name -eq 'Veritas Cluster Server')
)
{
# Get list of node names from $thisApplication -> devices
$cluster = -join ("Cluster: ", $thisApplication.name, " Nodes: ");
$k=0
while ($k -lt $thisApplication.devices.count)
{
$cluster = -join ($cluster, $thisApplication.devices[$k].host_name)
if ($k -lt $thisApplication.devices.count)
{
# more to do
$cluster = -join ($cluster, "; ")
}
$k = $k + 1
}
} |
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
if ($thisApplication.product.name -eq "VMware Cluster")
{
# VMWare cluster - will need to generate and follow links to nodes to get device names
$cluster = -join ("Cluster: ", $thisApplication.name, " Nodes: ");
$k=0
while ($k -lt $thisApplication.clusters.count)
{
$url2 = = -join ("http://", $sonar, "/api/v1/devices/", $thisApplication.clusters[$k].node_id)
$node = Invoke-RestMethod $url2 -Credential $credential
$cluster = -join($cluster, $node.host_name)
if ($k -lt $thisApplication.clusters.count)
{
# more to do
$cluster = -join ($cluster, "; ")
}
$k = $k + 1
}
} |
...