Hello!
I am trying to export-csv the following code, each obvious answer seems to fail or show the too much data, can anyone help?
foreach ($ClusterName in Get-Cluster) {
foreach ($VMHost In Get-VMHost -Location $ClusterName | where {$_.Name -match "rxh*"} | Sort Name ){
write-host $VMHost.Name, $ClusterName, $VMHost.CPUTotalMhz, $VMHost.MemoryTotalMB}}
Thanks
Steve
Try it like this
$report = @() foreach ($ClusterName in Get-Cluster) { foreach ($VMHost in (Get-VMHost -Location $ClusterName | where {$_.Name -match "rxh*"} | Sort Name)){ $row = "" | Select Name,ClusterName,CPUTotalMhz,MemoryTotalMB $row.Name = $VMHost.Name $row.ClusterName = $ClusterName.Name $row.CPUTotalMhz = $VMHost.CPUTotalMhz $row.MemoryTotalMB = $VMHost.MemoryTotalMB $report += $row } } $report | Export-Csv "C:\report.csv" -NoTypeInformation
You have to remember that Write-Host sends the data to the console, while the Export-Csv cmdlet picks up what is in the pipeline (when used like this)
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try it like this
$report = @() foreach ($ClusterName in Get-Cluster) { foreach ($VMHost in (Get-VMHost -Location $ClusterName | where {$_.Name -match "rxh*"} | Sort Name)){ $row = "" | Select Name,ClusterName,CPUTotalMhz,MemoryTotalMB $row.Name = $VMHost.Name $row.ClusterName = $ClusterName.Name $row.CPUTotalMhz = $VMHost.CPUTotalMhz $row.MemoryTotalMB = $VMHost.MemoryTotalMB $report += $row } } $report | Export-Csv "C:\report.csv" -NoTypeInformation
You have to remember that Write-Host sends the data to the console, while the Export-Csv cmdlet picks up what is in the pipeline (when used like this)
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks! thats perfect