Hey, Scripting Guy! How Can I Use VMM Cmdlets to Move Virtual Machines and Virtual Machine Hosts?

ScriptingGuy1

Bookmark and Share

About the author: Susan Hill writes content for Virtual Machine Manager. While her early influences include Kernighan and Ritchie, more recently she has broadened her skills under the Windows PowerShell movement. She aspires to be the Gauguin of Windows PowerShell, and in preparation for this, she spends her spare time travelling to warm climates, discovering new wines, and painting.

 

I would love to move to a nice place, preferably with sun, sand, and palm trees. Is it another dreary day in the Northwest corner of the United States? Why, yes it is. Now that I’m in the mood to move, it seems like a good time to discuss moving virtual machines. Yesterday’s blog post introduced you to System Center Virtual Machine Manager (VMM) cmdlets. Today, we’ll use VMM cmdlets to move virtual machines between hosts and store them in the library, and to move a virtual machine host into a host group.

But first, I’d like to introduce the concept of self-service. VMM provides the Self-Service Portal, which is a Web page through which self-service users can create and operate their own virtual machines within a controlled environment. By using self-service user roles, you can control which actions the self-service users can take on their virtual machines, whether they can create virtual machines, which template they would use to do so, and whether self-service users can store their virtual machines in the library. One question we often get asked is how to expire virtual machines owned by self-service users. Because this is not a feature currently included in the product, this week’s blog series will describe how to accomplish this.

We’ll start off today by moving a virtual machine from one host to another by using the Move-VM cmdlet. The following script:

1.       Connects to the VMM server (always the first thing you do from either the command line or within a script).

2.       Gets the host that you want to move the virtual machine to.

3.       Gets the virtual machine itself.

4.       Migrates the virtual machine to the specified host.

# Connect to the VMM server.

Get-VMMServer –ComputerName “VMMServer01.Contoso.com”

# Get the host that you want to move the virtual machine to.

$VMHost = Get-VMHost –ComputerName “VMHost01.Contoso.com”

#Get the virtual machine.

$VM = Get-VM -Name “VM01”

# Specify the path on the host.

$VMPath = $VMHost.VMPaths[0]

# Migrate the virtual machine.

Move-VM -VM $VM -VMHost $VMHost -Path $VMPath

To store a virtual machine in the library, you use the Store-VM cmdlet instead of the Move-VM vmdlet. The following command stores the virtual machine we just moved in the above script to the library.

Store-VM “VM01” –LibraryServer “LibServer01.Contoso.com” –SharePath “\FileServer01.Contoso.comMyLibrary1VMs”

You do, however, use the Move-VM cmdlet to deploy a virtual machine to a host from the library:

# Connect to the VMM server.

Get-VMMServer –ComputerName “VMMServer01.Contoso.com”

# Get the virtual machine.

$VM = Get-VM | where { $_.Name -eq “VM01” -and $_.LibraryServer -eq “FileServer01.Contoso.com”}

# Get the host.

$VMHost = Get-VMHost -ComputerName “VMHost01.Contoso.com”

# Specify the path on the host.

$VMPath = $VMHost.VMPaths[0]

# Deploy the virtual machine.

Move-VM -VM $VM -VMHost $VMHost -Path $VMPath

To move a host, use the Move-VMHost cmdlet. In the following script, we’ll create a new host group called “Self-Service”, and move the specified host into that host group.

# Get the VMM server.

Get-VMMServer –ComputerName “VMMServer01.Contoso.com”

# Create a host group.

$NewHG = New-VMHostGroup -Name “Self-Service”

# Get the host.

$VMHost = Get-VMHost -ComputerName “VMHost01.Contoso.com”

# Move the host to the new host group.

Move-VMHost -VMHost $VMHost -ParentHostGroup $NewHG

Now that we have our self-service host group with a host in it, we’re set up for tomorrow’s post where we’ll create new virtual machines and place them on the host, and then give them an expiration date.

 

0 comments

Discussion is closed.

Feedback usabilla icon