{"id":439,"date":"2019-10-29T20:33:11","date_gmt":"2019-10-30T02:33:11","guid":{"rendered":"http:\/\/draith.com\/?p=439"},"modified":"2019-10-29T20:33:11","modified_gmt":"2019-10-30T02:33:11","slug":"poshazurelab-starting-azure-machines-in-order","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=439","title":{"rendered":"PoshAzurelab &#8211; starting azure machines in order"},"content":{"rendered":"\n\n\n<h2>aka &#8211; please be careful of asking me something<\/h2>\n\n\n\n<p>Recently a friend sent me an email asking how to start a series of machines in Azure in a certain order.\u00a0 These particular machines were all used for labs and demos &#8211; some of them needed to be started and running before others.\u00a0 This is probably something that a lot of people can relate to; imagine needing to start domain controllers before bringing up SQL servers before bringing up ConfigMgr servers before starting demo client machines..\u00a0 You get the idea.<\/p>\n<p>So, even though we got something for my friend up and running, I decided to go ahead and <a href=\"https:\/\/github.com\/donnietaylor\/PoshAzureLab\">build a repo<\/a> for this.\u00a0 The idea right now is that this repo will\u00a0 start machines in order, but I will expand it later to also stop them.\u00a0 Eventually I will expand it to other resources that have a start\/stop action.\u00a0 Thanks a lot, Shaun.<\/p>\n<p>The basics of the current repo are 2 files &#8211; start-azurelab.ps1 and config.json.\u00a0 Start-AzureLab performs the actual heavy lifting, but the important piece is really config.json.\u00a0 Let&#8217;s examine the basic one:<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">{\n    \"TenantId\": \"fffffff-5555-4444-fake-tenantid\",\n    \"subscriptions\": [\n        {\n            \"subscription_name\": \"Pay-As-You-Go\",\n            \"resource_groups\": [\n                {\n                    \"resource_group_name\": \"AzureLabStartup001\",\n                    \"data\": {\n                        \"virtual_machines\": [\n                            {\n                                \"vm_name\": \"Server001\",\n                                \"wait\":false,\n                                \"delay_after_start\": \"1\"\n                            },\n                            {\n                                \"vm_name\": \"Server002\",\n                                \"wait\":false,\n                                \"delay_after_start\": \"2\"\n                            }\n                        ]\n                    }\n                }\n            ]\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>The first thing you see is the &#8216;tenantid&#8217;.\u00a0 You will want to replace this with your personal tenant id from Azure.\u00a0 To find your tenant id, click help and show diagnostics:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"847\" height=\"321\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2019\/10\/mjDzO.png\" alt=\"\" class=\"wp-image-440\" srcset=\"\/wp-content\/uploads\/2019\/10\/mjDzO.png 847w, \/wp-content\/uploads\/2019\/10\/mjDzO-300x114.png 300w, \/wp-content\/uploads\/2019\/10\/mjDzO-768x291.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure><\/div>\n\n\n\n<p>Next, you can see the subscription name.\u00a0 If you are familiar with JSON, you can also see that you can enter multiple subscriptions &#8211; more on this later.\u00a0 Enter your subscription name (not ID).\u00a0<\/p>\n<p>Next, you can see the Resource_Group_Name.\u00a0 Again, you can have multiple resource groups, but in this simple example there is only one.\u00a0 Put in your specific resource group name.\u00a0 Now we get down to the meat of the config.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">\"data\": {\n         \"virtual_machines\": [\n          {\n                \"vm_name\": \"Server001\",\n                \"wait\":false,\n                \"delay_after_start\": \"1\"\n           },\n           {\n                 \"vm_name\": \"Server002\",\n                 \"wait\":false,\n                 \"delay_after_start\": \"2\"\n            }\n     ]\n}<\/code><\/pre>\n\n\n\n<p>This is where we put in the VM names.\u00a0 Place them in the order you want them to start.\u00a0 The two other properties have special meaning &#8211; &#8220;wait&#8221; and &#8220;delay_after_start&#8221;.\u00a0 Let&#8217;s look at &#8220;wait&#8221;.<\/p>\n<p>When you start an Azure VM with start-azvm, there is a property you specify that tells the cmdlet to either start the VM and keep checking until the machine is running, or start the VM and immediately return back to the terminal.\u00a0 If you set the &#8220;wait&#8221; property in this config to &#8216;false&#8217;, then when that VM is started the script will immediately return back to the terminal and process the next instruction.\u00a0 This is important when dealing with machines that take a bit to start\u00a0 &#8211; i.e. large Windows servers.<\/p>\n<p>Now &#8211; combine the &#8220;wait&#8221; property with the &#8220;delay_after_start&#8221; property, and you have capability to really customize your lab start ups.\u00a0 For example, maybe you have a domain controller, a DNS server , and a SQL server in your lab, plus a bunch of client machines.\u00a0 You want the DC to come up first, and probably want to wait 20-30 seconds after it&#8217;s running to make sure your DC services are all up and running.\u00a0 Same with the DNS and SQL boxes, but maybe you don&#8217;t need to wait so long after the box is running.\u00a0 The client machines, though &#8211; you don&#8217;t need to wait at all, and you might set the delay to 0 or 1.\u00a0 Just get them up and running and be done with it.<\/p>\n<p>So now we can completely control how our lab starts, but say you have multiple resource groups or multiple subscriptions to deal with.\u00a0 The JSON can be configured to allow for both!\u00a0 In the github repo, there are 2 additional examples &#8211; config-multigroup.json and config-multisub.json.\u00a0 \u00a0 A fully baked JSON might look like this:<\/p>\n<p>\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">{\n    \"TenantId\": \"ff744de5-59a0-4b5b-a181-54d5efbb088b\",\n    \"subscriptions\": [\n        {\n            \"subscription_name\": \"Pay-As-You-Go\",\n            \"resource_groups\": [\n                {\n                    \"resource_group_name\": \"AzureLabStartup001\",\n                    \"data\": {\n                        \"virtual_machines\": [\n                            {\n                                \"vm_name\": \"Server001\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"1\"\n                            },\n                            {\n                                \"vm_name\": \"Server002\",\n                                \"wait\":false,\n                                \"delay_after_start\": \"2\"\n                            }\n                        ]\n                    }\n                },\n                {\n                    \"resource_group_name\": \"AzureLabStartup002\",\n                    \"data\": {\n                        \"virtual_machines\": [\n                            {\n                                \"vm_name\": \"Server001\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"1\"\n                            },\n                            {\n                                \"vm_name\": \"Server002\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"2\"\n                            }\n                        ]\n                    }\n                }\n            ]\n        },\n        {\n            \"subscription_name\": \"SubID2\",\n            \"resource_groups\": [\n                {\n                    \"resource_group_name\": \"AzureLabStartup001\",\n                    \"data\": {\n                        \"virtual_machines\": [\n                            {\n                                \"vm_name\": \"Server001\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"1\"\n                            },\n                            {\n                                \"vm_name\": \"Server002\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"2\"\n                            }\n                        ]\n                    }\n                },\n                {\n                    \"resource_group_name\": \"AzureLabStartup002\",\n                    \"data\": {\n                        \"virtual_machines\": [\n                            {\n                                \"vm_name\": \"Server001\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"1\"\n                            },\n                            {\n                                \"vm_name\": \"Server002\",\n                                \"wait\":true,\n                                \"delay_after_start\": \"2\"\n                            }\n                        ]\n                    }\n                }\n            ]\n        }\n    ]\n}<\/code><\/pre>\n\n\n\n<p>So there you have it &#8211; hope you enjoy the repo, and keep checking back because I will be updating it to add more features and improve the actual code.\u00a0 Hope this helps!\u00a0\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,16],"class_list":["post-439","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-azure","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=439"}],"version-history":[{"count":0,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/439\/revisions"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}