다음을 통해 공유


서비스 라이브러리에 저장 한 다음 다시 배포 (스크립트)

 

적용 대상: System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

라이브러리에 배포 된 서비스에 저장 하 고 다시 라이브러리에서 저장 된 서비스를 배포할 수 있습니다. 라이브러리에는 서비스를 저장 하기 위해 저장 모든 라이브러리;에 해당 서비스에 대 한 가상 컴퓨터 서비스 정의 유지 하 고 서비스 라이브러리에 나타납니다. 각 가상 컴퓨터를 서비스에 저장된 하는 서비스를 다시 배포 하려면 배포 프로그램 VMM 환경입니다.

고 지 사항

다음 스크립트는 지정된 된 서비스에 대 한 모든 가상 컴퓨터를 가져옵니다는 최고 등급으로 라이브러리 서버에 저장 합니다. 저장 된 모든 가상 컴퓨터, 후 스크립트에는 서비스 라이브러리에 저장 된 있는지 확인할 수 있도록 일시 중지 합니다. 그런 다음 스크립트 각각 저장 된 가상 컴퓨터의 최적 정격된 호스트에 배포 하 여 서비스를 다시 배포 합니다. 특정 호스트에 가상 컴퓨터를 배포 하려는 경우 해당 호스트 이름에 대 한 $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  
}