Combine 2 collections into one for CSV file export

I’m starting with this code:

Get-UcsServer | Get-UcsFabricLocale | Get-UcsFabricPath | Get-UcsDcxVc | Select dn,switchid,operstate,adminstate,vnic,linkstate | Where { $_.vnic } | Sort dn | ft

It does the job nicely, however there are properties in UCSServer, UCSFabricLocale, UCSFabrixPath and UCSDcxVc which I would like to add to the output and then export the results to CSV.

I expanded the code to this behemoth:


$AllUCSPaths = @()
$AllUCSServers = Get-UcsServer
ForEach ($UCSServer in $AllUCSServers)
    {
    $UCSFabricLocales = $UCSServer | Get-UcsFabricLocale
    $Item1 = New-Object PSObject
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_AdminState -Value $UCSServer.AdminState
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_AssignToDN -Value $UCSServer.AssignedToDN
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Associateion -Value $UCSServer.Association
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Availability -Value $UCSServer.Availability
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_ChassisID -Value $UCSServer.ChassisID
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Discovery -Value $UCSServer.Discovery
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_DiscoverStatus -Value $UCSServer.DiscoverStatus
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Model -Value $UCSServer.Model
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Name -Value $UCSServer.Name
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_NumOfAdapters -Value $UCSServer.NumOfAdapters
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_OperPower -Value $UCSServer.OperPower
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_OperState -Value $UCSServer.OperState
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Operability -Value $UCSServer.Operability
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Serial -Value $UCSServer.Serial
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_ServerID -Value $UCSServer.ServerID
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_SLotID -Value $UCSServer.SLotID
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_UsrLbl -Value $UCSServer.UsrLbl
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_Vendor -Value $UCSServer.Vendor
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_DN -Value $UCSServer.DN
    $Item1 | Add-Member -type NoteProperty -Name UCSServer_RN -Value $UCSServer.RN
    ForEach ($UCSFabricLocale in $UCSFabricLocales)
        {
        $AllUCSFabricPaths = $UCSFabricLocale | Get-UcsFabricPath
        ForEach ($UCSFabricPath in $AllUCSFabricPaths)
            {
            $AllUCSDcxVc = $UCSFabricPath | Get-UcsDcxVc
            ForEach ($UCSDcxVc in $AllUCSDcxVc)
                {
                $Item = New-Object PSObject
                
                Write-Host $UCSDcxVc.DN
                Write-Host $UCSDcxVc.SwitchID
                Write-Host $UCSDcxVc.OperState
                Write-Host $UCSDcxVc.AdminState
                Write-Host $UCSDcxVc.vNIC
                Write-Host $UCSDcxVc.LinkState
                $Item | Add-Member -type NoteProperty -Name DCxVc_DN -Value $UCSDcxVc.DN
                $Item | Add-Member -type NoteProperty -Name DCxVc_SwitchID -Value $UCSDcxVc.SwitchID
                $Item | Add-Member -type NoteProperty -Name DCxVc_OperState -Value $UCSDcxVc.OperState
                $Item | Add-Member -type NoteProperty -Name DCxVc_AdminState -Value $UCSDcxVc.AdminState
                $Item | Add-Member -type NoteProperty -Name DCxVc_vNIC -Value $UCSDcxVc.vNIC
                $Item | Add-Member -type NoteProperty -Name DCxVc_LinkState -Value $UCSDcxVc.LinkState
                $AllUCSPaths += $Item + $Item1
                
                }

            }
        }



    }

$AllUCSPaths | Export-CSV -Path "AllUCSServerPaths.csv" -NoTypeInformation -UseCulture

The aim is to create a new “row” for each UcsDcxV object property so it can be manipulated in XL. However I’m having problems combining the 2 collections into one at the line “$AllUCSPaths += $Item + $Item1” with the error:

Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named ‘op_Addition’.
At C:\Users\jmilano\Documents\PowerShell_Scripts\UCS\Get-AllServerNICStatus.ps1:50 char:17

  •             $AllUCSPaths += $Item + $Item1
    
  •             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (op_Addition:String) , RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

Can anyone suggest a good way to fix this?

The problem is this: $Item + $Item1

If you want $Item1 to be a property of $Item then you need something like this:

$Item | Add-Member -MemberType NoteProperty -Name 'Item1' -Value $Item1

You might want to look at the [PSCustomObject] type accelerator for object creation, not least for readability:

    $Item1 = [PSCustomObject] @{
        UCSServer_AdminState     = $UCSServer.AdminState
        UCSServer_AssignToDN     = $UCSServer.AssignedToDN
        UCSServer_Associateion   = $UCSServer.Association
        UCSServer_Availability   = $UCSServer.Availability
        UCSServer_ChassisID      = $UCSServer.ChassisID
        UCSServer_Discovery      = $UCSServer.Discovery
        UCSServer_DiscoverStatus = $UCSServer.DiscoverStatus
        UCSServer_Model          = $UCSServer.Model
        UCSServer_Name           = $UCSServer.Name
        UCSServer_NumOfAdapters  = $UCSServer.NumOfAdapters
        UCSServer_OperPower      = $UCSServer.OperPower
        UCSServer_OperState      = $UCSServer.OperState
        UCSServer_Operability    = $UCSServer.Operability
        UCSServer_Serial         = $UCSServer.Serial
        UCSServer_ServerID       = $UCSServer.ServerID
        UCSServer_SLotID         = $UCSServer.SLotID
        UCSServer_UsrLbl         = $UCSServer.UsrLbl
        UCSServer_Vendor         = $UCSServer.Vendor
        UCSServer_DN             = $UCSServer.DN
        UCSServer_RN             = $UCSServer.RN
    }

+= for adding elements to an array is considered a bad practice, and can cause performance problems with large collections.

This would be better:

$AllUCSPaths = ForEach ($UCSServer in $AllUCSServers) { ... }
1 Like