VMware Cloud Community
LittleOne
Enthusiast
Enthusiast
Jump to solution

Observed IP Ranges

Hey Guys,

I need some help to modify a script I found here. Basically i need to obtain esxi server name, not the vmhostid and if its posible add a column with the cluster name,

Thanks in advance!

Fer

This is the output i obtain with the below script

VMHostId                                  Device                         IPSubnet                                               Vlan

HostSystem-host-52823           vmnic0                         xxx.xxx.34.128-xxx.xxx.34.254                             934

I hope someone can help me.

function Get-ObservedIPRange {

[CmdletBinding()]
param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True,HelpMessage=”Physical NIC from Get-VMHostNetworkAdapter”)]
[Object[]]$Nics

)

process {

forEach ($Nic in $Nics)
{

if($Nic -notlike “vmk*”)
{
[VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]$NicImpl = $Nic
$hostView = Get-VMHost -Id $NicImpl.VMHostId | Get-View -Property ConfigManager
$ns = Get-View $hostView.ConfigManager.NetworkSystem
$hints = $ns.QueryNetworkHint($NicImpl.Name)

foreach ($hint in $hints)
{
if( ($hint.ConnectedSwitchPort) )
{
foreach ($subnet in $hint.subnet)
{
$observed = New-Object -TypeName PSObject
$observed | Add-Member -MemberType NoteProperty -Name Device -Value $NicImpl.Name
$observed | Add-Member -MemberType NoteProperty -Name VMHostId -Value $NicImpl.VMHostId
$observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet
$observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId
Write-Output $observed
}
}
}

}

}
}

}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

function Get-ObservedIPRange {

    [CmdletBinding()]

    param(

    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True,HelpMessage=”Physical NIC from Get-VMHostNetworkAdapter”)]

    [Object[]]$Nics

    )

    process {

        forEach ($Nic in $Nics){

            if($Nic -notlike “vmk*”){

                [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]$NicImpl = $Nic

                $esx = Get-VMHost -Id $NicImpl.VMHostId

                $ns = Get-View $esx.ExtensionData.ConfigManager.NetworkSystem

                $hints = $ns.QueryNetworkHint($NicImpl.Name)

               

                foreach ($hint in $hints){

                    if( ($hint.ConnectedSwitchPort)){

                       foreach ($subnet in $hint.subnet){

                           $observed = New-Object -TypeName PSObject

                           $observed | Add-Member -MemberType NoteProperty -Name Device -Value $NicImpl.Name

                           $observed | Add-Member -MemberType NoteProperty -Name VMHost-Value $esx.Name

                           $observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet

                           $observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId

                           Write-Output $observed

                       }

                    }

                }

            }

        }

    }

}


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

View solution in original post

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

function Get-ObservedIPRange {

    [CmdletBinding()]

    param(

    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True,HelpMessage=”Physical NIC from Get-VMHostNetworkAdapter”)]

    [Object[]]$Nics

    )

    process {

        forEach ($Nic in $Nics){

            if($Nic -notlike “vmk*”){

                [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]$NicImpl = $Nic

                $esx = Get-VMHost -Id $NicImpl.VMHostId

                $ns = Get-View $esx.ExtensionData.ConfigManager.NetworkSystem

                $hints = $ns.QueryNetworkHint($NicImpl.Name)

               

                foreach ($hint in $hints){

                    if( ($hint.ConnectedSwitchPort)){

                       foreach ($subnet in $hint.subnet){

                           $observed = New-Object -TypeName PSObject

                           $observed | Add-Member -MemberType NoteProperty -Name Device -Value $NicImpl.Name

                           $observed | Add-Member -MemberType NoteProperty -Name VMHost-Value $esx.Name

                           $observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet

                           $observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId

                           Write-Output $observed

                       }

                    }

                }

            }

        }

    }

}


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

Reply
0 Kudos
LittleOne
Enthusiast
Enthusiast
Jump to solution

Thanks a lot LucD!!!!

Reply
0 Kudos
virtualycool
Contributor
Contributor
Jump to solution

worksgreat.

whatwould I need to modify to have it show which vswitch the card is associated with?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

Note it only supports VSS for now, not VDS

function Get-ObservedIPRange {

   [CmdletBinding()]

    param(

    [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$True,HelpMessage=”Physical NIC from Get-VMHostNetworkAdapter”)]

    [Object[]]$Nics

    )

    process {

        forEach ($Nic in $Nics){

            if($Nic -notlike “vmk*”){

                [VMware.VimAutomation.Client20.Host.NIC.PhysicalNicImpl]$NicImpl = $Nic

                $esx = Get-VMHost -Id $NicImpl.VMHostId

                $pnicTab = @{}

                $esx.ExtensionData.Config.Network.Pnic | %{

                    $pnicTab.Add($_.Key,$_.Device)

                }

                $switchTab = @{}

                $esx.ExtensionData.Config.Network.Vswitch | %{

                    $vss = $_.Name

                    $_.Pnic | %{

                        $switchTab.Add($pnicTab[$_],$vss)

                    }

                }

                $ns = Get-View $esx.ExtensionData.ConfigManager.NetworkSystem

                $hints = $ns.QueryNetworkHint($NicImpl.Name)

              

                foreach ($hint in $hints){

                    if( ($hint.ConnectedSwitchPort)){

                       foreach ($subnet in $hint.subnet){

                           $observed = New-Object -TypeName PSObject

                           $observed | Add-Member -MemberType NoteProperty -Name Device -Value $NicImpl.Name

                           $observed | Add-Member -MemberType NoteProperty -Name VMHost -Value $esx.Name

                           $observed | Add-Member -MemberType NoteProperty -Name VSS -Value $switchTab[$Nic.Name]

                           $observed | Add-Member -MemberType NoteProperty -Name IPSubnet -Value $subnet.IPSubnet

                           $observed | Add-Member -MemberType NoteProperty -Name VlanId -Value $subnet.VlanId

                           Write-Output $observed

                       }

                    }

                }

            }

        }

    }

}

Get-ObservedIPRange -Nics (Get-VMHostNetworkAdapter -VMHost MyEsx)


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

virtualycool
Contributor
Contributor
Jump to solution

yourROCK!!!Smiley Happy

Reply
0 Kudos
amarwane854001
Contributor
Contributor
Jump to solution

I tried to implement your function in my scrip to obtain the IP subnet for vlan configured on physical NICs, but I kept getting no output.

When I verified the content of my variables, both at the main script and inside the function, my physical NICs object had correct data in them such as:

$NicImpl

Name       Mac               DhcpEnabled IP              SubnetMask      DeviceName

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

vmnic3     xx:xx:xx:xx:xx:xx False                                           vmnic3

However the hints variable onside the Get-ObservedIPRange function had only Device Name, while subnet and Network have none, which explains why I got no output:

$hint

Device              : vmnic3

Subnet              :

Network             :

ConnectedSwitchPort : VMware.Vim.PhysicalNicCdpInfo

LldpInfo            :

I am wondering if there is any thing that changed on the powershell APIs in version ESX 6.5 build 8935087 that rendered the commands you used in your function Obsolete or Deprecated.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I suspect it might be an issue of CDP not being activated on the switches to which the pNICs are connected.

Do you see the data in the Web Client?


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

Reply
0 Kudos