Compartir a través de


Un servicio de almacén de la biblioteca y, a continuación, volver a implementarlo (Script)

 

Se aplica a: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

Puede almacenar un servicio implementado a la biblioteca y volver a implementar un servicio almacenado en la biblioteca. Para almacenar un servicio en la biblioteca, almacena todas las máquinas virtuales de ese servicio en la biblioteca. se conserva la definición de servicio y el servicio aparece en la biblioteca. Para volver a implementar un servicio almacenado, implementa cada máquina virtual en el servicio para su VMM entorno.

Declinación de responsabilidades

La siguiente secuencia de comandos obtiene todas las máquinas virtuales para el servicio especificado y los almacena en el servidor de biblioteca con la clasificación más alta. Después de que todas las máquinas virtuales se almacenan, se detiene la secuencia de comandos para que pueda comprobar que el servicio se ha almacenado en la biblioteca. A continuación, la secuencia de comandos volver a implementa el servicio mediante la implementación de cada una de las máquinas virtuales almacenadas en el host mejor calificado. Si desea implementar las máquinas virtuales en un host específico, puede proporcionar ese nombre de host para la variable $VMHost.

  
<#  
  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  
}