0

How can I export List of all VMs ( VMs network VLANs id ) in Cluster from Vcenter ? I don't see there any network details option.

Using Vcenter 6.5

enter image description here

I tried this option. I only replaced Get-VirtualPortGroup to Get-VDPortgroup, but not works for me, got some erros

$Portgroups = Get-VDPortgroup |Sort-Object -Unique
$Datastores = Get-Datastore |Sort-Object -Unique
$VMs = get-vm 
[System.Collections.ArrayList]$Output = @()
foreach ($currentVM in $VMs) {
    $VM = New-Object -TypeName PsObject
    $VM | Add-Member -Membertype NoteProperty -Name "Name" -Value $currentVM.Name
    foreach ($Portgroup in $Portgroups) {
        $VM | Add-Member -MemberType NoteProperty -Name $Portgroup.Name -Value (($currentVM|Get-VDPortgroup).name -contains $Portgroup.Name)
    }
    foreach ($Datastore in $Datastores) {
        $VM | Add-Member -MemberType NoteProperty -Name $Datastore.Name -Value (($currentVM|Get-Datastore).name -contains $Datastore.Name)
    }
    $Output += $VM
}
$Output |Export-Csv -Path "vm_export.csv"

error:

Get-VDPortgroup : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:9 char:93
+ ... rty -Name $Portgroup.Name -Value (($currentVM|Get-VDPortgroup).name - ...
+                                                   ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (test-vm:PSObject) [Get-VDPortgroup], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.Vds.Commands.Cmdlets.GetVDPortgroup
andrew
  • 209
  • 2
  • 9

2 Answers2

1

You can get every information you need via PowerCLI.

Get-Cluster cluster |
    Get-vm |Get-NetworkAdapter |
    Select Parent,NetworkName,@{name='VlanID';e={(Get-VirtualPortgroup -Name $_.NetworkName |select -First 1).vlanid}} |
    Export-Csv -Path "vm_export.csv"

Getting all portgroups and datastores as separate columns makes it a little more complicated. This works for me:

$Portgroups = Get-VirtualPortGroup |Sort-Object -Unique
$Datastores = Get-Datastore |Sort-Object -Unique
$VMs = get-vm
[System.Collections.ArrayList]$Output = @()
foreach ($currentVM in $VMs) {
    $VM = New-Object -TypeName PsObject
    $VM | Add-Member -Membertype NoteProperty -Name "Name" -Value $currentVM.Name
    foreach ($Portgroup in $Portgroups) {
        $VM | Add-Member -MemberType NoteProperty -Name $Portgroup.Name -Value (($currentVM|Get-VirtualPortGroup).name -contains $Portgroup.Name)
    }
    foreach ($Datastore in $Datastores) {
        $VM | Add-Member -MemberType NoteProperty -Name $Datastore.Name -Value (($currentVM|Get-Datastore).name -contains $Datastore.Name)
    }
    $Output += $VM
}
$Output |Export-Csv -Path "vm_export.csv"
Gerald Schneider
  • 23,274
  • 8
  • 57
  • 89
  • Thanks, and what if VM has two NICs, how will be then ? Or how to add also used datastores ? – andrew Oct 11 '22 at 23:47
  • In my example you will get a line for every NIC. A VM with three NICs will show up three times. For datastores you can replace Get-NetworkAdapter with Get-Datastore (and use the appropiate properties). To get both in the same line it gets more complicated. – Gerald Schneider Oct 12 '22 at 05:07
  • Aah, you have right, is showing per line. is possible to show it per column ? NIC1, NIC2 Then how please also add e.g. datastores ? so then I can create my own export. Thanks – andrew Oct 12 '22 at 08:26
  • I added an extended example that produces a matrix that has columns for all portgroups and datastores and shows which are connected to wich VM. – Gerald Schneider Oct 12 '22 at 09:21
  • thanks Gerald, btw do you know why is VM names truncated to 18 characters from basic get-vm command ? how to get full name ? get-vm> This-is-my-test-ma... PoweredOn 2 4.000 – andrew Oct 12 '22 at 11:39
  • You should ask separate questions for this. – Gerald Schneider Oct 12 '22 at 11:44
1

The easy way is to use rvtool. Just install it on your computer and save an excel report. Then you can filter as you want.