VMware Cloud Community
BobAlbion
Contributor
Contributor
Jump to solution

export-csv issue

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
BobAlbion
Contributor
Contributor
Jump to solution

Thanks! thats perfect

Reply
0 Kudos