rxjoseph
Enthusiast
Enthusiast

VM Portgroup and VLAN details

Jump to solution

Hi LucD

 

I have a requirement where I  need to get a report with the following details

Cluster name, vCenter, VMname, Portgroup and VLAN id

The below scripts works but if the VM has two Network Adaptors then i dont get the VLAN id for the 2nd adapter 

Script

##################################################################

$Report =@()

$Report +=Get-cluster  -PipelineVariable cluster |Get-VMHost |`
get-vm | where {$_.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*"} |`

Select-Object @{N="vCenter"; E={$_.Uid.Split(':@')[1]}},

@{ N= 'Cluster' ; E={$cluster.Name}},

@{ N= 'VMName' ; E={$_.name}},

# @{N ='VMcount' ; E={($_| get-vm).count }},


#@{N= 'NW_Adapter1' ; E={(Get-NetworkAdapter -VM $_)[0].Name }},

@{N= 'PortGroup1' ; E={(get-networkadapter -VM $_)[0].NetworkName}},

@{N= 'VLAN1' ;E={(Get-VirtualPortGroup -VM $_).extensiondata.config.DefaultPortConfig.vlan.vlanid[0]}},

 

#@{N= 'NW_Adapter2' ; E={(Get-NetworkAdapter -VM $_)[1].Name }},

@{N= 'PortGroup2' ; E={(get-networkadapter -VM $_)[1].NetworkName}},
@{N= 'VLAN2' ;E={(Get-VirtualPortGroup -VM $_).extensiondata.config.DefaultPortConfig.vlan.vlanid[1]}},


#@{N= 'NW_Adapter3' ; E={(Get-NetworkAdapter -VM $_)[2].Name }},

@{N= 'PortGroup3' ; E={(get-networkadapter -VM $_)[2].NetworkName}},
@{N= 'VLAN3' ;E={(Get-VirtualPortGroup -VM $_).extensiondata.config.DefaultPortConfig.vlan.vlanid[2]}},

@{N= 'PortGroup4' ; E={(Get-NetworkAdapter -VM $_)[3].Name }},
@{N= 'VLAN4' ;E={(Get-VirtualPortGroup -VM $_).extensiondata.config.DefaultPortConfig.vlan.vlanid[3]}}

$report |Export-Csv $ReportPath -NoTypeInformation -UseCulture

Any help to fix this issue please

 

many thanks

 

RXJ

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Can you try like this?

 

Get-Cluster  -PipelineVariable cluster |
Get-VMHost |
Get-VM -PipelineVariable vm |
Where-Object { $_.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*" } |
foreach-object -process {
  $obj = [ordered]@{
    vCenter = ([uri]$vm.ExtensionData.Client.ServiceUrl).Host
    Cluster = $cluster.Name
    VMName = $vm.Name
    Portgroup1 = ''
    VLAN1 = ''
    Portgroup2 = ''
    VLAN2 = ''
    Portgroup3 = ''
    VLAN3 = ''
    Portgroup4 = ''
    VLAN4 = ''
  }
  $i = 1
  Get-VirtualPortGroup -VM $vm |
  foreach-object -Process {
    $obj."Portgroup$i" = $_.Name
    $obj."VLAN$i" = $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
    $i++
  }
  New-Object -Type PSObject -Property $obj
} | Export-Csv $ReportPath -NoTypeInformation -UseCulture

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership

Can you try like this?

 

Get-Cluster  -PipelineVariable cluster |
Get-VMHost |
Get-VM -PipelineVariable vm |
Where-Object { $_.ExtensionData.Config.ManagedBy.extensionKey -NotLike "com.vmware.vcDr*" } |
foreach-object -process {
  $obj = [ordered]@{
    vCenter = ([uri]$vm.ExtensionData.Client.ServiceUrl).Host
    Cluster = $cluster.Name
    VMName = $vm.Name
    Portgroup1 = ''
    VLAN1 = ''
    Portgroup2 = ''
    VLAN2 = ''
    Portgroup3 = ''
    VLAN3 = ''
    Portgroup4 = ''
    VLAN4 = ''
  }
  $i = 1
  Get-VirtualPortGroup -VM $vm |
  foreach-object -Process {
    $obj."Portgroup$i" = $_.Name
    $obj."VLAN$i" = $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId
    $i++
  }
  New-Object -Type PSObject -Property $obj
} | Export-Csv $ReportPath -NoTypeInformation -UseCulture

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

rxjoseph
Enthusiast
Enthusiast

Thanks LucD as usual you are a star it works perfect

 

0 Kudos