Skip to content

Commit 9a0bc90

Browse files
committed
Initial commit
0 parents  commit 9a0bc90

7 files changed

+535
-0
lines changed

01-Getting Started.ps1

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<#########################################################
2+
3+
Author: Trevor Sullivan <[email protected]>
4+
Description: This PowerShell script demonstrates how to get started with
5+
Azure Automation using the Microsoft Azure 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+
### Install the Microsoft Azure Resource Manager (ARM) PowerShell module
15+
Install-Module -Name AzureRM -Scope CurrentUser -Force;
16+
17+
### Install the Azure PowerShell Extensions (developed by Art of Shell)
18+
Install-Module -Name AzureExt -Scope CurrentUser -Force;
19+
20+
### Install the Microsoft Azure Automation Authoring Toolkit (for PowerShell ISE)
21+
Install-Module -Name AzureAutomationAuthoringToolkit -Scope CurrentUser -Force;
22+
23+
### Import the Azure Automation Authoring Toolkit
24+
Import-Module -Name AzureAutomationAuthoringToolkit -Force;
25+
26+
### Inspect the commands for the Azure Automation module
27+
Get-Command -Module AzureRM.Automation;
28+
29+
### Authenticate to Microsoft Azure using Azure Active Directory (AAD)
30+
$AzureUsername = '[email protected]';
31+
$AzureCredential = Get-Credential -Username $AzureUsername -Message 'Please enter your Microsoft Azure password.';
32+
Add-AzureRmAccount -Credential $AzureCredential;
33+
34+
### Create a new Azure Resource Manager (ARM) Resource Group
35+
$ResourceGroup = @{
36+
Name = 'ArtofShell-Automation';
37+
Location = 'West US';
38+
Force = $true;
39+
}
40+
New-AzureRmResourceGroup @ResourceGroup;
41+
42+
### Create a new Azure Automation Account
43+
$AutomationAccount = @{
44+
ResourceGroupName = $ResourceGroup.Name;
45+
Name = 'ArtofShell';
46+
Location = 'West US';
47+
Plan = 'Free'; ### Basic is also supported
48+
Tags = @(
49+
@{ Name = 'Company'; Value = 'Art of Shell'; };
50+
@{ Name = 'Department'; Value = 'Marketing'; };
51+
)
52+
};
53+
New-AzureRmAutomationAccount @AutomationAccount;
54+
55+
### Import an Azure Automation Runbook
56+
Get-Command -Module AzureRM.Automation -Name *runbook*;
57+
58+
### Import an Azure Automation Runbook
59+
$Runbook = @{
60+
AutomationAccountName = $AutomationAccount.Name;
61+
Path = '{0}\02-RunbookExample.ps1' -f $PSScriptRoot;
62+
Description = 'This is an example Runbook';
63+
Type = 'PowerShell';
64+
Published = $true;
65+
Force = $true;
66+
};
67+
Import-AzureRmAutomationRunbook @Runbook;

02-RunbookExample.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<#########################################################
2+
3+
Author: Trevor Sullivan <[email protected]>
4+
Description: This PowerShell script demonstrates how to create a simple PowerShell
5+
script that acts as an Azure Automation Runbook.
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+
[CmdletBinding()]
15+
param (
16+
[Parameter(Mandatory = $false)]
17+
[string] $CredentialName = 'AzureAdmin'
18+
)
19+
20+
### The first thing we need to do is authenticate to Microsoft Azure using Azure Active Directory (AAD)
21+
### because this Runbook is managing resources inside our Microsoft Azure Subscription.
22+
23+
### LEARNING POINT: We use the special Get-AutomationPSCredential command to retrieve a credential
24+
### from the Azure Automation Asset Store.
25+
$AzureCredential = Get-AutomationPSCredential -Name $CredentialName;
26+
Write-Verbose -Message 'Retrieved credential from Azure Automation Asset Store';
27+
28+
### LEARNING POINT: The reason we put "$null =" at the beginning, is to suppress output
29+
### from the Add-AzureRmAccount command
30+
$null = Add-AzureRmAccount -Credential $AzureCredential;
31+
Write-Verbose -Message 'Finished authenticating to Microsoft Azure';
32+
33+
### Delete specific Azure Resource Manager (ARM) Resource Groups
34+
Get-AzureRmResourceGroup |
35+
Where-Object -FilterScript { $PSItem.Name -in @('ArtofShell-Test1', 'ArtofShell-Test2') } |
36+
Remove-AzureRmResourceGroup -Force;
37+
38+
Write-Verbose -Message 'Runbook completed';

03-DSC Configuration.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<#########################################################
2+
3+
Author: Trevor Sullivan <[email protected]>
4+
Description: This PowerShell script demonstrates how to create a PowerShell
5+
Desired State Configuration (DSC) document.
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+
configuration ArtofShell {
15+
Import-DscResource -ModuleName PSDesiredStateConfiguration;
16+
17+
node @('web01', 'web02', 'web03') {
18+
WindowsFeature IIS {
19+
Name = 'Web-Server';
20+
Ensure = 'Present';
21+
}
22+
}
23+
24+
node @('db01', 'db02') {
25+
### Install .NET Framework dependency for SQL Server
26+
WindowsFeature NET-Framework-Core {
27+
Ensure = 'Present';
28+
Name = 'NET-Framework-Core';
29+
}
30+
}
31+
}

04-Automating Azure Automation.ps1

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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

Comments
 (0)