VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Need help in getting only IPv4 details

Hi,

I am getting IPv4 and IPv6 both using the below script. How can I get only IPv4 Addresses.

Please help.

$vm = Import-Csv -Path .\VMs.csv -UseCulture

$report = Get-VM $vm.Name | Get-NetworkAdapter |

Select-Object @{N="VM";E={$_.Parent.Name}},

   @{N="NIC";E={$_.Name}},

   @{N="Network";E={$_.NetworkName}},

  MacAddress,

   @{N='IP';E={

   $vNIc = $_

   ($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress -join ', '

   }},

   @{N='DNS';E={($_.Parent.ExtensionData.Guest.Net.DNSConfig).IPAddress -join ', '}}

$report | ft -auto

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

My bad, try with

@{N='IP';E={

    $vNIc = $_

    (($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress | where{$_ -match '\.'}) -join ', '

    }},   @{N='DNS';E={($_.Parent.ExtensionData.Guest.Net.DNSConfig).IPAddress -join ', '}}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Add a Where-clause, something like this for example

@{N='IP';E={

    $vNIc = $_

    (($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress | where{$_ -contains '.'}) -join ', '

    }},


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

LucD,

I am getting IP field blank.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, try with

@{N='IP';E={

    $vNIc = $_

    (($_.Parent.ExtensionData.Guest.Net | where { $_.MacAddress -eq $vNIc.MacAddress }).IPAddress | where{$_ -match '\.'}) -join ', '

    }},   @{N='DNS';E={($_.Parent.ExtensionData.Guest.Net.DNSConfig).IPAddress -join ', '}}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much LucD. That worked perfectly Smiley Happy

0 Kudos