Author Archives: Jiafeng Bi

Start And Stop VMs By Azure Automation

1.Add the Azure Automation Account

1. Sign in to the Azure portal. Search for Automation Accounts. In the search results, select Automation Accounts.

2. On the next screen, click Add.

3. In the Add Automation Account pane, enter a name for your new Automation account in the Name field. You can’t change this name after it’s chosen.

If you have more than one subscription, use the Subscription field to specify the subscription to use for the new account.

For Resource group, enter or select a new or existing resource group.

For Location, select an Azure datacenter location.

For the Create Azure Run As account option, ensure that Yes is selected, and then click Create.

2. Import AzureRM,profile

1. From your Automation account, under Shared Resources, select Modules. In the search bar, enter the module name (for example, AzureRM.Profile).

2. On the PowerShell Module page, select Import to import the module into your Automation account.

3. Select I agree to update all of the Azure modules.

4. Wait for the installation to complete.

3. Create Runbook

1. Click Create a runbook.

2. Enter a name for the runbook and select its type. The runbook name must start with a letter and can contain letters, numbers, underscores, and dashes.

3. Copy the code to the code editor.

Workflow Stop-Start-AzureVM 
{ 
    Param 
    (    
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
        [String] 
        $AzureSubscriptionId, 
        [Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()] 
        [String] 
        $AzureVMList="All", 
        [Parameter(Mandatory=$true)][ValidateSet("Start","Stop")] 
        [String] 
        $Action 
    ) 
     
    $connectionName = "AzureRunAsConnection"
try
{
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
}
catch {
    if (!$servicePrincipalConnection)
    {
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
}
 
    if($AzureVMList -ne "All") 
    { 
        $AzureVMs = $AzureVMList.Split(",") 
        [System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs 
    } 
    else 
    { 
        $AzureVMs = (Get-AzureRmVM).Name 
        [System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs 
 
    } 
 
    foreach($AzureVM in $AzureVMsToHandle) 
    { 
        if(!(Get-AzureRmVM | ? {$_.Name -eq $AzureVM})) 
        { 
            throw " AzureVM : [$AzureVM] - Does not exist! - Check your inputs " 
        } 
    } 
 
    if($Action -eq "Stop") 
    { 
        Write-Output "Stopping VMs"; 
        foreach -parallel ($AzureVM in $AzureVMsToHandle) 
        { 
            Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Stop-AzureRmVM -Force 
        } 
    } 
    else 
    { 
        Write-Output "Starting VMs"; 
        foreach -parallel ($AzureVM in $AzureVMsToHandle) 
        { 
            Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Start-AzureRmVM 
        } 
    } 
}

4. Click Save and Publish (before publishing please have a test for the runbook).

4. Test the Runbook

Before you publish the runbook to make it available in production, you should test it to make sure that it works properly. Testing a runbook runs its Draft version and allows you to view its output interactively.

1. In the Azure portal, open your Automation account. Select Runbooks under Process Automation to open the list of runbooks. Click your runbook.

2. Click Edit.

3. Click Test pane to open the Test pane.

4. Enter the AZURESUBSCRIPTIONID, AZUREVMLIST and ACTION’s values. Click Start to start the test.

5. When the runbook job completes, close the Test pane to return to the canvas.

5. Create a Schedule in the Azure portal

1. From your Automation account, on the left-hand pane select Schedules under Shared Resources. On the Schedules page, select Add a schedule.

2. Choose Schedule.

3. Create a new schedule.

4. Select whether the schedule runs once or on a reoccurring schedule by selecting Once or Recurring. If you select Once, specify a start time and then select Create. If you select Recurring, specify a start time. For Recur every, select how often you want the runbook to repeat. Select by hour, day, week, or month.

  • If you select Week, the days of the week are presented for you to choose from. Select as many days as you want. The first run of your schedule will happen on the first day selected after the start time. For example, to choose a weekend schedule, select Saturday and Sunday.
  • If you select Month, you’re given different options. For the Monthly occurrences option, select either Month days or Week days. If you select Month days, a calendar appears so that you can choose as many days as you want. If you choose a date such as the 31st that doesn’t occur in the current month, the schedule won’t run. If you want the schedule to run on the last day, select Yes under Run on last day of month. If you select Week days, the Recur every option appears. Choose First, Second, Third, Fourth, or Last. Finally, choose a day to repeat on.

5. When you’re finished, select Create.

6. Choose Parameters and run settings.

7. Enter the AZURESUBSCRIPTIONID, AZUREVMLIST and ACTION’s values.

8. Click OK.

9.Finished. Follow the steps above to create a stop schedule.