- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The Export-Csv cmdlet looks at the 1st row if data to determine which columns will appear in the CSV.
So if you rows are not the same, the CSV file only contain part of the data.
You could repeat the information on each row, thus making sure each row has the same columns.
foreach ($dc in Get-Datacenter) {
$dcVm = Get-View -ViewType VirtualMachine -Property Name -SearchRoot $dc.ExtensionData.MoRef
foreach ($cluster in Get-Cluster -Location $dc){
$vms = Get-view -ViewType VirtualMachine -SearchRoot $cluster.ExtensionData.MoRef
foreach ($vm in $vms){
$info = "" | select Datacenter, Name, ToolsStatus, NumCpu, MemoryMB, guestos, IPAddress,
Datastore, DatastoreUsedGB,NrVMperDC,NrVMperCluster
$info.IPAddress = ($vm.Guest.net.IPAddress | where{$_} | Sort-Object -Unique) -join '|'
$info.Datastore = (Get-View -Id $vm.Datastore -Property Name).Name -join '|'
$info.DatastoreUsedGB = [math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)
$info.datacenter = $dc.name
$info.Name = $vm.name
$info.toolsstatus = $vm.guest.toolsstatus
$info.NumCpu = $vm.Summary.config.NumCpu
$info.MemoryMB = $vm.Summary.config.memorySizeMB
$info.guestos = $vm.guest.guestfullname
$info.NrVMperDC = $dcVm.Count
$info.NrVmperCluster = $vms.Count
$report += $info
}
}
}
$report | export-csv "\Report.csv" -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference