Un servizio di archivio nella libreria e quindi ridistribuirla (Script)

 

Si applica a: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

È possibile archiviare un servizio distribuito alla libreria e ridistribuire un servizio archiviato dalla libreria. Per archiviare un servizio nella libreria, archiviare tutte le macchine virtuali per il servizio per la libreria. viene mantenuta la definizione del servizio e il servizio verrà visualizzato nella libreria. Per distribuire di nuovo un servizio archiviata, si distribuisce ogni macchina virtuale nel servizio per il VMM ambiente.

Dichiarazione di non responsabilità

Lo script seguente ottiene tutte le macchine virtuali per il servizio specificato e li archivia nel server di libreria con la classificazione più alta. Dopo che tutte le macchine virtuali vengono archiviate, lo script viene sospesa per consentire di verificare che il servizio è stato archiviato nella libreria. Quindi, lo script ridistribuisce il servizio distribuendo ogni le macchine virtuali archiviate all'host migliori. Se si desidera distribuire le macchine virtuali in un host specifico, è possibile fornire tale nome host per la variabile $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  
}