Utilizzare le proprietà personalizzate su livelli di Computer per determinare l'arresto e l'ordine di avvio delle macchine virtuali in un servizio (Script)

 

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

È possibile creare e aggiungere proprietà personalizzate agli oggetti in System Center 2012 – Virtual Machine Manager (VMM). Per creare una nuova proprietà personalizzata, utilizzare il New-SCCustomProperty cmdlet. Quando si crea o aggiorna una proprietà personalizzata, è possibile determinare gli oggetti che la proprietà può essere applicata a utilizzando il AddMember parametro. È possibile applicare le proprietà personalizzate per le macchine virtuali, modelli di macchine virtuali, host, cluster host, gruppi host, modelli di servizio, livelli dei computer e cloud. Dopo aver creato una proprietà personalizzata e aggiunto a un oggetto, è possibile aggiungere un valore alla proprietà personalizzata e intraprendere azioni per l'oggetto in base al valore della proprietà personalizzata.

Dichiarazione di non responsabilità

Lo script seguente crea una proprietà personalizzata per gli oggetti di livello di computer. Quindi, si applica i valori per le proprietà personalizzate che determinano l'ordine di avvio e arresto delle macchine virtuali nei livelli.

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