Hi,
I am getting incorrect subnet in the output. Please help.
#From a file
$names = Get-Content -Path .\vmlist.txt
$regexNames = $names -join '|'
Get-View -ViewType VirtualMachine -Filter @{'Name'=$regexNames;'Runtime.PowerState'='poweredOn'} -PipelineVariable vm | Select @{N='vCenter';E={([uri]$_.Client.ServiceUrl).Host}},
@{N="Folder";E={Get-View $_.Parent | Select -ExpandProperty Name}},
@{N='VMHost';E={(Get-View -Id $_.Runtime.Host -Property Name).Name}},
Name,
@{N='IPAddress';E={$_.Guest.ipAddress}},
@{N='Gateway';E={($_.Guest.ipstack.iprouteconfig.iproute.gateway.ipaddress | where{$_ -ne $null}) -join '|'}},
@{N='SubnetMask';E={
$ipAddr = $_.Guest.ipAddress
@(($_.Guest.Net.ipconfig.ipaddress | where{$ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0}).PrefixLength | %{
[IPAddress]$ip = 0;
$ip.Address = (([UInt32]::MaxValue) -shl (32 - $_) -shr (32 - $_))
$ip.IPAddressToString
}) -join '|'
}},
@{N='DNS';E={[string]::Join(',',($_.Guest.IpStack.DnsConfig.IpAddress))}},
@{N='MacAddress';E={($_.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).MacAddress -join '|'}},
@{N='Network Adapter';E={$_.Config.Hardware.Device | Where {$_ -is [VMware.Vim.VirtualEthernetCard]} | ForEach {$_.GetType().Name.Replace('Virtual','')}}},
@{N='VlanId';E={
$folder = Get-View -Id $_.Parent -Property "[VirtualMachine]ChildEntity.Network.*"
($folder.LinkedView.ChildEntity.where({$vm.MoRef -eq $_.MoRef})).LinkedView.Network.Name}} | ft -auto
but in the VM, we have configured the subnet as 255.255.254.0
Try with
@{N = 'SubnetMask'; E = {
$ipAddr = $_.Guest.ipAddress
(($_.Guest.Net.ipconfig.ipaddress | Where-Object { $ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0 }).PrefixLength |
ForEach-Object -Process {
$bitString = ('1' * $_).PadRight(32, '0')
$ipString = [String]::Empty
for ($i = 0; $i -lt 32; $i += 8 ) {
$byteString = $bitString.Substring($i, 8)
$ipString += "$([Convert]::ToInt32($byteString, 2))."
}
$ipString.TrimEnd('.')
}) -join '|'
}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try with
@{N = 'SubnetMask'; E = {
$ipAddr = $_.Guest.ipAddress
(($_.Guest.Net.ipconfig.ipaddress | Where-Object { $ipAddr -contains $_.IpAddress -and $_.PrefixLength -ne 0 }).PrefixLength |
ForEach-Object -Process {
$bitString = ('1' * $_).PadRight(32, '0')
$ipString = [String]::Empty
for ($i = 0; $i -lt 32; $i += 8 ) {
$byteString = $bitString.Substring($i, 8)
$ipString += "$([Convert]::ToInt32($byteString, 2))."
}
$ipString.TrimEnd('.')
}) -join '|'
}},
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am getting the below error
Looks like something went wrong in the copy.
I corrected the code above
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That worked...thank you very much ![]()
