{"id":24,"date":"2020-12-10T18:12:06","date_gmt":"2020-12-10T10:12:06","guid":{"rendered":"http:\/\/blog.bijiafeng.com\/?p=24"},"modified":"2024-03-19T15:33:51","modified_gmt":"2024-03-19T07:33:51","slug":"start-and-stop-vms-by-azure-automation","status":"publish","type":"post","link":"https:\/\/blog.bijiafeng.com\/?p=24","title":{"rendered":"Start And Stop VMs By Azure Automation"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">1.Add the Azure Automation Account<\/h1>\n\n\n\n<p>1. Sign in to the Azure portal. Search for <strong>Automation Accounts<\/strong>. In the search results, select <strong>Automation Accounts<\/strong>.<\/p>\n\n\n\n\n\n<p>2. On the next screen, click <strong>Add<\/strong>.<\/p>\n\n\n\n\n\n<p>3. In the <strong>Add<\/strong> <strong>Automation Account<\/strong> pane, enter a name for your new Automation account in the <strong>Name<\/strong> field. You can&#8217;t change this name after it&#8217;s chosen.<\/p>\n\n\n\n<p>If you have more than one subscription, use the <strong>Subscription<\/strong> field to specify the subscription to use for the new account.<\/p>\n\n\n\n<p>For <strong>Resource<\/strong> <strong>group<\/strong>, enter or select a new or existing resource group.<\/p>\n\n\n\n<p>For <strong>Location<\/strong>, select an Azure datacenter location.<\/p>\n\n\n\n<p>For the <strong>Create Azure Run As account<\/strong> option, ensure that <strong>Yes<\/strong> is selected, and then click <strong>Create<\/strong>.<\/p>\n\n\n\n\n\n<h1 class=\"wp-block-heading\">2. Import AzureRM,profile<\/h1>\n\n\n\n<p>1. From your <strong>Automation<\/strong> <strong>account<\/strong>, under <strong>Shared Resources<\/strong>, select <strong>Modules<\/strong>. In the search bar, enter the module name (for example, AzureRM.Profile).<\/p>\n\n\n\n\n\n<p>2. On the <strong>PowerShell Module<\/strong> page, select <strong>Import<\/strong> to import the module into your Automation account.<\/p>\n\n\n\n\n\n<p>3. Select <strong>I agree to update all of the Azure modules<\/strong>.<\/p>\n\n\n\n\n\n<p>4. Wait for the installation to complete.<\/p>\n\n\n\n\n\n<h1 class=\"wp-block-heading\">3. Create Runbook<\/h1>\n\n\n\n<p>1. Click <strong>Create a runbook<\/strong>.<\/p>\n\n\n\n\n\n<p>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.<\/p>\n\n\n\n\n\n<p>3. Copy the code to the code editor.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Workflow Stop-Start-AzureVM \n{ \n    Param \n    (    \n        &#91;Parameter(Mandatory=$true)]&#91;ValidateNotNullOrEmpty()] \n        &#91;String] \n        $AzureSubscriptionId, \n        &#91;Parameter(Mandatory=$true)]&#91;ValidateNotNullOrEmpty()] \n        &#91;String] \n        $AzureVMList=\"All\", \n        &#91;Parameter(Mandatory=$true)]&#91;ValidateSet(\"Start\",\"Stop\")] \n        &#91;String] \n        $Action \n    ) \n     \n    $connectionName = \"AzureRunAsConnection\"\ntry\n{\n    # Get the connection \"AzureRunAsConnection \"\n    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         \n\n    \"Logging in to Azure...\"\n    Add-AzureRmAccount `\n        -ServicePrincipal `\n        -TenantId $servicePrincipalConnection.TenantId `\n        -ApplicationId $servicePrincipalConnection.ApplicationId `\n        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint \n}\ncatch {\n    if (!$servicePrincipalConnection)\n    {\n        $ErrorMessage = \"Connection $connectionName not found.\"\n        throw $ErrorMessage\n    } else{\n        Write-Error -Message $_.Exception\n        throw $_.Exception\n    }\n}\n \n    if($AzureVMList -ne \"All\") \n    { \n        $AzureVMs = $AzureVMList.Split(\",\") \n        &#91;System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs \n    } \n    else \n    { \n        $AzureVMs = (Get-AzureRmVM).Name \n        &#91;System.Collections.ArrayList]$AzureVMsToHandle = $AzureVMs \n \n    } \n \n    foreach($AzureVM in $AzureVMsToHandle) \n    { \n        if(!(Get-AzureRmVM | ? {$_.Name -eq $AzureVM})) \n        { \n            throw \" AzureVM : &#91;$AzureVM] - Does not exist! - Check your inputs \" \n        } \n    } \n \n    if($Action -eq \"Stop\") \n    { \n        Write-Output \"Stopping VMs\"; \n        foreach -parallel ($AzureVM in $AzureVMsToHandle) \n        { \n            Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Stop-AzureRmVM -Force \n        } \n    } \n    else \n    { \n        Write-Output \"Starting VMs\"; \n        foreach -parallel ($AzureVM in $AzureVMsToHandle) \n        { \n            Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Start-AzureRmVM \n        } \n    } \n}\n<\/code><\/pre>\n\n\n\n<p>4. Click <strong>Save<\/strong> and <strong>Publish <\/strong>(before publishing please have a test for the runbook).<\/p>\n\n\n\n\n\n<h1 class=\"wp-block-heading\">4. Test the Runbook<\/h1>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>1. In the Azure portal, open your Automation account. Select <strong>Runbooks<\/strong> under <strong>Process Automation<\/strong> to open the list of runbooks. Click your runbook.<\/p>\n\n\n\n\n\n<p>2. Click <strong>Edit<\/strong>.<\/p>\n\n\n\n\n\n<p>3. Click <strong>Test pane<\/strong> to open the Test pane.<\/p>\n\n\n\n\n\n<p>4. Enter the AZURESUBSCRIPTIONID, AZUREVMLIST and ACTION\u2019s values. Click <strong>Start<\/strong> to start the test.<\/p>\n\n\n\n\n\n<p>5. When the runbook job completes, close the Test pane to return to the canvas.<\/p>\n\n\n\n\n\n<h1 class=\"wp-block-heading\">5. Create a Schedule in the Azure portal<\/h1>\n\n\n\n<p>1. From your Automation account, on the left-hand pane select <strong>Schedules<\/strong> under <strong>Shared Resources<\/strong>. On the <strong>Schedules<\/strong> page, select <strong>Add a schedule<\/strong>.<\/p>\n\n\n\n\n\n<p>2. Choose <strong>Schedule<\/strong>.<\/p>\n\n\n\n\n\n<p>3. <strong>Create a new schedule<\/strong>. <img loading=\"lazy\" decoding=\"async\" width=\"626\" height=\"208\" src=\"\"><\/p>\n\n\n\n<p>4. Select whether the schedule runs once or on a reoccurring schedule by selecting <strong>Once<\/strong> or <strong>Recurring<\/strong>. If you select <strong>Once<\/strong>, specify a start time and then select <strong>Create<\/strong>. If you select <strong>Recurring<\/strong>, specify a start time. For <strong>Recur<\/strong> <strong>every<\/strong>, select how often you want the runbook to repeat. Select by hour, day, week, or month.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If you select <strong>Week<\/strong>, 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.<\/li><li>If you select <strong>Month<\/strong>, you&#8217;re given different options. For the <strong>Monthly<\/strong> <strong>occurrences<\/strong> option, select either <strong>Month<\/strong> <strong>days<\/strong> or <strong>Week<\/strong> <strong>days<\/strong>. If you select <strong>Month<\/strong> <strong>days<\/strong>, 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&#8217;t occur in the current month, the schedule won&#8217;t run. If you want the schedule to run on the last day, select <strong>Yes<\/strong> under <strong>Run on last day of month<\/strong>. If you select <strong>Week days<\/strong>, the <strong>Recur every<\/strong> option appears. Choose <strong>First, Second, Third, Fourth<\/strong>, or <strong>Last<\/strong>. Finally, choose a day to repeat on.<\/li><\/ul>\n\n\n\n\n\n<p>5. When you&#8217;re finished, select Create.<\/p>\n\n\n\n<p>6. Choose <strong>Parameters and run settings<\/strong>.<\/p>\n\n\n\n\n\n<p>7. Enter the AZURESUBSCRIPTIONID, AZUREVMLIST and ACTION\u2019s values.<\/p>\n\n\n\n\n\n<p>8. Click <strong>OK<\/strong>.<\/p>\n\n\n\n\n\n<p>9.Finished. Follow the steps above to create a stop schedule.<\/p>\n\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t change this name after [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-24","post","type-post","status-publish","format-standard","hentry","category-cloud"],"_links":{"self":[{"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=\/wp\/v2\/posts\/24","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24"}],"version-history":[{"count":2,"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":27,"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=\/wp\/v2\/posts\/24\/revisions\/27"}],"wp:attachment":[{"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bijiafeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}