Adding Objects to ArrayList for CSV Output

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 



This would be my way of doing it. you will need PowerShell verion 5 for [PSCustomObject] and [Ordered] Type.

$clusterNames = get-cluster | Select-Object -Property Name
$report = @()
$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){
    $count++
    $CPU = get-vmhost -Name $vHost.Name | Sort Name | Get-View | Select-Object -property Name, @{N=“CPU“;E={$_.Hardware.CpuPkg[0].Description}}
    $report += [PSCustomObject][Ordered]@{
        ClusterName = $EVCmode.Name
        EVCMode     = $EVCmode.EVCMode
        HostName    = $CPU.Name
        CPU         = $CPU.CPU
        Count       = $Count
    }    
  }
}
$report | Export-Csv -Path "C:\users\someuser\Desktop\EVCMode.csv" -NoTypeInformation

I have been struggling with the exact same issue.

$ConfigFiles = @()
$ConfigFiles = [PSCustomObject][Ordered]@{
ConfigItems = "configuration.xml"
ConfigDestinations = "$env:systemdrive\ProgramData"
}

When I do a get member it returns TypeName: System.Management.Automation.PSCustomObject

When I go to add using the example of Naw’s post I run:

 $ConfigFiles += [pscustomobject][Ordered] @{ConfigItems = "PostureCFG.xml"
ConfigDestinations = "$env:systemdrive\ProgramData\C"}

When I run this I get the following error:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named
‘op_Addition’.
At line:8 char:2

  • $ConfigFiles += [pscustomobject][Ordered] @{ConfigItems = "ISEPostur …
  • CategoryInfo : InvalidOperation: (op_Addition:String) , RuntimeException
  • FullyQualifiedErrorId : MethodNotFound

 

PSVersionTable gets me:
Name Value


PSVersion 5.1.17134.765
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
BuildVersion 10.0.17134.765
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Hi Shawn

I came across similar issue myself. My understanding is like this:
$ConfigFiles = @() #This is an empty array
$ConfigFiles = [PSCustomObject][Ordered]@{} #As soon as you assign this $ConfigFiles is no longer an array but becomes PowerShell Object.
The Array has the ‘addition’ function which is ‘+=’ but the ps object hasn’t got the addition function like the one from the array type. So it throws that error.

Perhaps try remove-variable -Name ConfigFiles before you run the addition below.
$ConfigFiles = @()
$ConfigFiles += [PSCustomObject][Ordered]@{}

Hope this helps.

found the article here.
https://stackoverflow.com/questions/50911366/method-invocation-failed-because-system-management-automation-psobject-doesnt

[quote quote=165289]This would be my way of doing it. you will need PowerShell verion 5 for [PSCustomObject] and [Ordered] Type.

$report += [PSCustomObject][Ordered]@{
ClusterName = $EVCmode.Name
EVCMode = $EVCmode.EVCMode
HostName = $CPU.Name
CPU = $CPU.CPU
Count = $Count

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
[/quote]

Naw, thank you very much! This worked like a charm, buddy! Problem solved.