Hyper-V and PowerShell: Get Hyper-V VM IP Addresses

Retrieving a virtual machine’s IP addresses isn’t an especially difficult process, but it’s still something you might do often enough that it would be convenient to have a quicker way. For me, I don’t do it often, but I still like to have a single function. I’ve improved the one I use to make it more useful for a variety of purposes.

This article is part of the Hyper-V and PowerShell series.

The script is quite simple. It just needs a VM, either by name or by using a VM object. I gave it a few extra parameters.

Parameters

  • Name: The name of the virtual machine whose IP addresses you wish to retrieve. Cannot be used with VM.
  • ComputerName: The name of the host that contains the virtual machine whose IP addresses you wish to receive. Cannot be used with VM.
  • VM: The VM object whose IP addresses you wish to receive. Cannot be used with Name or ComputerName.
  • Type: There are three options, and you can use tab completion to cycle through them .”Any” shows all IP addresses. “IPv4” shows only IPv4 addresses. “IPv6” shows only IPv6 addresses.
  • IgnoreAPIPA: Skips 169.254.0.0/16 IPv4 addresses and fe80::/64 IPv6 addresses.

Type Modification

By default, the “Type” is “Any”, so it will retrieve both IPv4 and IPv6 addresses if you don’t specify otherwise. If you prefer, you can modify line 85 to change the default to IPv4 or IPv6.

The Script

Copy the contents of the following script block into a text editor and save it as a .PS1 file. It’s written to expect Get-VMIPAddresses.ps1 as the name, but you can use anything you like:

<#
.SYNOPSIS
	Lists the IP addresses for a virtual machine.

.DESCRIPTION
	Lists the IP addresses for a virtual machine. Can optionally be limited to IPv4 or IPv6 addresses.

.PARAMETER Name
	The name of the virtual machine whose IP addresses you wish to retrieve. Cannot be used with VM.

.PARAMETER ComputerName
	The name of the host that contains the virtual machine whose IP addresses you wish to receive. Cannot be used with VM.

.PARAMETER VM
	The VM object whose IP addresses you wish to receive. Cannot be used with Name or ComputerName.

.PARAMETER Type
	"Any" shows all IP addresses.
	"IPv4" shows only IPv4 addresses.
	"IPv6" shows only IPv6 addresses.

.PARAMETER IgnoreAPIPA
	Skips 169.254.0.0/16 IPv4 addresses and fe80::/64 IPv6 addresses.

.OUTPUTS
	A string array of the virtual machine's IP addresses.

.NOTES
	Author: Eric Siron
	Copyright: (C) 2014 Altaro Software
	Version 1.0
	Authored Date: November 17, 2014

.LINK
	
Hyper-V and PowerShell: Get Hyper-V VM IP Addresses
.EXAMPLE C:\PS> .\Get-VMIPAddresses -Name svtest Description ----------- Retrieves the IP addresses for the VM named svtest. .EXAMPLE C:\PS> .\Get-VMIPAddresses -Name svtest -ComputerName svhv2 Description ----------- Retrieves the IP addresses for the VM named svtest on host svhv2. .EXAMPLE C:\PS> .\Get-VMIPAddresses -Name svtest -Type IPv6 Description ----------- Retrieves only the IPv6 addresses used by svtest. #> #requires -Version 3 #requires -Modules Hyper-V [CmdletBinding()] param ( [Alias("VMName")] [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='ByName', Mandatory=$true, Position=1)] [String]$Name, [Alias("VMHost")] [Parameter(ValueFromPipelineByPropertyName=$true, Position=2, ParameterSetName='ByName')] [String]$ComputerName = $env:COMPUTERNAME, [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, ParameterSetName='ByVM')] [Microsoft.HyperV.PowerShell.VirtualMachine]$VM, [Parameter(ValueFromPipelineByPropertyName=$true)] [ValidateSet("Any", "IPv4", "IPv6")] [String]$Type, [Parameter(ValueFromPipelineByPropertyName=$true)] [Switch]$IgnoreAPIPA=$false ) BEGIN { New-Variable -Name DefaultType -Value "Any" -Option Constant # Change to IPv4 or IPv6 if desired } PROCESS { if([String]::IsNullOrEmpty($Type)) { $Type = $DefaultType } if($VM) { $ParameterSet = @{ 'VM'=$VM } } else { $ParameterSet = @{ 'VMName'="$Name"; 'ComputerName'="$ComputerName" } } $IPAddresses = (Get-VMNetworkAdapter @ParameterSet).IPAddresses switch($Type) { "IPv4" { $IPAddresses = $IPAddresses | where { $_ -match "\." } } "IPv6" { $IPAddresses = $IPAddresses | where { $_ -match ":" } } } if($IgnoreAPIPA) { $IPAddresses = $IPAddresses | where { $_ -notmatch "^(169.254)|(fe80)" } } $IPAddresses } END {}

Converting to a Function

This was written as a script that you call as needed. If you’d like to convert it to a function so you can place it into your PowerShell profile, encapsulate the entire contents of the BEGIN, PROCESS, and END blocks inside a function, preferably Get-VMIPAddresses. In order for the context-sensitive help to work, you’ll also need to move that inside the function block. For example:

#requires -Version 3
#requires -Modules Hyper-V

function Get-VMIPAddresses {
<#
... help info...
#>
BEGIN {
... begin block contents
}
PROCESS {
... process block contents
}
END {}
}

In the next post in the series we will show you an easy way to get a Hyper-V VM MAC Address.

 

Share this post