Automating Microsoft Azure With PowerShell - Sample Chapter
Automating Microsoft Azure With PowerShell - Sample Chapter
virtual
networks,
traffic
manager,
cloud
Automating Microsoft
Azure with PowerShell
runbooks.
P U B L I S H I N G
Aman Dhally
$ 29.99 US
19.99 UK
John Chapman
ee
Sa
pl
e
P r o f e s s i o n a l
E x p e r t i s e
D i s t i l l e d
John Chapman
Aman Dhally
P U B L I S H I N G
Aman Dhally is a PowerShell MVP and founder of New Delhi PowerShell User
Group. He has more than 14 years of experience in the IT industry. His main focus
is the automation of manual tasks using PowerShell. He came in to contact with
PowerShell in 2010. Since then, he has published a wealth of articles, videos, blogs,
and PowerShell scripts. He loves teaching PowerShell and speaking at User
Group events.
Aman works as a network analyst for Analysys Mason Limited, which he describes
as a "cool" company to work for. Here, he works on various IT projects, and his
PowerShell skills bring a unique approach to solving IT issues and improving
efficiency within the company.
He is fascinated by anything to do with life: philosophy, self-help techniques,
and biographies of famous people, which he finds inspiring. In his free time, you
will often find him reading a book on any of these subjects. His philosophy can be
summed up by his phrase: "You see a mousetrap, I see free cheese and a challenge!"
Chapter 9
[ 99 ]
fwlink/p/?linkid=236297)
After installing these tools, reboot the computer to complete the installation.
If you use the Azure Active Directory module, you will encounter
a known issue if you attempt to use the 32-bit version on a 64-bit
computer. If you use the 32-bit version on a 64-bit computer and
receive errors such as the term is not recognized, refer to http://
stackoverflow.com/questions/16018732/msonline-cantbe-imported-on-powershell-connect-msolservice-error.
[ 100 ]
Chapter 9
[ 101 ]
3. Select the USERS tab and then click on ADD USER at the bottom of
the screen:
4. Enter the name in the USER NAME field and click on the next button:
[ 102 ]
Chapter 9
5. Select Global Administrator for ROLE and provide FIRST NAME, LAST
NAME, DISPLAY NAME, and ALTERNATE EMAIL ADDRESS for the
new user. Click on the next button:
6. Click on CREATE.
[ 103 ]
7. Make a note of the full username and the temporary password for
the new user:
8. To enable this user to create new Active Directory domains, rather than just
manage the Default Directory, we will need to add this user as an Azure
Administrator. From the left-hand side of the Azure management portal,
select SETTINGS, ADMINISTRATORS, and then click on ADD, as shown
in the following screenshot:
[ 104 ]
Chapter 9
9. In the EMAIL ADDRESS field, enter the full username of the user we
created (for example, [email protected]), select
SUBSCRIPTION, and click on the checkmark button to complete the
process of adding the user:
[ 105 ]
10. Before the newly created account can be used, we need to change the
temporary password. First, log out from the Azure portal by selecting your
account name in the top-right corner of the page and clicking on SIGN OUT.
11. Click on SIGN IN to return to the login page.
12. Use the username and temporary password of the newly created account
to log in.
13. When prompted, provide a new password for the account.
With the new administrator account created and the password set, we can now
connect to Azure Active Directory with PowerShell.
[ 106 ]
Chapter 9
For more information about Azure Active Directory and the cmdlet used in this
section, refer to the following resources:
com/en-us/library/azure/jj151815.aspx)
[ 107 ]
Before you continue, ensure that you are connected to Azure Active
Directory in a PowerShell session, as outlined in the Connecting to
Azure Active Directory section.
In this tutorial, we will use PowerShell to create a new Azure Active Directory
domain in the default directory. However, we will not cover how to configure the
DNS settings. Microsoft has provided instructions on how to configure the DNS
settings at various registrars on MSDN (http://msdn.microsoft.com/library/
azure/jj151803.aspx#BKMK_cname). To create a new Azure Active Directory
domain in the default directory, follow these steps:
1. Use the New-MsolDomain cmdlet to create a new Active Directory domain
and assign it to a PowerShell variable. The name provided should be in the
form of a fully qualified domain name (FQDN). It should match the domain
name purchased from a registrar or should be provided by the group in the
organization that manages public domain names:
PS C:\> $domain = New-MsolDomain Name PowerShell.local
2. Enter the variable and press Enter. The variable can be used to retrieve
information about the new domain or to configure it:
PS C:\> $domain
Chapter 9
4. Use the label for the domain to add a TXT or MX record to the DNS settings
for the domain name. In the sample used for the screenshots, the TXT record
will be ms43896406, and the MX record will be ms43896406.msv1.invalid.
Microsoft provides instructions to update the DNS records at many of the
common domain name registrars on MSDN at http://msdn.microsoft.
com/library/azure/jj151803.aspx#BKMK_cname.
Once DNS records are updated, it can take up to 48 hours for
them to propagate.
5. Once the DNS records for the domain are configured, use the ConfirmMsolDomain cmdlet to process the verification of the domain:
PS C:\> Confirm-MsolDomain -DomainName PowerShell.local
For more information about verifying domain names and the cmdlets used in this
section, refer to the following resources:
com/library/azure/jj151803.aspx)
[ 109 ]
In this tutorial, we will configure the password policy for the domain that we
created in the Creating a new Azure Active Directory domain section. The password
policy includes two options: NotificationDays and ValidityPeriod. The
NotificationDays option sets how many calendar days the password change
notification should be sent before the password expires, and the ValidityPeriod
option sets how many calendar days the passwords are valid for. To configure the
password policy for the domain, follow these steps:
1. Use the Get-MsolPasswordPolicy cmdlet to view the current password
policy settings. By default, nothing will be set for the NotificationDays
and ValidityPeriod values:
PS C:\> Get-MsolPasswordPolicy -DomainName PowerShell.local
For more information about the cmdlets used in this section, use the Get-Help
cmdlet in PowerShell, or refer to the following resources:
[ 110 ]
Chapter 9
Services: Support
Support: John
Development: Jane
The Services group provides access to the Services file share, and the
Development group provides access to the Development file share
In this example, Jane has access to the Development file share as she is a member
of the Development group. In addition, John has access to the Services file share
because he is a member of the Support group that is a member of the Services group.
For more information about how Active Directory domains are structured, refer to
http://en.wikipedia.org/wiki/Active_Directory.
Before you continue, ensure that you are connected to Azure Active
Directory in a PowerShell session, as outlined in the Connecting to
Azure Active Directory section.
[ 111 ]
For this tutorial, we will create users and groups (described in the preceding example).
In addition, we will assign group membership. To do this, follow these steps:
1. Use the New-MsolUser cmdlet to create a user account for Jane and John and
assign them to variables. The username must include the domain name, for
example, [email protected], as shown here:
PS C:\> $jane = New-MsolUser -UserPrincipalName
"[email protected]" -DisplayName "Jane" -Password
"P@assword1234~"
PS C:\> $john = New-MsolUser -UserPrincipalName
"[email protected]" -DisplayName "John" -Password
"P@assword1234~"
Users and groups in Azure Active Directory have a number of properties that can be
set with PowerShell. Updating these properties can be done using the Set-MsolUser
and Set-MsolGroup cmdlets (for example, PS C:\> Set-MsolUser -ObjectId
$jane.ObjectId -City "London").
For more information about Active Directory and the cmdlets used in this section,
refer to the following resources:
Chapter 9
In this tutorial, we will create a .csv file with some user details, and we will use this
data to create users in Azure Active Directory with PowerShell. To do this, follow
these steps:
1. Create a .csv file with data that looks like the following code snippet:
"Username","Password","DisplayName","City"
"[email protected]","P@ssword1234~","Rose","London"
"[email protected]","P@ssword1234~","Jose","London"
"[email protected]","P@ssword1234~","Pierre","London"
"[email protected]","P@ssword1234~","Diego","London"
"[email protected]","P@ssword1234~","Sherlock","London"
[ 113 ]
You can create the .csv file with any plain text editor (such as
Notepad) or spreadsheet software (such as Microsoft Excel).
2. Use the Import-Csv cmdlet to import the user data from the .csv file
to a PowerShell object:
PS C:\> $users = Import-Csv C:\Files\Users.csv
[ 114 ]
Chapter 9
For more information about the cmdlets used in this section, use the Get-Help
cmdlet in PowerShell, or refer to the following resources:
Summary
Azure Active Directory offers a myriad of features and services for Azure customers.
In this chapter, we covered the basics of how to manage Azure Active Directory
from PowerShell and showcased an example where PowerShell automation can save
administrators time and effort. Using these techniques and ideas, many mundane
and repetitive tasks can be automated to allow administrators to focus on more
important tasks.
In the next chapter, we will explore how to automate Microsoft Azure tasks in
PowerShell using runbooks and automation cmdlets.
[ 115 ]
www.PacktPub.com
Stay Connected: