Automation

 View Only
  • 1.  VM Portgroup and VLAN details

    Posted Jun 20, 2022 10:30 AM

    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

     



  • 2.  RE: VM Portgroup and VLAN details
    Best Answer

    Posted Jun 20, 2022 11:14 AM

    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

     



  • 3.  RE: VM Portgroup and VLAN details

    Posted Jun 20, 2022 02:30 PM

    Thanks LucD as usual you are a star it works perfect