PowerCLI

 View Only
  • 1.  Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch?

    Posted Feb 04, 2019 01:53 AM

    Hi All,

    I'm running vSphere v6.7U1 and using Distributed vSwitch in all of my ESXi servers. I need to get some information in the .CSV file with the below column:

    VM Name, IPv4, MAC Address, NetworkName, VLanID, NicType, VMHostname

    However, the VLanID and the VMHostname is not listed properly or it is still empty with the below script:

    Get-VM | Where-Object {($_.Name -like "PRD-SQL*")} |

        ForEach-Object {

        $vm = $_

        $NIC = $vm | Get-NetworkAdapter

        $portGrp = $vm | Get-VirtualPortGroup

        $vm.Guest.Nics |

            ForEach-Object {

            $mac = $_.MacAddress

            $netName = $_.NetworkName

            # Port group treatment depends on the switch type used (distributed etc.)

            $portGrp | Where-Object { $_.Name -eq $netName } |

                ForEach-Object {

                if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {

                    if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {

                        $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId

                    }

                    elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {

                        if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {

                            [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object {"$($_.Start)-$($_.End)"}))

                        }

                        else {

                            $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId

                        }

                    }

                }

                else {

                    $_.VlanId

                }

            } | Set-Variable VLanID

            [PSCustomObject] @{

                Name        = $vm.Name

                IP          = $_.IPAddress -join ','

                MAC         = $mac

                NetworkName = $netName

                VLanID      = $VLanID

                NicType     = $NIC     | Where-Object { $_.MacAddress -eq $mac } | Select-Object -Expand Type      

                VMHostName  = $_.VMHostName 

            }

        }

    } | Export-CSV C:\VM-Results.csv -NoType

    This is my PowerCLI configuration:

    PowerCLI Version

    ----------------

       VMware PowerCLI 11.0.0 build 10380590

    ---------------

    Component Versions

    ---------------

       VMware Cis Core PowerCLI Component PowerCLI Component 11.0 build 10335701

       VMware VimAutomation VICore Commands PowerCLI Component PowerCLI Component 11.0 build 10336080

       VMware VimAutomation Vds Commands PowerCLI Component PowerCLI Component 11.0 build 10336077

       VMware VimAutomation Cloud PowerCLI Component PowerCLI Component 11.0 build 10379994

    Any help would be greatly appreciated.

    Thanks in advance.



  • 2.  RE: Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch?
    Best Answer

    Posted Feb 04, 2019 08:19 AM

    Try like this

    Get-VM -PipelineVariable vm | Where-Object {($_.Name -like "PRD-SQL*")} |

       ForEach-Object {

       Get-NetworkAdapter -VM $vm -PipelineVariable vnic |

       ForEach-Object {

       [PSCustomObject] @{

       Name = $vm.Name

       IP = ($vm.Guest.Nics | where {$_.Device.Name -eq $vnic.Name}).IPAddress -join ','

       MAC = $vnic.MacAddress

       NetworkName = $vnic.NetworkName

       VLanID = & {

       if ($vnic.NetworkName) {

       Get-VirtualPortGroup -Name $vnic.NetworkName -VM $vm |

       ForEach-Object -Process {

       if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {

       if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {

       $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId

       }

       elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {

       if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {

       [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object {"$($_.Start)-$($_.End)"}))

       }

       else {

       $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId

       }

       }

       }

       else {

       $_.VlanId

       }

       }

       }}

       NicType = $vnic.Type

       VMHostName = $vm.VMHost.Name

       }

       }

    } | Export-CSV C:\VM-Results.csv -NoType



  • 3.  RE: Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch?

    Posted Feb 05, 2019 06:07 AM

    Hi LucD​, thanks for assisting me in this thread, however the column in the .CSV does not contains the ESXi server and the VLAN-ID from the VM?

    MacAddressWakeOnLanEnabledNetworkNameTypeParentIdParentUidConnectionStateExtensionDataIdName

    This is the script that I have executed under my PowerShell IDE (VS Code):

    Get-VM -PipelineVariable vm | Where-Object {($_.Name -like "PRD-SQL*")} |

       ForEach-Object {

        Get-NetworkAdapter -VM $vm -PipelineVariable vnic

        ForEach-Object {

            [PSCustomObject] @{

            Name = $vm.Name

            IP = ($vm.Guest.Nics | Where-Object {$_.Device.Name -eq $vnic.Name}).IPAddress -join ','

            MAC = $vnic.MacAddress

            NetworkName = $vnic.NetworkName

            VLanID = & {

                if ($vnic.NetworkName) {

                    Get-VirtualPortGroup -Name $vnic.NetworkName -VM $vm |

                    ForEach-Object -Process {

                        if ($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]) {

                            if ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchPvlanSpec]) {

                                $_.ExtensionData.Config.DefaultPortConfig.Vlan.PvlanId

                            } elseif ($_.ExtensionData.Config.DefaultPortConfig.Vlan -is [VMware.Vim.VmwareDistributedVirtualSwitchVlanSpec]) {

                                if ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -is [VMware.Vim.NumericRange[]]) {

                                    [string]::Join(',', ($_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId | ForEach-Object {"$($_.Start)-$($_.End)"}))

                                } else {

                                    $_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId

                                }

                            }

                        }

                        else {

                            $_.VlanId

                        }

                    }

                }

            }

            NicType = $vnic.Type

            VMHostName = $vm.VMHost.Name

            }

        }

    } | Export-CSV C:\VM-Results.csv -NoType



  • 4.  RE: Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch?

    Posted Feb 05, 2019 07:03 AM

    You seem to be missing the pipeline symbol at the end of this line

    Get-NetworkAdapter -VM $vm -PipelineVariable vnic |



  • 5.  RE: Unable to get the VLanID and VMHostname from VM running with Distributed vSwitch?

    Posted Feb 06, 2019 02:34 AM

    Yes, you are correct.

    Thanks Luc for the help, I appreciate it.