Export (0) Print
Expand All

Store a Service in the Library and then Re-Deploy It (Script)

Updated: November 1, 2013

Applies To: System Center 2012 - Virtual Machine Manager, System Center 2012 R2 Virtual Machine Manager

You can store a deployed service to the library, and re-deploy a stored service from the library. To store a service to the library, you store all of the virtual machines for that service to the library; the service definition is retained, and the service appears in the library. To re-deploy a stored service, you deploy each virtual machine in the service to your VMM environment.

Disclaimer

The following script gets all virtual machines for the specified service, and stores them to the library server with the highest rating. After all of the virtual machines are stored, the script pauses to allow you to verify that the service has been stored in the library. Then, the script re-deploys the service by deploying each of the stored virtual machines to the best-rated host. If you want to deploy the virtual machines on a specific host, you can provide that host name for the $VMHost variable.


<#
  Description:   This script stores all of the virtual machines for the specified
                 service to the best-rated library server. The script then pauses.
                 When it resumes, the script re-deploys each of the virtual machines
                 for the service to the best-rated host, thereby re-dedeploying the
                 service.
#>

# Get the service you want to store in the library.
$Service = Get-SCService -Name "Service01"

# Get all the virtual mnachines for the service.
$VMs = Get-SCVirtualMachine -Service $Service

# Get the library server with the highest rating.
$LibServers = @(Get-SCLibraryServer)
$RatedLibServers = Get-SCLibraryRating -LibraryServer $LibServers | where {$_.Rating -gt 0} | sort -property rating -descending
If ($RatedLibServers.Count -eq 0) {throw "No library servers meet the placement requirements."}
$LibServer = $RatedLibServers[0].Library
$SharePath = "\\$LibServer\Services"

# Store all virtual machines in the service to the library server.
ForEach ($VM in $VMs)
{
   If ($_.Status -eq "Running")
   {
      Stop-SCVirtualMachine -VM $VM
   }

   Else
   {
      Save-SCVirtualMachine -VM $VM -LibraryServer $LibServer -SharePath $SharePath -RunAsynchronously -JobVariable "StoringVMS"
   }
}

# Display progress of storing virtual machines.
While ($StoringVMs.status -eq "Running")
{
   Write-Progress -Activity "Storing virtual machines" -Status $StoringVMs.CurrentStep
   Start-Sleep -Seconds 5
}

# Ensure that the service is stored in the library before continuing.
While ($Service.DeploymentState -ne "Stored")
{
   Start-Sleep -Seconds 5
}

# Re-deploy the service.

ForEach ($VM in $VMs)
{
   # Get the host with the highest rating for the virtual machine.
   $VMHosts = Get-SCVMHost
   $HostRatings = @(Get-SCVMHostRating -VMHost $VMHosts -VM $VM | where {$_.Rating -gt 0} | sort -Property Rating -Descending)
   If ($HostRatings.Count -eq 0) {throw "No hosts meet the placement requirements."}
   $VMHost = $HostRatings[0].vmhost

   # Deploy the virtual machine.
   Write-Host "Deploying virtual machine $VM on host $VMHost."
   Move-SCVirtualMachine -VMHost $VMHost -VM $VM -Path "D:\VirtualMachinePath" -StartVMOnTarget -RunAsynchronously
}

-----
For additional resources, see Information and Support for System Center 2012.

Tip: Use this query to find online documentation in the TechNet Library for System Center 2012. For instructions and examples, see Search the System Center 2012 Documentation Library.
-----
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft