VMware Cloud Community
jaguar5150
Contributor
Contributor
Jump to solution

Obtain Guest Subnet Mask

Hello,

I've been up and down the internet and cannot seem to find an answer to this.

I'm trying to script output to include a Guest VM's IP address and its Subnet Mask.  I cannot seem to find any command that will present it without having to provide an account to the VM (Get-VMGuestNetworkInterface).  Isn't VMtools aware of the IPaddress and netmask?  Maybe this is so simple I'm overlooking something.

I'm looking for something like this:  Get-Cluster "<cluster>" | Get-VM | Select Name, {$_.Guest.IPAddress}, {$_.ExtensionData.Summary.Guest.Subnet}

Obviously "Guest.Subnet" isn't a valid parameter, but is there something that would work in its place?

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That is because the ForEach doesn't place any objects in the pipeline.

A trick to avoid this, is to do the complete script in "call" block (the & operator), and then pipe that to the Export-Csv.

Something like this

&{foreach($vm in (Get-Cluster | Get-VM)){
   
$vm.ExtensionData.Guest.Net | Select @{N="VM";E={$vm.Name}},MacAddress,Network,
   
@{N="DHCP";E={$_.IpConfig.Dhcp.Ipv4.Enable}},
   
@{N="IP";E={$_.IpAddress[0]}},
   
@{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)
        }}
}}
| Export-Csv -Path report.csv -NoTypeInformation -UseCulture


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

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

The subnet mask is not directly available, but the prefixlength is :smileycool:

Try something like this

$vm = Get-VM MyVM
$vm.ExtensionData.Guest.Net | Select @{N="VM";E={$vm.Name}},MacAddress,Network,
   
@{N="DHCP";E={$_.IpConfig.Dhcp.Ipv4.Enable}},
   
@{N="IP";E={$_.IpAddress[0]}},
   
@{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)
    }}

Note that this only handles VMs with 1 NIC and an IPv4 address.

For multiple NICs the code needs to be adapted.


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

0 Kudos
jaguar5150
Contributor
Contributor
Jump to solution

Wow, nice one.  I think it's safe to say I would not have figured that out.  Worked great for one VM!  If I may ask for one small modification - I would like to run this against all VMs in a cluster (or group of cluster).  Is that possible?

Thanks a ton!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, something like this should do the trick

foreach($vm in (Get-Cluster | Get-VM)){
   
$vm.ExtensionData.Guest.Net | Select @{N="VM";E={$vm.Name}},MacAddress,Network,
   
@{N="DHCP";E={$_.IpConfig.Dhcp.Ipv4.Enable}},
   
@{N="IP";E={$_.IpAddress[0]}},
   
@{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)
        }}
}


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

0 Kudos
jaguar5150
Contributor
Contributor
Jump to solution

Thanks again.  And my apologies, I thought I would be able to figure this one out on my own, but I cannot seem to get it to export to .csv.  If I pipe in Export-CSV on the end of it all, it throws an error about an empty pipe element.

Any ideas there?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is because the ForEach doesn't place any objects in the pipeline.

A trick to avoid this, is to do the complete script in "call" block (the & operator), and then pipe that to the Export-Csv.

Something like this

&{foreach($vm in (Get-Cluster | Get-VM)){
   
$vm.ExtensionData.Guest.Net | Select @{N="VM";E={$vm.Name}},MacAddress,Network,
   
@{N="DHCP";E={$_.IpConfig.Dhcp.Ipv4.Enable}},
   
@{N="IP";E={$_.IpAddress[0]}},
   
@{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)
        }}
}}
| Export-Csv -Path report.csv -NoTypeInformation -UseCulture


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

jaguar5150
Contributor
Contributor
Jump to solution

Thank you very much!  This is perfect!

0 Kudos
DomusIT
Contributor
Contributor
Jump to solution

Thanks for a VERY useful script! - a bit of editing and here is a script version that lists ALL VM's (not just the ones that are included in clusters) - also inluded secondary IP's and MASK's so that IPv4 and IPv6 are shown:

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

    $vm.ExtensionData.Guest.Net | Select @{N="VM";E={$vm.Name}},MacAddress,Network,

    @{N="DHCP";E={$_.IpConfig.Dhcp.Ipv4.Enable}},

    @{N="IP-1";E={$_.IpAddress[0]}},

    @{N="Subnet Mask-1";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="IP-2";E={$_.IpAddress[1]}},

    @{N="Subnet Mask-2";E={

            $dec = [Convert]::ToUInt32($(("1" * $_.IpConfig.IpAddress[1].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)

        }}

}

- Erkka Hakkarainen

0 Kudos
MRoushdy
Hot Shot
Hot Shot
Jump to solution

LuCD, You always impress me. Thanks a lot, this script saved us a lot of time.

vEXPERT - VCAP-DCV - Blog: arabitnetwork.com | YouTube: youtube.com/c/MohamedRoushdy
0 Kudos
bikashyadav
Contributor
Contributor
Jump to solution

How do we add the gateway here  ? Tried but its giving irrelevant info.
0 Kudos