VMware Cloud Community
vin01
Expert
Expert

guest os ipaddress,subnet and gateway information

Hello

I'm trying to pull Guest VMS IP address,Subnet and gateway with the below script..But unfortunately its not working...In  datacenter vms run on standard and DVswitch..vms version start from version 7.Can anyone correct me..I need this report on 3000vms


Used script..

==

$vm = Get-VM "Testvm"

$row = "" | Select Name,Host,OS,NicType,VLAN,IP,Gateway,Subnetmask,DNS

$nicInfo = Get-VMGuestNetworkInterface -VM $vm

$row.Name = $vm.Name

$row.Host = $vm.Host.Name

$row.OS = $vm.Guest.OSFullName

$row.NicType = [string]::Join(',',(Get-NetworkAdapter -VM $vm  | Select -ExpandProperty Type))

$row.VLAN = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | Select -ExpandProperty VlanId))

$row.IP = [string]::Join(',',$vm.Guest.IPAddress)

$row.Gateway = [string]::Join(',',($nicInfo | Select -ExpandProperty DefaultGateway))

$row.Subnetmask = [string]::Join(',',($nicInfo | Select -ExpandProperty Subnetmask))

$row.DNS = [string]::Join(',',($nicInfo | Select -ExpandProperty Dns))

$row

====

Errors while running on vms connected with standard and DV switch..

=====

WARNING: The 'Host' property of VirtualMachine type is deprecated. Use the

'VMHost' property instead.

Select-Object : Cannot process argument because the value of argument "obj" is

null. Change the value of argument "obj" to a non-null value.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:10 char:53

+ $row.Gateway = [string]::Join(',',($nicInfo | Select <<<<  -ExpandProperty De

faultGateway))

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgument

   NullException

    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.Selec

   tObjectCommand

Select-Object : Cannot process argument because the value of argument "obj" is

null. Change the value of argument "obj" to a non-null value.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:11 char:56

+ $row.Subnetmask = [string]::Join(',',($nicInfo | Select <<<<  -ExpandProperty

Subnetmask))

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgument

   NullException

    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.Selec

   tObjectCommand

Select-Object : Cannot process argument because the value of argument "obj" is

null. Change the value of argument "obj" to a non-null value.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:12 char:49

+ $row.DNS = [string]::Join(',',($nicInfo | Select <<<<  -ExpandProperty Dns))

    + CategoryInfo          : InvalidArgument: (:) [Select-Object], PSArgument

   NullException

    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.Selec

   tObjectCommand

Name       : testvm

Host       : 10.50.XX.XX

OS         : Microsoft Windows Server 2008 R2 (64-bit)

NicType    : Vmxnet3

VLAN       : 58

IP         : 10.50.XX.XX,fe80::f5f9:e945:ca03:55f5

Gateway    :

Subnetmask :

DNS        :

===================================

error while running on DVswitch vms..

===============================

Get-VMGuestNetworkInterface : 22-Jul-14 4:14:17 PM    Get-VMGuestNetworkInterfa

ce        Execution of the  script in guest OS on VM 'testvm' failed: 'Ac

cess is denied.

'

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:3 char:39

+ $nicInfo = Get-VMGuestNetworkInterface <<<<  -VM $vm

    + CategoryInfo          : InvalidData: (DCMLXLIVEAMS:VirtualMachineImpl) [

   Get-VMGuestNetworkInterface], VimException

    + FullyQualifiedErrorId : Client20_RunScriptInGuestHelper_RunScriptFileInG

   uest_ScriptError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuestNe

  tworkInterface

Get-VMGuestNetworkInterface : 22-Jul-14 4:14:17 PM    Get-VMGuestNetworkInterfa

ce        Unable to parse script output.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:3 char:39

+ $nicInfo = Get-VMGuestNetworkInterface <<<<  -VM $vm

    + CategoryInfo          : InvalidArgument: (:) [Get-VMGuestNetworkInterfac

   e], ViError

    + FullyQualifiedErrorId : Client20_VmHostServiceImpl_GetVmGuestNetworkInte

   rface_UnableToParse,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGues

  tNetworkInterface

WARNING: The output of the command produced distributed virtual portgroup

objects. This behavior is obsolete and may change in the future. To retrieve

distributed portgroups, use Get-VDPortgroup cmdlet in the VDS component. To

retrieve standard portgroups, use -Standard.

Select-Object : Property "VlanId" cannot be found.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:8 char:70

+ $row.VLAN = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | Select <<<<  -

ExpandProperty VlanId))

    + CategoryInfo          : InvalidArgument: (10.16.12.1-254_V10:PSObject) [

   Select-Object], PSArgumentException

    + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Comm

   ands.SelectObjectCommand

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: value"

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:8 char:27

+ $row.VLAN = [string]::Join <<<< (',',(Get-VirtualPortGroup -VM $vm | Select -

ExpandProperty VlanId))

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: value"

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:10 char:30

+ $row.Gateway = [string]::Join <<<< (',',($nicInfo | Select -ExpandProperty De

faultGateway))

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: value"

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:11 char:33

+ $row.Subnetmask = [string]::Join <<<< (',',($nicInfo | Select -ExpandProperty

Subnetmask))

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Exception calling "Join" with "2" argument(s): "Value cannot be null.

Parameter name: value"

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vmipsubnetgateway1.p

s1:12 char:26

+ $row.DNS = [string]::Join <<<< (',',($nicInfo | Select -ExpandProperty Dns))

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : DotNetMethodException

Name       : Testvm

Host       : 10.50.XX.XX

OS         : Microsoft Windows Server 2003 (32-bit)

NicType    : Vmxnet3

VLAN       :

IP         : 10.16.XX.XX,10.16.XX.XX

Gateway    :

Subnetmask :

DNS        :

Regards Vineeth.K
4 Replies
ssbkang
Enthusiast
Enthusiast

Hello Vin,

Would you like to try this?

$vm = Get-VM -Name "Name"

$subnetmask = @()

$row = "" | Select Name,Host,OS,NicType,VLAN,IP,Gateway,Subnetmask,DNS

$row.Name = $vm.Name

$row.Host = $vm.VMHost.Name

$row.OS = $vm.Guest.OSFullName

$row.NicType = [string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select -ExpandProperty Type))

$row.VLAN = [string]::Join(',',(Get-VirtualPortGroup -VM $vm | %{$_.VlanId))

$row.IP = [string]::Join(',',$vm.Guest.IPAddress)

$row.Gateway = $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress | where {$_ -ne $null}

foreach ($iproute in $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute) {

    if (($vm.Guest.IPAddress -replace "[0-9]$|[1-9][0-9]$|1[0-9][0-9]$|2[0-4][0-9]$|25[0-5]$", "0" | select -uniq) -contains $iproute.Network) {

        $subnetmask += $iproute.Network + "/" + $iproute.PrefixLength

    }

}

$row.Subnetmask = [string]::Join(',',($subnetmask))

$row.DNS = [string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))

$row

filip_hasa
Contributor
Contributor

hi,

script is amazing. Just VLAN did not work for me. here is my update.

Filip

$vm = Get-VM -Name 'name'

$subnetmask = @()

$row = "" | Select Name,Host,OS,NicType,VLAN,IP,Gateway,Subnetmask,DNS

$row.Name = $vm.Name

$row.Host = $vm.VMHost.Name

$row.OS = $vm.Guest.OSFullName

$row.NicType = [string]::Join(',',(Get-NetworkAdapter -Vm $vm | Select -ExpandProperty Type))

$row.VLAN = [string]::Join(',',((Get-VirtualPortGroup -VM $vm ).Name))

$row.IP = [string]::Join(',',$vm.Guest.IPAddress)

$row.Gateway = $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute.Gateway.IpAddress | where {$_ -ne $null}

foreach ($iproute in $vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute) {

    if (($vm.Guest.IPAddress -replace "[0-9]$|[1-9][0-9]$|1[0-9][0-9]$|2[0-4][0-9]$|25[0-5]$", "0" | select -uniq) -contains $iproute.Network) {

        $subnetmask += $iproute.Network + "/" + $iproute.PrefixLength

    }

}

$row.Subnetmask = [string]::Join(',',($subnetmask))

$row.DNS = [string]::Join(',',($vm.ExtensionData.Guest.IpStack.DnsConfig.IpAddress))

$row

0 Kudos
vin01
Expert
Expert

‌This thread might help you.

guest os ipaddress,subnet and gateway information

Regards Vineeth.K
0 Kudos
dhanu2k7
Enthusiast
Enthusiast

Hi

I am reciving mask is empty, is there any reason?

0 Kudos