Hi,
My first time posting. I’m still fairly green at Powershell, and I was wondering if anyone could help.
GOAL: To obtain similar type of output, as below, for the purpose of saving to a CSV file. First row is the header, and the next rows are the actual data for each column header. I might have over 20 clusters, and each cluster might have various hosts. Basically, I can grab the EVC Mode and the Cluster Names in the first loop; but I will have to perform an inner loop on the Cluster Names to grab the actual CPU values for each host within that cluster.
Cluster Name | CPU's | EVC Mode |
Company-Dev-01 | Intel(R) Xeon(R) Gold 6148 | Ivy Bridge |
Company-Dev-02 | Intel abc (Haswell)
Intel efg (Broadwell)
|
Haswell |
PROBLEM: In my code, everything works as expected, except the output. It looks like the way I want, but not the values I want. The objects being added to my $report array list are repetitive host values, before being wiped out at the beginning of the next inner loop’s iteration and replaced by a new round of repetitive host values. For a cluster containing 7 different hosts (host1-host7), I’ll receive repetitive values being written into the array, but only for the last host in that cluster.
Cluster Name EVC Mode Host Name CPU's ------------ -------- --------- ----- Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz Company-Dev-SLS intel-westmere host7.mycompany.com Intel(R) Xeon(R) CPU E5-2695 v2 @ 2.40GHz
Below, is my code. The problems are in lines 19-24. Perhaps I’m not understanding the philosophy being using add-member? Any help would be appreciated. Thank you!
$clusterNames = get-cluster | Select-Object -Property Name $report = New-Object System.Collections.ArrayList $row = New-Object PSObject [int]$count = 0 foreach ($cluster in $clusterNames) { $EVCmode = get-cluster $cluster.Name | Select-Object -Property Name, EVCMode $clusterHosts = get-VMHost -Location $cluster.Name | Select-Object -property Name foreach ($vHost in $clusterHosts) { $CPU = get-vmhost -Name $vHost.Name | Sort Name | Get-View | Select-Object -property Name, @{N=“CPU“;E={$_.Hardware.CpuPkg[0].Description}} $row | Add-Member -MemberType NoteProperty -Name "Cluster Name" -Value $EVCmode.Name -Force $row | Add-Member -MemberType NoteProperty -Name "EVC Mode" -Value $EVCmode.EVCMode -Force $row | Add-Member -MemberType NoteProperty -Name "Host Name" -Value $CPU.Name -Force $row | Add-Member -MemberType NoteProperty -Name "CPU's" -Value $CPU.CPU -Force $report.Insert($count,$row) $count++ } } $report | Export-Csv -Path "C:\users\someuser\Desktop\EVCMode.csv" -Delimiter ',' -NoTypeInformation