Skip to content

Commit 75383b9

Browse files
authored
Merge pull request #282851 from blakedrumm/patch-1
Format the PowerShell examples to best practices 👔
2 parents 728e08c + e87150d commit 75383b9

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

articles/automation/learn/automation-tutorial-runbook-textual.md

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -111,29 +111,30 @@ Start by creating a simple [PowerShell Workflow runbook](../automation-runbook-t
111111

112112
You can either type code directly into the runbook, or you can select cmdlets, runbooks, and assets from the Library control and add them to the runbook with any related parameters. For this tutorial, you type code directly into the runbook.
113113

114-
Your runbook is currently empty with only the required `Workflow` keyword, the name of the runbook, and the braces that encase the entire workflow.
114+
Your runbook is currently empty with only the required `workflow` keyword, the name of the runbook, and the braces that encase the entire workflow.
115115

116116
```powershell
117-
Workflow MyFirstRunbook-Workflow
117+
workflow MyFirstRunbook-Workflow
118118
{
119119
}
120120
```
121121

122122
1. You can use the `Parallel` keyword to create a script block with multiple commands that will run concurrently. Enter the following code *between* the braces:
123123

124124
```powershell
125-
Parallel {
126-
Write-Output "Parallel"
127-
Get-Date
128-
Start-Sleep -s 3
129-
Get-Date
130-
}
131-
132-
Write-Output " `r`n"
133-
Write-Output "Non-Parallel"
134-
Get-Date
135-
Start-Sleep -s 3
136-
Get-Date
125+
parallel
126+
{
127+
Write-Output "Parallel"
128+
Get-Date
129+
Start-Sleep -Seconds 3
130+
Get-Date
131+
}
132+
133+
Write-Output " `r`n"
134+
Write-Output "Non-Parallel"
135+
Get-Date
136+
Start-Sleep -Seconds 3
137+
Get-Date
137138
```
138139

139140
1. Save the runbook by selecting **Save**.
@@ -189,16 +190,16 @@ You've tested and published your runbook, but so far it doesn't do anything usef
189190
```powershell
190191
workflow MyFirstRunbook-Workflow
191192
{
192-
$resourceGroup = "resourceGroupName"
193-
194-
# Ensures you do not inherit an AzContext in your runbook
195-
Disable-AzContextAutosave -Scope Process
196-
197-
# Connect to Azure with system-assigned managed identity
198-
Connect-AzAccount -Identity
199-
200-
# set and store context
201-
$AzureContext = Set-AzContext SubscriptionId "<SubscriptionID>"
193+
$resourceGroup = "resourceGroupName"
194+
195+
# Ensures you do not inherit an AzContext in your runbook
196+
Disable-AzContextAutosave -Scope Process
197+
198+
# Connect to Azure with system-assigned managed identity
199+
Connect-AzAccount -Identity
200+
201+
# set and store context
202+
$AzureContext = Set-AzContext -SubscriptionId "<SubscriptionID>"
202203
}
203204
```
204205

@@ -262,38 +263,42 @@ You can use the `ForEach -Parallel` construct to process commands for each item
262263
```powershell
263264
workflow MyFirstRunbook-Workflow
264265
{
265-
Param(
266-
[string]$resourceGroup,
267-
[string[]]$VMs,
268-
[string]$action
269-
)
270-
271-
# Ensures you do not inherit an AzContext in your runbook
272-
Disable-AzContextAutosave -Scope Process
273-
274-
# Connect to Azure with system-assigned managed identity
275-
Connect-AzAccount -Identity
276-
277-
# set and store context
278-
$AzureContext = Set-AzContext –SubscriptionId "<SubscriptionID>"
279-
280-
# Start or stop VMs in parallel
281-
if ($action -eq "Start") {
282-
ForEach -Parallel ($vm in $VMs)
283-
{
284-
Start-AzVM -Name $vm -ResourceGroupName $resourceGroup -DefaultProfile $AzureContext
285-
}
286-
}
287-
elseif ($action -eq "Stop") {
288-
ForEach -Parallel ($vm in $VMs)
289-
{
290-
Stop-AzVM -Name $vm -ResourceGroupName $resourceGroup -DefaultProfile $AzureContext -Force
291-
}
292-
}
293-
else {
294-
Write-Output "`r`n Action not allowed. Please enter 'stop' or 'start'."
295-
}
296-
}
266+
param
267+
(
268+
[string]$resourceGroup,
269+
[string[]]$VMs,
270+
[string]$action
271+
)
272+
273+
# Ensures you do not inherit an AzContext in your runbook
274+
Disable-AzContextAutosave -Scope Process
275+
276+
# Connect to Azure with system-assigned managed identity
277+
Connect-AzAccount -Identity
278+
279+
# set and store context
280+
$AzureContext = Set-AzContext -SubscriptionId "<SubscriptionID>"
281+
282+
# Start or stop VMs in parallel
283+
if ($action -eq "Start")
284+
{
285+
ForEach -Parallel ($vm in $VMs)
286+
{
287+
Start-AzVM -Name $vm -ResourceGroupName $resourceGroup -DefaultProfile $AzureContext
288+
}
289+
}
290+
elseif ($action -eq "Stop")
291+
{
292+
ForEach -Parallel ($vm in $VMs)
293+
{
294+
Stop-AzVM -Name $vm -ResourceGroupName $resourceGroup -DefaultProfile $AzureContext -Force
295+
}
296+
}
297+
else
298+
{
299+
Write-Output "`r`n Action not allowed. Please enter 'stop' or 'start'."
300+
}
301+
}
297302
```
298303
299304
1. If you want the runbook to execute with the system-assigned managed identity, leave the code as-is. If you prefer to use a user-assigned managed identity, then:

0 commit comments

Comments
 (0)