erichoge
Contributor
Contributor

PowerCLI - Stuck with code - Export data / Convert problem

Hello everyone,

I have a problem with the code.


The following is the problem: @{N="VM Affinity-Host-s";E={Get-VM -Name $vm | Get-VMHost | Get-DrsClusterGroup | Select-Object Member}}


If I execute the code part "Get-VM -Name $vm | Get-VMHost | Get-DrsClusterGroup | Select-Object Member" directly, I get the desired result.
If I execute the code as a whole, I get the following message:
VM Affinity Host-s : @{Member=VMware.VimAutomation.ViCore.Interop.V1.Inventory.VMHostInterop[]}

I just can't get any further here and don't know what to do.
Hope one of you can solve it?!

----------Code start-----------

$vms = Get-VM -Name vm*
$report = foreach($vm in $vms){

$vm | Get-VMHost | Select-Object @{N="Cluster";E={$_.Parent.Name}},

@{N="VMHost";E={get-vmhost -Name $_.Name}},

@{N="Sockets";E={$_.ExtensionData.Hardware.CpuInfo.NumCpuPackages}},

@{N="CPU Cores";E={$_.ExtensionData.Hardware.CpuInfo.NumCpuCores}},

@{N="CPU Threads";E={$_.ExtensionData.Hardware.CpuInfo.NumCpuThreads}},

@{N="VM Name";E={Get-VM -Name $vm}},

@{N="VM Name-Notes";E={Get-VM -Name $vm | Select-Object Notes}},

@{N="VM Affinity-Host-s";E={Get-VM -Name $vm | Get-VMHost | Get-DrsClusterGroup | Select-Object Member}}



} $report | Sort-Object VMHost | Export-csv -Path "C:\Temp\test123.csv" -NoTypeInformation -Encoding UTF8 -Delimiter ';'

--------Code finish ------------

Thanks
Erich

0 Kudos
2 Replies
jburen
Expert
Expert

Hi Eric,

When you run this piece of code (Get-VM -Name $VM | Get-VMHost | Get-DrsClusterGroup) you will see that Member is not a simple string but an array of values. So if you want all members you will have to run them through a foreach... Something like this:  Get-VM -Name $VM | Get-VMHost | Get-DrsClusterGroup | %{$_.Member}.

Now, you will get all the hosts that are a member of that DRS group. If you only want the names you can change the code to this: Get-VM -Name $VM | Get-VMHost | Get-DrsClusterGroup | %{$_.Member.Name}

 

Consider giving Kudos if you think my response helped you in any way.
0 Kudos
scott28tt
VMware Employee
VMware Employee

@erichoge 

Moderator: Moved to PowerCLI Discussions, the {code} area is for SDK matters.

0 Kudos