Versions Compared

Key

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

...

Code Block
languagepowershell
titleSave 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
languagepowershell
titleFull example - Host, CPU and OS details
collapsetrue
#
# 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



Info

Filter by label (Content by label)
showLabelsfalse
max5
spacescom.atlassian.confluence.content.render.xhtml.model.resource.identifiers.SpaceResourceIdentifier@1fb5c7
showSpacefalse
sortmodified
reversetrue
typepage
cqllabel in ("api","rest","powershell","restapi") and type = "page" and space = "CSKB"
labelsRESTAPI REST API PowerShell

...