VMware Cloud Community
paco699
Contributor
Contributor

Get IP address select from NetworkName

Hi folks,

From this code:

Get-VM -name $vm | Get-VMGuest | Select-Object -ExpandProperty Nics | select NetworkName,IPAddress

NetworkName IPAddress
----------- ---------
IP Storage {10.100.38.38}
Backup     {10.100.37.163}

VM Produccion-42 {10.100.43.163}

VM Management{10.100.29.163}

i would like to filter by NetworkName.

i tried:

Get-VM -name $vm | Get-VMGuest | Select-Object -ExpandProperty Nics | select NetworkName,IPAddress | where {$_.Nics.NetworkName -eq "VM Management"}

but it doesn't work.

Please help me to find out the correct syntax.

Thanks very much!

0 Kudos
6 Replies
LucD
Leadership
Leadership

Try like this

Get-VM -name $vm |

Get-VMGuest |

Select-Object -ExpandProperty Nics |

select NetworkName,IPAddress | where {$_.NetworkName -eq "VM Management"}


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

0 Kudos
paco699
Contributor
Contributor

It works like a charm.

Then, i put it in a script:

Get-VM -name $vm |

Get-VMGuest |

Select-Object -ExpandProperty Nics |

where {$_.NetworkName -eq "VM Management"} |

select @{N="IPAddress";E={$_.IPAddress -notlike "*:*"}}

and the result it's like that:

IP Address                  : @{IPAddress=10.100.29.163}

How do you remove "@{IPAddress=}" characters? i just need to have the IP address.

thanks very much!

0 Kudos
LucD
Leadership
Leadership

The IPAddress property is an array, hence the way the value is displayed.

You can join the array elements together in a string to avoid this.

Get-VM -name $vm |

Get-VMGuest |

Select -ExpandProperty Nics |

where {$_.NetworkName -eq "VM Management"} |

select @{N="IPAddress";E={($_.IPAddress | where{$_ -notlike "*:*"}) -join '|'}}

Or as an alternative (avoid one Select-Object).

Get-VM -name $vm |

Get-VMGuest |

where {$_.Nics.NetworkName -eq "VM Management"} |

Select @{N='IPAddress';E={($_.IPAddress | where{$_ -notlike '*:*'}) -join '|'}}


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

0 Kudos
paco699
Contributor
Contributor

From the command line the output is correct.

IPAddress 
--------- 
10.100.29.163

But, when i send the results by export-xlsx command, the valor in the "IP Address" column in the xlsx file is not the expected valor.

i have 2 sorts of valors:
System.Object[] or @{IPAddress=xxx.xxx.xxx}

Snipped Code:

{
$IP = Get-VM -name $vm |

Get-VMGuest |
Select -ExpandProperty Nics |
Where {$_.NetworkName -eq "ADMINISTRATION-41" -or $_.NetworkName -eq "VM Management"} |
Select @{N="IPAddress";E={($_.IPAddress | where{$_ -notlike "*:*"}) -join '|'}}

Add-member -InputObject $Vmresult -MemberType NoteProperty -Name "IP Address" -Value $IP

$Vmresult

$Report += $Vmresult

}

$LogFile = "C:\ReporteEM_" + (Get-Date -UFormat "%Y%m%d_%H%M") + ".xlsx"

$Report | Export-XLSX -Path $LogFile -WorksheetName 'VMsInfo' -Table -AutoFit -Force

Thanks very much for enlightening me about the case.

0 Kudos
LucD
Leadership
Leadership

Is that the snippet the complete script your are running?

How does the $vm variable get populated?

Are you having a loop over multiple VMs somewhere (that you are not showing in the snippet)?

Or do you mean something like this?

$report = foreach($vm in Get-VM){

        Get-VMGuest -VM $vm |

        Select -ExpandProperty Nics |

        Where {$_.NetworkName -eq "ADMINISTRATION-41" -or $_.NetworkName -eq "VM Management"} |

        Select @{N="IPAddress";E={($_.IPAddress | where{$_ -notlike "*:*"}) -join '|'}}

}

$LogFile = "C:\ReporteEM_" + (Get-Date -UFormat "%Y%m%d_%H%M") + ".xlsx"

$Report | Export-XLSX -Path $LogFile -WorksheetName 'VMsInfo' -Table -AutoFit -Force


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

0 Kudos
paco699
Contributor
Contributor

Hi folk,

Here my complete code.

$hosts = @(
     "IP1",
     "IP2",
     "IP3"

);

Connect-Viserver $hosts -User $user -Password $password

$Report = @()
  foreach ($vm in Get-VM) {
  Write-Host $vm.Name

  $IP = Get-VM -name $vm |

Get-VMGuest |
Select -ExpandProperty Nics |
Where {$_.NetworkName -eq "ADMINISTRATION-41" -or $_.NetworkName -eq "VM Management"} |
Select @{N="IPAddress";E={($_.IPAddress | where{$_ -notlike "*:*"}) -join '|'}}

  Add-member -InputObject $Vmresult -MemberType NoteProperty -Name "IP Address" -Value $IP
  $Vmresult
  $Report += $Vmresult

  }

$LogFile = "C:\ReporteEM_" + (Get-Date -UFormat "%Y%m%d_%H%M") + ".xlsx"
$Report | Export-XLSX -Path $LogFile -WorksheetName 'VMsInfo' -Table -AutoFit -Force

Disconnect-VIServer -Server * -Force -Confirm:$False

Again, thanks very much for your help.

0 Kudos