DESCRIPTION

This PowerShell Workflow runbook connects to Azure using an Automation Run As account and Starts/Stops all VMScalesets (VMSS) in a resource group in-parallel. You could add a recurring schedule to this runbook to run it at a specific time.

REQUIRED

1. A AzureResourceGroup input parameter value that allows scoping VMSSs to a particular resource group. 

2. An Action to perform. Stop - to stop the VM ScaleSets; Start - to start the VM Scalesets

NOTES:

1. SubscriptionId - Is added as an variable under Shared Resources at the Automation Account level.

2. AzureCredential - Is added as an credential under Shared Resources at the Automation Account level.

 

 

PowerShell
Edit|Remove
workflow Stop-Start-VMSS 
{ 
    Param 
    (    
        [Parameter(Mandatory=$true)] 
        [String] 
        $AzureResourceGroup, 
        [Parameter(Mandatory=$true)][ValidateSet("Start","Stop")] 
        [String] 
        $Action 
    ) 
     
    $credential = Get-AutomationPSCredential -Name 'AzureCredential' 
    Login-AzureRmAccount -Credential $credential 
    $selectedsubscription = Get-AutomationVariable -Name 'AzureSubscriptionId' 
 
    Select-AzureRmSubscription -SubscriptionId $selectedsubscription 
 
    if($Action -eq "Stop") 
    { 
        Write-Output "Stopping VMSS in '$($AzureResourceGroup)' resource group"; 
        foreach -parallel ($name in (Get-AzureRmVmss -ResourceGroupName $AzureResourceGroup).Name) 
        { 
            Stop-AzureRmVmss -ResourceGroupName $AzureResourceGroup -VMScaleSetName $name -Force -Verbose 
        } 
    } 
    else 
    { 
        Write-Output "Starting VMSS in '$($AzureResourceGroup)' resource group"; 
        foreach -parallel ($name in (Get-AzureRmVmss -ResourceGroupName $AzureResourceGroup).Name) 
        { 
            Start-AzureRmVmss -ResourceGroupName $AzureResourceGroup -VMScaleSetName $name -Verbose 
        } 
    } 
}