1
+ <# ########################################################
2
+
3
+ Author: Trevor Sullivan <[email protected] >
4
+ Description: This PowerShell script demonstrates how to manage the Microsoft Azure
5
+ Automation service using the Azure Resource Manager (ARM) PowerShell module.
6
+
7
+ COPYRIGHT NOTICE
8
+
9
+ This file is part of the "Microsoft Azure Automation" course, and is
10
+ copyrighted by Art of Shell LLC. This file may not be copied or distributed, without
11
+ written permission from an authorized member of Art of Shell LLC.
12
+ #########################################################>
13
+
14
+ # region Install Azure PowerShell Modules
15
+
16
+ $Result = Read-Host - Prompt ' Install Azure PowerShell modules? (y/n)' ;
17
+
18
+ if ($Result -eq ' y' ) {
19
+ # ## Install the official Microsoft Azure Resource Manager (ARM) PowerShell module
20
+ Install-Module - Name AzureRM - Scope CurrentUser - Force;
21
+
22
+ # ## Install the official Microsoft Azure Automation ISE Add-on
23
+ Install-Module - Name AzureAutomationAuthoringToolkit - Scope CurrentUser - Force;
24
+
25
+ # ## Install the Art of Shell Azure PowerShell Extensions
26
+ Install-Module - Name AzureExt - Scope CurrentUser - Force;
27
+ }
28
+
29
+ # endregion
30
+
31
+ # region Microsoft Azure Authentication
32
+
33
+ $Result = Read-Host - Prompt ' Authenticate to Microsoft Azure manually? (y/n)' ;
34
+
35
+ if ($Result -eq ' y' ) {
36
+ # ## Authenticate to Microsoft Azure using Azure Active Directory (AAD)
37
+ $AzureUsername = ' [email protected] ' ;
38
+ $AzureCredential = Get-Credential - Username $AzureUsername - Message ' Please enter your Microsoft Azure password.' ;
39
+ Add-AzureRmAccount - Credential $AzureCredential ;
40
+ }
41
+
42
+ # ## This command, part of Azure PowerShell Extensions, simplifies authentication
43
+ # ## and loads Intellisense + ISE Snippets all at once.
44
+ Start-AzureRm ;
45
+
46
+ # endregion
47
+
48
+ # region Azure Resource Manager (ARM) Resource Group
49
+ $Result = Read-Host - Prompt ' Create Azure Resource Manager (ARM) Resource Group? (y/n)' ;
50
+
51
+ if ($Result -eq ' y' ) {
52
+ $ResourceGroup = @ {
53
+ Name = ' ArtofShell-Automation' ;
54
+ Location = ' South Central US' ;
55
+ Force = $true ;
56
+ }
57
+ New-AzureRmResourceGroup @ResourceGroup ;
58
+ }
59
+ # endregion
60
+
61
+ # region Automation Account
62
+
63
+ Get-Command - Module AzureRM.Automation - Name * AutomationAccount* | Format-Table - AutoSize;
64
+
65
+ $Result = Read-Host - Prompt ' Create Azure Automation Account? (y/n)' ;
66
+
67
+ if ($Result -eq ' y' ) {
68
+ $AutomationAccount = @ {
69
+ ResourceGroupName = $ResourceGroup.Name ;
70
+ AutomationAccountName = ' ArtofShell-Automation' ;
71
+ Location = $ResourceGroup.Location ;
72
+ };
73
+ New-AzureRmAutomationAccount @AutomationAccount ;
74
+ }
75
+ # endregion
76
+
77
+ # region Getting Started with Azure Automation Runbooks
78
+
79
+ # ## Inspect commands related to Microsoft Azure Automation Runbooks
80
+ Get-Command - Module AzureRM.Automation - Name * runbook* , * webhook* ;
81
+
82
+ $Result = Read-Host - Prompt ' Import Azure Automation Runbook? (y/n)' ;
83
+
84
+ if ($Result -eq ' y' ) {
85
+ $Runbook = @ {
86
+ ResourceGroupName = $AutomationAccount.ResourceGroupName ;
87
+ AutomationAccountName = $AutomationAccount.AutomationAccountName ;
88
+ Path = ' {0}\02-RunbookExample.ps1' -f $PSScriptRoot ;
89
+ Name = ' DeleteAzureResourceGroups' ;
90
+ Type = ' PowerShell' ;
91
+ Published = $true ;
92
+ LogVerbose = $true ;
93
+ };
94
+ Import-AzureRmAutomationRunbook @Runbook ;
95
+ }
96
+
97
+ $Result = Read-Host - Prompt ' Start Azure Automation Runbook? (y/n)' ;
98
+
99
+ if ($Result -eq ' y' ) {
100
+ $Runbook = @ {
101
+ ResourceGroupName = $AutomationAccount.ResourceGroupName ;
102
+ AutomationAccountName = $AutomationAccount.AutomationAccountName ;
103
+ Name = ' DeleteAzureResourceGroups' ;
104
+ Parameters = @ {
105
+ CredentialName = ' AzureAdmin' ;
106
+ };
107
+ Wait = $true ;
108
+ };
109
+ $Job = Start-AzureRmAutomationRunbook @Runbook ;
110
+
111
+ # ## Retrieve the Runbook Job results
112
+ $RunbookJob = @ {
113
+ ResourceGroupName = $AutomationAccount.ResourceGroupName ;
114
+ AutomationAccountName = $AutomationAccount.AutomationAccountName ;
115
+ Id = $Job.JobId ;
116
+ }
117
+ Get-AzureRmAutomationJobOutput @RunbookJob ;
118
+ }
119
+
120
+ # endregion
121
+
122
+ # region Getting Started with Azure Automation Runbooks
123
+
124
+ # ## Inspect commands related to Microsoft Azure Automation DSC
125
+ Get-Command - Module AzureRM.Automation - Name * dsc* ;
126
+
127
+ # ## Import a Desired State Configuration (DSC) Document
128
+ $Result = Read-Host - Prompt ' Import DSC Configuration to Azure Automation? (y/n)' ;
129
+
130
+ if ($Result -eq ' y' ) {
131
+ $ImportDsc = @ {
132
+ ResourceGroupName = $AutomationAccount.ResourceGroupName ;
133
+ AutomationAccountName = $AutomationAccount.AutomationAccountName ;
134
+ SourcePath = ' {0}\ArtofShell.ps1' -f $PSScriptRoot ;
135
+ Description = ' This DSC Configuration contains the infrastructure configuration for the Art of Shell organization.' ;
136
+ Published = $true ;
137
+ Force = $true ;
138
+ };
139
+ Import-AzureRmAutomationDscConfiguration @ImportDsc ;
140
+
141
+ $CompilationJob = @ {
142
+ ResourceGroupName = $AutomationAccount.ResourceGroupName ;
143
+ AutomationAccountName = $AutomationAccount.AutomationAccountName ;
144
+ ConfigurationName = ' ArtofShell' ;
145
+ }
146
+ Start-AzureRmAutomationDscCompilationJob @CompilationJob ;
147
+
148
+ # region Deploy an Azure Resource Manager (ARM) JSON Template containing a DSC Node
149
+ $Deployment = @ {
150
+ ResourceGroupName = $ResourceGroup.Name # ## This is the Resource Group where the ARM JSON Template will be deployed into
151
+ Name = ' ArtofShell-DSC-VM' ; # ## This is the name of the Deployment object that will be created inside the Resource Group
152
+ TemplateFile = ' {0}\04-Azure Automation DSC Node.json' -f $PSScriptRoot ; # ## The URL to the publicly (anonymously) accessible ARM JSON Template file
153
+ TemplateParameterObject = @ { # ## These are the input parameters that are fed to the ARM Template
154
+ adminUsername = ' aos' ;
155
+ adminPassword = ' ILove!Aos!' ;
156
+ dnsLabelPrefix = ' artofshell-dsc' ;
157
+ }
158
+ Mode = ' Incremental' ; # ## You can perform an "incremental" deployment or a "complete" deployment,
159
+ # ## the latter of which wipes out all existing resources in the target Resource Group.
160
+ DeploymentDebugLogLevel = ' All' ;
161
+ Force = $true ;
162
+ }
163
+ New-AzureRmResourceGroupDeployment @Deployment ;
164
+ # endregion
165
+
166
+ # ## Register an Azure Virtual Machine
167
+ $NodeRegistration = @ {
168
+ ResourceGroupName = $AutomationAccount.ResourceGroupName ;
169
+ AutomationAccountName = $AutomationAccount.AutomationAccountName ;
170
+ AzureVMName = ' ArtofShellDSC' ;
171
+ NodeConfigurationName = ' ArtofShell.web01' ;
172
+ };
173
+ Register-AzureRmAutomationDscNode @NodeRegistration ;
174
+ }
175
+ # endregion
0 commit comments