I don't know if this is the "best" way, but I've been writing sometehing to export some information based on this tool:
http://www.wooditwork.com/2010/08/16/exporting-all-that-useful-vm-information-with-powercli
So the basic idea in terms of the data structures is:
1) Create an empty report (array)
2) Create an table ( $Summary = {} | Select Thing1, Thing2) and populate it for each machine
3) $report += $Summary at the end of each iteration
4) Build your CSV off of $Report
Here's my example if it's any help; I'm exporting different information than you, but same idea (and feedback welcome if I'm doing something dumb). The idea of having an XML version is that it lets you handle nested data structures better for information for which there can be >1, e.g., network adapters, hard drives, etc.
Param(
[Parameter(Mandatory=$true)]
[ValidateSet("XML","CSV")]
[String]
$OutputFormat
)
# Limit to just "Foo" datacenter for now
$VMS = Get-VM -Location "Foo"
# Create an empty report array
$Report = @()
ForEach ($VM in $VMS) {
$VMView = $VM | Get-View
# Construct an object to stuff
$Summary = {} | Select Name, Parent, OS, Powerstate, Memory, `
ToolsVersion, ToolsStatus, HardwareVersion, VMConfigPath, `
VMHDPaths, NetworkInfo
# Then stuff in a bunch of facts
$Summary.Name = $VM.name
$Summary.Parent = $VM.VMHost.name
$Summary.OS = $VMView.Summary.Config.guestFullName
$Summary.Powerstate = [String] $VM.Powerstate
$Summary.Memory = [Math]::Round(($VM.MemoryMB),2)
$Summary.ToolsVersion = [Int] $VMView.Config.Tools.ToolsVersion
$Summary.ToolsStatus = [String] $VMView.Guest.ToolsStatus
$Summary.HardwareVersion = $VMView.Config.Version
$Summary.VMConfigPath = $VMView.Config.Files.VMPathName
# stuffing params that can have multiple values into a CSV is kind of ugly
$HDinfo = @()
ForEach ($hd in $hds) {
$detail = $VM | Get-HardDisk -Name $hd.Name
$HDTmp = @{}
$HDTmp.HDName = $detail.Name
$HDTmp.File = $detail.Filename
if ($OutputFormat -eq "XML") {
$HDinfo += $HDTmp
} else {
$HDinfo += [String]::join(":", $HDTmp.Values)
}
}
if ($OutputFormat -eq "XML") {
$Summary.VMHDPaths = $HDinfo
} else {
$Summary.VMHDPaths = [String]::join("|", $HDinfo)
}
$adapters = $VM | Get-NetworkAdapter
$Netinfo = @()
ForEach ($adapter in $adapters) {
$detail = $VM | Get-NetworkAdapter -Name $adapter.Name
# Pack into a data structure
$NITmp = @{}
$NITmp.AdapterName = $adapter.Name
$NITmp.NetworkName = $detail.NetworkName
$NITmp.MacAddress = $detail.MacAddress
if ($OutputFormat -eq "XML") {
$Netinfo += $NITmp
} else {
$Netinfo += [String]::join("|", $NITmp.Values)
}
}
if ($OutputFormat -eq "XML") {
$Summary.NetworkInfo = $Netinfo
} else {
$Summary.NetworkInfo = [String]::join("^", $Netinfo)
}
$Report += $Summary
}
if ($OutputFormat -eq "XML") {
$Report | Export-Clixml vm_info.xml
} elseif ($OutputFormat -eq "CSV") {
$Report | Export-CSV -NoTypeInformation vm_info.csv
}
Message was edited by: will47: formatting
Message was edited by: will47 add additional code block, formatting.