How to Deploy or Store a Virtual Machine and Find a Suitable Host: Command-Line Examples

The examples in this topic show you how to use the Windows PowerShell - Virtual Machine Manager command shell to perform the following tasks:

  • Create and deploy a new virtual machine. When you create a virtual machine, you can deploy it on a host or store it in the Virtual Machine Manager library. The example in this topic shows you how to create and deploy a new virtual machine.
  • Store an existing virtual machine. You can move an existing machine that is currently deployed on a host off of the host and store it in the Virtual Machine Manager library.
  • Calculate the rating for a host. If you have a virtual machine and multiple hosts, you can determine the suitability of a given server as a host for that virtual machine.

In addition to the examples provided here, other examples for how to deploy, store, or place virtual machines are available in the command-line help.

How to Create and Deploy a Virtual Machine

You can create a new virtual machine from a virtual hard disk and deploy it on a host managed by Virtual Machine Manager by typing the following series of commands at the Virtual Machine Manager command prompt:

PS C:\> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
PS C:\> $VHD = Get-Virtualharddisk | where {$_.Name -eq "Blank Disk - Large"}
PS C:\> $VMHost = Get-VMHost | where {$_.Name -eq "VMHost01.Contoso.com"}
PS C:\> New-VM -Name "VM01" -VirtualHardDisk $VHD -VMHost $VMHost -Path "C:\MyVMs" –RunAsynchronously

Explanation of each command line:

  • The first command establishes a connection to VMMServer1 in the Contoso.com domain (if a connection does not already exist) and retrieves from the Virtual Machine Manager database on VMMServer1 the object that represents VMMServer1. The following commands use this server by default.
  • The second command retrieves from the Virtual Machine Manager database on VMMServer1 the virtual hard disk named "Blank Disk - Large" and stores the virtual hard disk object in variable $VHD.
  • The third command retrieves the object that represents the virtual machine host named VMHost01 (located in the Contoso.com domain) and stores the host object in variable $VMHost.
  • The fourth command creates a new virtual machine named VM01 from the virtual hard disk called "Blank Disk - Large" and deploys the new virtual machine on the C: drive of VMHost01 in the folder called MyVMs. Using the -RunAsynchronously parameter returns control to the shell immediately (before the command completes).

For more information about how to use the command shell to create a virtual machine, type Get-Help New-VM at the Virtual Machine Manager command prompt.

How to Store a Deployed Virtual Machine in the Library

You can store a virtual machine (that is currently deployed on a host but is turned off) in the Virtual Machine Manager library by typing the following series of commands at the Virtual Machine Manager command prompt:

C:\PS> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
C:\PS> $VM = Get-VM -Name "VM01"
C:\PS> $Library = Get-LibraryServer -ComputerName "Library1"
C:\PS> Store-VM -LibraryServer $Library -VM $VM -SharePath "\\Library1.Contoso.com\MyLibrary1\VMs"

Explanation of each command line:

  • The first command connects to VMMServer1 in the Contoso.com domain and retrieves the server object from the Virtual Machine Manager database. The following commands use this server by default.
  • The second command gets from the Virtual Machine Manager database the object that represents the virtual machine named VM01 and stores the virtual machine object in variable $VM.
  • The third command gets the object that represents the library server named Library1 and stores the library server object in variable $Library.
  • The fourth command removes VM01 from its host and stores it in the location \\Library1.Contoso.com\MyLibrary1\VMs on Library1. The command automatically uses the fastest available transfer type.

For more information about how to use the command shell to store a virtual machine in the library, type Get-Help Store-VM at the Virtual Machine Manager command prompt.

How to Get the Rating for a Host

You can calculate the rating for a virtual machine host, which indicates the suitability of that computer as a host for a specific virtual machine, by typing the following commands at the Virtual Machine Manager command prompt:

PS C:\> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
PS C:\> $VM = Get-VM -Name "VM-01"
PS C:\> $VMHost = Get-VMHost -ComputerName "VMHost01.Contoso.com" 
PS C:\> $HostRating = Get-VMHostRating -VM $VM -VMHost $VMHost

Explanation of each command line:

  • The first command connects to VMMServer1 in the Contoso.com domain and retrieves the server object from the Virtual Machine Manager database. The following commands use this server by default.
  • The second command gets from the Virtual Machine Manager database the object that represents the virtual machine named VM01 and stores the virtual machine object in variable $VM.
  • The third command retrieves the object that represents the host named VMHost01 in the Contoso.com domain and stores the host object in variable $VMHost.
  • The fourth command returns the placement rating for VMHost01 that indicates its suitability as a host for VM01 and stores the rating information in variable $HostRating.

For more information about how to use the command shell to calculate the host rating, type Get-Help Get-VMHostRating at the Virtual Machine Manager command prompt.

See Also

Concepts

How to Add Hosts and Library Servers: Command-Line Examples
How to Create Library Resources: Command-Line Examples
How to Enable Self-Service Users: Command-Line Examples