{"id":87701,"date":"2020-08-10T17:00:56","date_gmt":"2020-08-10T17:00:56","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=87701"},"modified":"2020-09-12T09:47:24","modified_gmt":"2020-09-12T09:47:24","slug":"start-stopping-virtual-machines-resource-group","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/start-stopping-virtual-machines-resource-group\/","title":{"rendered":"Start and stopping all Virtual Machines on a Resource Group"},"content":{"rendered":"<p>Some tasks on azure are easier if we automate them. The Azure Portal provides us the cloud shell, which we can use for this kind of automation.<\/p>\n<p>I was making some experiences with SQL Server Always On, so I created three VMs inside a resouce group. Every time I want to start some experiment I need to start all three VMs and, in the end, stop all three again.<\/p>\n<h3>Creating a Powershell script<\/h3>\n<p>This is not a complex task, but it&#8217;s not something we would like to type every time. Using <strong>Azure CLI<\/strong>, we have the following instructions available:<\/p>\n<p style=\"padding-left: 30px\"><strong>az account set<\/strong>: Choose the default subscription. If you have more than a single subscription, you would like <br \/>\n<strong>az vm list<\/strong> : List all VM&#8217;s and it&#8217;s possible to filter by resource group<br \/>\n<strong>az vm start\/stop<\/strong>: Start or stop the vm<\/p>\n<p>In order to start or stop the VMs, we can combine the <strong>CLI<\/strong> statements using powershell. They will be like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\" class=\"\">az vm start --ids $(az vm list --resource-group sqlagpreview --query \"[].id\" -o tsv)\r\n\r\naz vm stop --ids $(az vm list --resource-group sqlagpreview --query \"[].id\" -o tsv)\r\n<\/pre>\n<\/div>\n<p>\nOn these examples we are using the <strong>AZ VM LIST<\/strong> to retrieve the list of Ids of the VMs and use this list to start or stop the VMs.<\/p>\n<p style=\"padding-left: 30px\">&#8211;query extracts only the .id from the <strong>JSON<\/strong> object returned by the <strong>LIST<\/strong> statement<br \/>\n-o tsv specifies the format, tab separated values, for the Ids<\/p>\n<p>We can prepare the script to receive two parameters, the operation we will execute (start\/stop) and the name of the resource group. The first line of the script will be like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\" class=\"\">param($activity='start',[Parameter(Mandatory)]$resourceGroup)\r\n<\/pre>\n<\/div>\n<p>\nThe <strong>$activity<\/strong> parameter has a default value, while the <strong>$resourceGroup<\/strong> is required.<\/p>\n<p>The final powershell script will be this:<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff;overflow: auto;width: auto;border: solid gray;border-width: .1em .1em .1em .8em;padding: .2em .6em\">\n<pre style=\"margin: 0;line-height: 125%\" class=\"\">param($activity='start',[Parameter(Mandatory)]$resourceGroup)\r\n\r\naz account set --subscription \"MCT subscription\"\r\nif ($activity='start')\r\n{\r\n    az vm start --ids $(az vm list --resource-group $resourceGroup --query \"[].id\" -o tsv)\r\n}\r\nelse\r\n{\r\n    az vm stop --ids $(az vm list --resource-group $resourceGroup --query \"[].id\" -o tsv)\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<h3>Using the script on Azure<\/h3>\n<p>The Azure Portal has a feature called cloud shell, which we can use to run powershell commands and <strong>Azure CLI<\/strong> statements. However, how can we keep our script file available to be used in cloud shell?<\/p>\n<p>The secret is on the first time you access the cloud shell. This tool creates a blob storage specially for it, so you can store many scripts and files and become able to execute automated tasks very easily.<\/p>\n<p>The first time you start the cloud shell, it will ask you about the blob storage creation. You can accept the basic automatic settings or use the advanced option to customize everything.<\/p>\n<p>&nbsp;<\/p>\n<table>\n<tbody>\n<tr>\n<td><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-87706\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/startvm01.png\" alt=\"cloud shell storage basic\" width=\"464\" height=\"185\" \/><\/td>\n<td><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-87707\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/startvm02.png\" alt=\"cloud shell - advanced\" width=\"476\" height=\"188\" \/><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\nIn my case I need to choose the correct subscription using <strong>AZ ACCOUNT SET<\/strong>. You can remove this line if you don&#8217;t need it or you can create a third parameter for the subscription to make the script more flexible.<\/p>\n<p>Once we create the file locally, we can upload it using the cloud shell. The image bellow shows the upload button.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87709\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/startvm03.png\" alt=\"\" width=\"452\" height=\"106\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>In the cloud shell we are connected to our user space. Inside it, we have a folder called <strong>CloudDrive<\/strong> which is linked to the cloud shell storage.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-87710\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/startvm04.png\" alt=\"Cloud Drive Folder\" width=\"976\" height=\"146\" \/><\/p>\n<p>\nWe can create folders and save files inside the clouddrive folder or outside the clouddrive folder. Only what we create inside will be kept in our cloud shell storage. Anything created outside will be temporary and can disappear at any time.<\/p>\n<p>When uploading, the content will always be uploaded to our home folder (in my example, \/home\/dennes ). We will need to move the script file to the clouddrive folder using the following statement:<\/p>\n<p style=\"padding-left: 30px\"><strong><em>Move StartStop.ps1 clouddrive<\/em><\/strong><\/p>\n<p>If you would like to upload more than a simple file, probably is better to use directly the file share created by the cloud shell. You need to find the cloud storage using the Azure UI, locate the file share and use the UI to upload the files you need to the correct location.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-87711\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/startvm06.png\" alt=\"cloud shell file share\" width=\"645\" height=\"308\" \/><\/p>\n<p>After we have the file in the correct location, we can use the powershell script to help us. Once it&#8217;s located in the cloud shell storage it will be always there, available to help us with our work.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-87712\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2020\/07\/startvm05.png\" alt=\"script execution\" width=\"744\" height=\"283\" \/><\/p>\n<h3>\nConclusion<\/h3>\n<p>We can create many powershell scripts to help us with our activities on Azure. I created a repository to keep these scripts, at the moment it has only this script, but more will come. You can find the repository on\u00a0<a href=\"https:\/\/github.com\/DennesTorres\/powershellAzureScripts\">https:\/\/github.com\/DennesTorres\/powershellAzureScripts<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some tasks on azure are easier if we automate them. The Azure Portal provides us the cloud shell, which we can use for this kind of automation. I was making some experiences with SQL Server Always On, so I created three VMs inside a resouce group. Every time I want to start some experiment I&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[6810],"class_list":["post-87701","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/87701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=87701"}],"version-history":[{"count":11,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/87701\/revisions"}],"predecessor-version":[{"id":88179,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/87701\/revisions\/88179"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=87701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=87701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=87701"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=87701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}