You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/devtest-labs/start-machines-use-automation-runbooks.md
+33-18Lines changed: 33 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,34 @@
1
1
---
2
-
title: Start machines using Automation runbooks
3
-
description: Learn how to start virtual machines in a lab in Azure DevTest Labs by using Azure Automation runbooks.
2
+
title: Start machines in order with Azure Automation runbooks
3
+
description: Learn how to start virtual machines in order by using Azure Automation runbooks in Azure DevTest Labs.
4
4
ms.topic: how-to
5
-
ms.date: 06/26/2020
5
+
ms.date: 03/15/2022
6
6
ms.custom: devx-track-azurepowershell
7
7
---
8
8
9
-
# Start virtual machines in a lab in order by using Azure Automation runbooks
10
-
The [autostart](devtest-lab-set-lab-policy.md#set-autostart) feature of DevTest Labs allows you to configure VMs to start automatically at a specified time. However, this feature doesn't support machines to start in a specific order. There are several scenarios where this type of automation would be useful. One scenario is where a Jumpbox VM in a lab is the access point to the other VMs. The Jumpbox VM must start before the other VMs. This article shows you how to set up an Azure Automation account with a PowerShell runbook that executes a script. The script uses tags on VMs in the lab to allow you to control the startup order without having to change the script.
9
+
# Start DevTest Labs VMs in order with Azure Automation runbooks
11
10
12
-
## Setup
13
-
In this example, VMs in the lab need to have the tag **StartupOrder** added with the appropriate value, such as 0, 1, 2. Designate any machine that doesn't need starting as -1.
11
+
This article explains how to start up DevTest Labs virtual machines (VMs) in a specific order by using a PowerShell runbook in Azure Automation. The PowerShell script uses tags on lab VMs, so you can change the startup order without having to change the script.
14
12
15
-
## Create an Azure Automation account
16
-
Create an Azure Automation account by following instructions in [this article](../automation/automation-create-standalone-account.md). Choose the **Run As Accounts** option when creating the account. Once the automation account is created, open the **Modules** page, and select **Update Azure Modules** on the menu bar. The default modules are several versions old and without the update the script may not function.
13
+
The DevTest Labs [autostart](devtest-lab-set-lab-policy.md#set-autostart) feature can configure lab VMs to start automatically at a specified time. However, sometimes you might want lab VMs to start in a specific order. For example, if a jumpbox VM in a lab is the access point to the other VMs, the jumpbox VM must start before the other VMs.
17
14
18
-
## Add a runbook
19
-
Now, to add a runbook to the automation account, select **Runbooks** on the left menu. Select **Add a runbook** on the menu, and follow instructions to [create a PowerShell runbook](../automation/learn/powershell-runbook-managed-identity.md).
15
+
## Prerequisites
20
16
21
-
## PowerShell script
22
-
The following script takes the subscription name, the lab name as parameters. The flow of the script is to get all the VMs in the lab, and then parse out the tag information to create a list of the VM names and their startup order. The script walks through the VMs in order and starts the VMs. If there are multiple VMs in a specific order number, they start asynchronously using PowerShell jobs. For those VMs that don’t have a tag, set startup value to be the last (10). Those machines start last, by default. If you don't want the VM to be auto started, set the tag value to 11, and the script will ignore the VM.
17
+
- Apply the tag **StartupOrder** to all lab VMs with an appropriate startup value, 0 through 10. Designate any machines that don't need starting as -1.
18
+
19
+
- Create an Azure Automation account by following instructions in [Create a standalone Azure Automation account](/azure/automation/automation-create-standalone-account). Choose the **Run As Accounts** option when creating the account.
20
+
21
+
## Create the PowerShell runbook
22
+
23
+
1. On the **Overview** page for the Automation Account, select **Runbooks** from the left menu.
24
+
1. On the **Runbooks** page, select **Create a runbook**.
25
+
1. Follow the instructions in [Create an Automation PowerShell runbook using managed identity](../automation/learn/powershell-runbook-managed-identity.md) to create a PowerShell runbook. Populate the runbook with the following PowerShell script.
26
+
27
+
### PowerShell script
28
+
29
+
The following script takes the subscription name and the lab name as parameters. The script gets all the VMs in the lab and parses their tag information to create a list of VM names and their startup order. The script walks through the list in order and starts the VMs.
30
+
31
+
If there are multiple VMs in a specific order number, those VMs start asynchronously using PowerShell jobs. VMs that don't have a tag have their startup value set to 10 and start last by default. The script ignores any VMs that have tag values other than 0 through 10.
# Get the StartupOrder tag, if missing set to be run last (10)
58
+
# Get the StartupOrder tag. If missing, set to start up last (10).
50
59
ForEach ($vm in $AllVMs) {
51
60
if ($vm.Tags) {
52
61
if ($vm.Tags['StartupOrder']) {
@@ -119,10 +128,16 @@ While ($current -le 10) {
119
128
}
120
129
```
121
130
122
-
## Create a schedule
123
-
To have this script execute daily, [create a schedule](../automation/shared-resources/schedules.md#create-a-schedule) in the automation account. Once the schedule is created, [link it to the runbook](../automation/shared-resources/schedules.md#link-a-schedule-to-a-runbook).
131
+
## Alternatives
132
+
133
+
- To run this script daily, [create a schedule](../automation/shared-resources/schedules.md#create-a-schedule) in the Automation Account, and [link the schedule to the runbook](../automation/shared-resources/schedules.md#link-a-schedule-to-a-runbook).
124
134
125
-
In a large-scale situation that has multiple subscriptions with multiple labs, store the parameter information in a file for different labs. Pass the file to the script instead of passing the individual parameters. The script must be modified, but the core execution is the same. While this sample uses the Azure Automation to execute the PowerShell script, there are other options like using a task in a Build/Release pipeline.
135
+
- In an enterprise scenario that has several subscriptions with multiple labs, you can store the parameter information in a file for different labs and subscriptions. Pass the file to the script instead of passing the individual parameters.
136
+
137
+
- This example uses Azure Automation to run the PowerShell script, but you can also use other options, like a task in a build/release pipeline.
126
138
127
139
## Next steps
128
-
See the following article to learn more about Azure Automation: [An introduction to Azure Automation](../automation/automation-intro.md).
140
+
141
+
-[What is Azure Automation?](/azure/automation/automation-intro)
142
+
-[Start up lab virtual machines automatically](devtest-lab-auto-startup-vm.md)
143
+
-[Use command-line tools to start and stop Azure DevTest Labs virtual machines](use-command-line-start-stop-virtual-machines.md)
0 commit comments