Automation

 View Only
  • 1.  Obtain Guest Subnet Mask

    Posted Dec 13, 2013 07:35 PM

    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!



  • 2.  RE: Obtain Guest Subnet Mask

    Posted Dec 13, 2013 08:49 PM

    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.



  • 3.  RE: Obtain Guest Subnet Mask

    Posted Dec 13, 2013 09:00 PM

    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!



  • 4.  RE: Obtain Guest Subnet Mask

    Posted Dec 13, 2013 09:34 PM

    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)
            }}
    }


  • 5.  RE: Obtain Guest Subnet Mask

    Posted Dec 16, 2013 05:11 PM

    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?



  • 6.  RE: Obtain Guest Subnet Mask
    Best Answer

    Posted Dec 16, 2013 06:03 PM

    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


  • 7.  RE: Obtain Guest Subnet Mask

    Posted Dec 16, 2013 06:18 PM

    Thank you very much!  This is perfect!



  • 8.  RE: Obtain Guest Subnet Mask

    Posted Nov 10, 2016 12:51 PM

    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



  • 9.  RE: Obtain Guest Subnet Mask

    Posted Sep 28, 2020 02:01 PM
    How do we add the gateway here  ? Tried but its giving irrelevant info.


  • 10.  RE: Obtain Guest Subnet Mask

    Posted Jul 26, 2017 11:45 AM

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