VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Getting Incorrect Subnet Details

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

 

ganapa2000_0-1665809454906.png

 

but in the VM, we have configured the subnet as 255.255.254.0

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am getting the below error

ganapa2000_0-1665816938342.png

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like something went wrong in the copy.
I corrected the code above


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

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

That worked...thank you very much :slightly_smiling_face:

Reply
0 Kudos