Поделиться через


Использование настраиваемых свойств уровня компьютера для определения завершения работы и порядок запуска виртуальных машин в службе (скрипт)

 

Применимо к:System Center 2012 R2 Virtual Machine Manager, System Center 2012 - Virtual Machine Manager

Можно создавать и добавлять пользовательские свойства к объектам в System Center 2012 — Virtual Machine Manager (VMM). Для создания нового пользовательского свойства, используйте New-SCCustomProperty командлета. При создании или обновлении пользовательское свойство, можно определить, какие объекты свойства может применяться к с помощью AddMember параметра. Можно применить пользовательские свойства для виртуальных машин, шаблоны виртуальных машин, узлы, кластеров узлов, групп узлов, шаблоны служб, уровней компьютера и облаков. После создания пользовательского свойства и добавлена к объекту можно добавить значение пользовательского свойства и предпринять действия на основе значения пользовательского свойства объекта.

Отказ от ответственности

Следующий скрипт создает пользовательское свойство уровня объектов-компьютеров. Затем применяет значения пользовательского свойства, определяющие порядок запуска и завершения работы виртуальных машин на уровнях.

<#
  Description:   This script creates a custom property for computer tier objects.
                 The script then applies values to the custom properties on the
                 computer tiers. Based on property values, the script stops the 
                 virtual machines in the specified order, and then starts the 
                 virtual machines, also in the specified order.
#>

# Create custom properties for the computer tiers.
$CustomProp = New-SCCustomProperty -Name StopStartOrder -AddMember "ComputerTier"

# Get the computer tiers on which you want to set the shutdown and startup order.
$Service = Get-SCService -Name "NewService6"
$Tiers = Get-SCComputerTier -Service $Service
$TierNumber = $Tiers.count

# Set the shutdown/startup order custom property on the computer tiers.
$ComputerTier1 = Get-SCComputerTier -Service $Service | where {$_.Name -eq "Web Tier"}
Set-SCCustompropertyValue -CustomProperty $CustomProp -InputObject $ComputerTier1 -Value "1"
$ComputerTier2 = Get-SCComputerTier -Service $Service | where {$_.Name -eq "ComputerTier2"}
Set-SCCustompropertyValue -CustomProperty $CustomProp -InputObject $ComputerTier2 -Value "2"

# Stop the virtual machines in order before stopping the service.
$i = 1
While ($i -le $TierNumber)
{
   Get-SCComputerTier -Service $Service | where {$_.CustomProperty.Values -eq "$i"} | Get-SCVirtualMachine | Stop-SCVirtualMachine -Shutdown

   $i = $i+1
}

# Stop the service.
Stop-SCService -Service $Service

# Pause to ensure that the service is stopped.
Start-Sleep "30"

# Start the virtual machines in order before starting the service.
$i = 1
While ($i -le $TierNumber)
{
   Get-SCComputerTier -Service $Service | where {$_.CustomProperty.Values -eq "$i"} | Get-SCVirtualMachine | Start-SCVirtualMachine

   $i = $i+1
}

# Start the service.
Start-SCService -Service $Service