VMware Cloud Community
antoniogemelli
Hot Shot
Hot Shot

Get all Vlan for the host and network info

Hello,

I need a script to get vlan information for each host with network information, I use this to get for each VM:

&{foreach($vm in (Get-VM)) {

    $vm.ExtensionData.Guest.Net | select -Property @{N='VM';E={$vm.Name}},

    @{N='Host';E={$vm.VMHost.Name}},

    @{N='OS';E={$vm.Guest.OSFullName}},

    @{N='Tools';E={$vm.ExtensionData.Guest.ToolsRunningStatus}},

    @{N='NicType';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty Type))}},

    @{N='VLAN';E={[string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select-Object -ExpandProperty NetworkName))}},

    @{N='IP';E={[string]::Join(',',($vm.Guest.IPAddress | Where {($_.Split(".")).length -eq 4}))}},

    @{N='Gateway';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},

    @{N='Subnet Mask';E={

                $dec = [Convert]::ToUInt32($(('1' * $_.IpConfig.IpAddress[0].PrefixLength).PadRight(32, '0')), 2)

                $DottedIP = $( For ($i = 3; $i -gt -1; $i--) {

                        $Remainder = $dec % [Math]::Pow(256, $i)

                        (                        $dec - $Remainder) / [Math]::Pow(256, $i)

                        $dec = $Remainder

                    } )

                [String]::Join('.', $DottedIP)

            }},

    @{N="DNS";E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))}},

    @{N='MAC';E={[string]::Join(',',$_.MacAddress)}}

  }

} | Export-Csv C:\Users\gemela\Desktop\ESXI_Ip.csv

I need something similar to get info only for hosts.

Thanks

5 Replies
LucD
Leadership
Leadership

Not sure what you mean with "similar", but have a look at Re: Script to get DVportgroup or Standardswtich portgroup Name, Vlan id, total ports and available p...


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

0 Kudos
antoniogemelli
Hot Shot
Hot Shot

Hi LucD, I cannot see this page: We are sorry, but access to this place or content is restricted. If you believe this message to be in error, please contact the system administrator at community_mgr@vmware.com or the person who directed you here.

0 Kudos
LucD
Leadership
Leadership

No problem.

This is the script I was referring to.

Check if it provides what you want, and eventually what is missing.

Get-VirtualPortGroup |

Select Name,

    @{N='Host';E={

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

            [string]::Join(',',(Get-View -Id $_.ExtensionData.Host -Property Name | %{$_.Name}))

        }

        else{

            Get-View -Id $_.VMHostId | Select -ExpandProperty Name

        }}},

    @{N='Type';E={

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

            'VDS'

        }

        else{'VSS'}}},

    @{N='VlanId';E={

        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 | %{"$($_.Start)-$($_.End)"}))

                }

                else{

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

                }

            }

        }

        else{$_.VlanId}}},

    @{N='Available ports';E={

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

            $_.numPorts - $_.ExtensionData.Vm.Count

        }

        else{'na'}}},

    @{N='Total ports';E={

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

            $_.NumPorts

        }

        else{'na'}}}


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

lElOUCHE_79
Enthusiast
Enthusiast

it's possible to export the output?

0 Kudos
LucD
Leadership
Leadership

Yes, pipe the result to whatever output cmdlet you want.
For example to create a CSV

    @{N='Total ports';E={
        if($_ -is [VMware.VimAutomation.ViCore.Impl.V1.Host.Networking.DistributedPortGroupImpl]){
            $_.NumPorts
        }
        else{'na'}}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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