GetVMNameAndIPAddress.ps1

適用於: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

System Center Virtual Machine Manager (VMM) does not run agents inside of the guest operating system on virtual machines; therefore, VMM does not manage the IP addresses for virtual machines. However, you can get the IP address for a virtual machine that is in DNS by using the [System.Net.DNS]::GetHostAddresses method.

The following script gets the computer name and IP address for each virtual machine that is managed by VMM and is in DNS.

Disclaimer

# Filename:      GetVMNameAndIPAddress.ps1
# Description:   Gets the computer name and the IP address
#                of each virtual machine managed by VMM that
#                is in DNS.

# Get the VMM server.
Get-VMMServer "VMMServer01.Contoso.com" | out-null

# Get all virtual machines managed by the VMM server.
$VMs = Get-VM

# Loop through the virtual machines and get the computer name
# and IP Address for each virtual machine that has a computer name
# and is in DNS.

Foreach ($VM in $VMs)
{
   if (($VM.ComputerName -ne $NULL) -AND ($VM.ComputerName -ne "*"))
   {   
      $IPS = [System.Net.DNS]::GetHostAddresses($VM.Name)
      trap [System.Exception] {"The following virtual machine is not registered in DNS:"; continue;}
      write-Host ""      
      write-Host "Virtual Machine Name: " $VM.Name
      write-Host "Computer Name: " $VM.ComputerName
      $IPS | where {$_.AddressFamily -eq "InterNetwork"} | Select IPAddressToString
      Write-Host ""
      Write-Host "#####################################################"   
   }
}