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
Try like this.
I also adapted the Subnet Mask calculation.
$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
Try like this.
I also adapted the Subnet Mask calculation.
$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
Thanks Luc, worked an absolute treat!