AddClusterCreateStopStartRemoveHAVM.ps1

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

System Center Virtual Machine Manager (VMM) 2008 supports highly available virtual machines (HAVMs) deployed on hosts in failover clusters. When their current host fails, HAVMs are failed over to different hosts in the cluster to provide continuing service. VMM detects which hosts in your VMM environment have been configured in a host cluster and allows the placement of HAVMs only on hosts that are part of a host cluster. VMM does not allow placing non-highly available virtual machines on clustered hosts. For more information about creating and managing a highly available environment, see Configuring Host Clusters in VMM to Support Highly Available Virtual Machines (https://go.microsoft.com/fwlink/?LinkId=145065) and Creating and Managing Highly Available Virtual Machines (https://go.microsoft.com/fwlink/?LinkId=145066).

The script in this topic highlights several of the actions you can take on host clusters and HAVMs using the Windows PowerShell - Virtual Machine Manager command shell. For more information about using the VMM command shell to manage your highly available environment, type get-help about_VMM_2008_Failover_Clusters at the command prompt.

The following script adds a host cluster and creates an HAVM. Then the script migrates the HAVM to the highest rated host, stops the HAVM to increase its memory, and then restarts the HAVM. The HAVM is then deleted, and the host cluster is removed from VMM.

Disclaimer

# Filename:      AddClusterCreateStopStartRemoveHAVM.ps1
# Description:   Adds a host cluster and creates a highly-available virtual
#                machine (HAVM). The script then migrates the HAVM to the
#                highest rated host, stops the HAVM to increase its memory, and
#                then starts the HAVM. Then the HAVM is removed, and the
#                cluster is removed.

# Connect to the VMM server.
$VMMServer = Get-VMMServer -ComputerName "VMMServer01.Contoso.com"

# Define the variables.
$Credential = Get-Credential
$Cluster = "Cluster01.Contoso.com"
$ParentGroup = Get-VMHostGroup | Where {$_.Path -eq "All Hosts"}
$VMHostGroup = New-VMHostGroup -Name "Cluster" -ParentHostGroup $ParentGroup
$Template = Get-Template | where {$_.Name -eq "Template01"}
$HAVMName = "HAVM01"
$Algorithm = "LoadBalance"
$Owner = "Contoso\Phyllis"
$VMJobGroup = [Guid]::NewGuid().ToString()

# Add the host cluster.
$VMHostCluster = Add-VMHostCluster -VMMServer $VMMServer -Credential $Credential -Name $Cluster -VMHostGroup $VMHostGroup -Description "" -RemoteConnectEnabled $true -Reassociate $false -RunAsynchronously -RemoteConnectPort 2179 -JobVariable "AddHostCluster"

# Pause the script to allow the cluster to finish adding.
While ($AddHostCluster.status -eq "Running")
{
   Start-Sleep -seconds 5
}

# Get the highest rated clustered host.
$VMHosts = Get-VMHostCluster $Cluster
$Ratings = @(Get-VMHostRating -PlacementGoal $Algorithm -Template $Template -VMHost $VMHosts.Nodes -DiskSpaceGB 5 -VMName $HAVMName -CPUPriority 0 -NetworkPriority 0 -DiskPriority 0 -MemoryPriority 10 | sort -Descending Rating)

If( $ratings[0].Rating -gt 0 )
{
   $VMHost = $ratings[0].VMHost
   $PathVolume = $ratings[0].PreferredVolume
   $Path = $pathvolume.Name
}

# Create a highly available virtual machine on the highest rated host.
New-VM -Template $Template -Name $HAVMName -Description "Highly Available Virtual machine" -VMHost $VMHost -Path $Path -JobGroup $VMJobGroup -RunAsynchronously -Owner $Owner -ComputerName "*" -OrgName "" -TimeZone 4 -JoinWorkgroup "WORKGROUP" -AnswerFile $null -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM -StartVM -JobVariable "NewHAVM"

# Pause the script while the HAVM is created.
While ($NewHAVM.status -eq "Running")
{
   Start-Sleep -seconds 5
}

# Migrate the HAVM to the highest rated host.
# Get and sort the host ratings for all the hosts in the host group.
$HostRatings = @(Get-VMHostRating -VM $HAVMName -VMHost $VMHosts.Nodes -IsMigration | where { $_.Rating -gt 0 } | Sort-Object -property Rating -descending)

If($HostRatings.Count -eq "0") { throw "No hosts meet the requirements." }

# If the highest rated host is not the same as the host on which
# the virtual machine is currently deployed, move the virtual
# machine to the highest rated host.

If ($HostRatings[0].VMHost -ne $VMHost)
{
   $VMRatedHost = $HostRatings[0].VMHost

   # Move the virtual machine.
   # In VMM 2008 R2, use the -UseCluster parameter to force a transfer of
   # the virtual machines by using quick migration.
   Write-Host "Moving $HAVMName to $VMRatedHost"
   Move-VM -VM $HAVMName -VMHost $VMRatedHost 
}

# If the highest rated host is the same as the host on which
# the virtual machine is currently deployed, move the virtual
# machine to the second-highest rated host.

Else 
{
   $VMRatedHost = $HostRatings[1].VMHost
   Write-Host "Moving $HAVMName to $VMRatedHost"
   Move-VM -VM $HAVMName -VMHost $VMRatedHost 
}

# Stop the HAVM and increase its memory, then restart the HAVM.
Stop-VM -VM $HAVMName
Set-VM -VM $HAVMName -MemoryMB 1024
Start-VM -VM $HAVMName

# Delete the HAVM.
Stop-VM -VM $HAVMName
Remove-VM -VM $HAVMName -Confirm

# Remove the cluster from VMM.
Remove-VMHostCluster -VMHostCluster $VMHostCluster -Confirm -Credential $Credential