Restart Azure VM/a list of VMs/All VMs,VMSS/Service Fabric Nodes

DESCRIPTION :

This PowerShell Workflow runbook connects to Azure using an Automation Credential and Starts/Stops a VM/a list of VMs/All VMs in a Subscription in-parallel. You can attach a recurring schedule to this runbook to run it at a specific time.REQUIRED1. SubscriptionName- pa

REQUIRED:

1. SubscriptionName- parameter that allows scoping Nodes to a particular Subscription.

2. ResourceGroupName- parameter to input the Resource Group which containes the Nodes

3. NodeToRestart –  parameter to input the Node name to Restart

4. Region – Optional parameter to choose Region (NA/China/Government)

NOTES:

1. AzureCredential – Is added as an credential under Shared Resources at the Automation Account level. This is used to Login to Azure using Login-AzureRmAccount

SCRIPT:

Param
(
[Parameter(Mandatory=$true)]
[String]
$SubscriptionName,
[Parameter(Mandatory=$true)]
[String]
$ResourceGroupName,
[Parameter(Mandatory=$true)]
[String]
$NodeToRestart, # Primary Node Should not be rebooted
[Parameter(Mandatory=$false)][ValidateSet(“China”,”NA”)]
[String]
$Region=”NA”
)

if($Region -eq “NA”)
{
$credential = Get-AutomationPSCredential -Name ‘AzureCredential’
Login-AzureRmAccount -Credential $credential
}
else
{
$credential = Get-AutomationPSCredential -Name ‘AzureCNCredential’
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $credential
}

Select-AzureRmSubscription -SubscriptionName $SubscriptionName

$VMSSInstances = (Get-AzureRmVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $NodeToRestart).InstanceId
foreach ($VMSSInstance in $VMSSInstances)
{
Write-Output ” Restarting instance [$VMSSInstance] in VMSS: [$NodeToRestart]”
Restart-AzureRmVmss -ResourceGroupName $ResourceGroupName -VMScaleSetName $NodeToRestart -InstanceId $VMSSInstance -Verbose -ErrorAction Stop
Sleep -Seconds 10
}

Be the first to comment

Leave a Reply

Your email address will not be published.


*