VMware Cloud Community
faherne_CTI
Enthusiast
Enthusiast
Jump to solution

Calculate Cluster VM's Subnetwork Address

Hi,

I used some of LucD's great posts to draft the following script, but the one thing I can't figure out how to do is to calculate a VM's Subnetwork address from it's IP address and Subnet Mask.

I know that you can use -band to apply binary calculations to values, but I don't know how to write the calculation part into the Select statement:

e.g. @{N='SubnetworkAddress';E={$IPAddress -band $SubnetMask}

Here's what I have so far:

&{foreach($vm in (Get-Cluster MyCluster | Get-VM)) {

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

    @{N='PowerState';E={$vm.PowerState}},

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

    @{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='Gateway';E={[string]::Join(',',($vm.ExtensionData.Guest.IpStack.IpRouteConfig.IpRoute | %{if($_.Gateway.IpAddress){$_.Gateway.IpAddress}}))}},

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

  }

} | Export-Csv C:\Scripts\Cluster-VM-Network-Info.csv -NoTypeInformation -UseCulture

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.
I also adapted the Subnet Mask calculation.

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

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

   @{N = 'PowerState'; E = {$vm.PowerState}},

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

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

   $vi = ($_.IPConfig.IPAddress | Where {($_.IPAddress.Split(".")).length -eq 4})[0]

   [IPAddress]$i = 0

   $i.Address = ([uint32]::MaxValue) -shl (32 - $vi.PrefixLength) -shr (32 - $vi.PrefixLength)

   $i.IPAddressToString

   }

   },

   @{N = 'SubnetworkAddress'; E = {

   $vi = ($_.IPConfig.IPAddress | Where {($_.IPAddress.Split(".")).length -eq 4})[0]

   $i = [IPAddress]$vi.IPAddress

   $i.Address = ([uint32]$i.Address) -shl (32 - $vi.PrefixLength) -shr (32 - $vi.PrefixLength)

   $i.IPAddressToString

   }

   },

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

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

   }

}


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this.
I also adapted the Subnet Mask calculation.

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

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

   @{N = 'PowerState'; E = {$vm.PowerState}},

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

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

   $vi = ($_.IPConfig.IPAddress | Where {($_.IPAddress.Split(".")).length -eq 4})[0]

   [IPAddress]$i = 0

   $i.Address = ([uint32]::MaxValue) -shl (32 - $vi.PrefixLength) -shr (32 - $vi.PrefixLength)

   $i.IPAddressToString

   }

   },

   @{N = 'SubnetworkAddress'; E = {

   $vi = ($_.IPConfig.IPAddress | Where {($_.IPAddress.Split(".")).length -eq 4})[0]

   $i = [IPAddress]$vi.IPAddress

   $i.Address = ([uint32]$i.Address) -shl (32 - $vi.PrefixLength) -shr (32 - $vi.PrefixLength)

   $i.IPAddressToString

   }

   },

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

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

   }

}


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

faherne_CTI
Enthusiast
Enthusiast
Jump to solution

Thanks Luc, worked an absolute treat!

0 Kudos