Azure PowerShell
Azure PowerShell
Services
Microsoft Azure is a cloud computing platform developed by Microsoft, offering a broad
range of services for building, deploying, and managing applications through Microsoft's
globally distributed data centers. It provides solutions for infrastructure, platform, and
software needs, making it a versatile choice for businesses of all sizes.
o Azure data centers are distributed globally, organized into regions and
availability zones to ensure data redundancy and service availability.
o Availability Zones: Data centers within a region that offer redundancy and
failover capabilities to keep applications running smoothly even if a data
center experiences issues.
1. Compute Services
o Azure App Service: A PaaS service for building and hosting web
applications, RESTful APIs, and mobile backends without managing
infrastructure.
2. Storage Services
o Azure Blob Storage: Offers scalable object storage for unstructured data,
such as images, videos, documents, and backups.
o Azure File Storage: A fully managed file share service that can be accessed
via SMB protocol, making it compatible with Windows and Linux systems.
3. Database Services
o Azure SQL Database: A managed relational database service based on SQL
Server, ideal for storing structured data.
o Azure Data Lake: A repository for storing large data sets, commonly used for
big data analytics.
4. Networking Services
o Azure DNS: A DNS service for hosting domain name services, allowing users
to manage their DNS zones in Azure.
o Azure Bot Service: A platform for building, testing, and deploying intelligent
chatbots that can interact with users across various platforms.
o Azure Synapse Analytics: A unified analytics platform for big data and data
warehousing, integrating Spark, SQL, and other data processing tools.
o Azure Stream Analytics: A real-time analytics service that can analyze and
process high-volume streaming data from IoT devices or apps.
o Azure Sphere: Provides an end-to-end security solution for IoT devices, from
hardware to cloud connectivity.
o Azure DevTest Labs: A service for developers and testers to quickly create
virtual environments with reusable templates.
2. Global Reach: Azure’s extensive network of data centers enables users to deploy
applications and store data close to their customers, minimizing latency.
3. Cost Efficiency: With pay-as-you-go pricing and reserved instances, Azure provides
cost-effective solutions for managing IT infrastructure and applications.
• Data Analytics: Analyzing and visualizing large datasets with Azure Synapse
Analytics and Power BI.
• Backup and Disaster Recovery: Using Azure Site Recovery and Backup to ensure
data safety and availability.
• IoT Applications: Managing IoT devices, collecting and processing data, and
building digital twin models with IoT Hub and Azure Digital Twins.
• Machine Learning and AI: Building and deploying intelligent models with Azure
Machine Learning and Cognitive Services.
Introduction
You can manage Microsoft Azure resources by using the Azure portal, which is usually the
most common way of administration. However, for some tasks, Azure PowerShell is more
convenient. In this module, you'll learn about the Azure PowerShell environment and the Az
module for Windows PowerShell. Also, you'll learn about ways to manage Microsoft Entra
ID by using PowerShell modules.
You should use Azure PowerShell when you want to build automated tools that use the
Azure Resource Manager model. You can use it in your browser with Azure Cloud Shell, or
install it on your local Windows, Mac, or Linux machine. In both cases, you have two modes
to choose from. You can use Azure PowerShell in interactive mode, in which you manually
issue one command at a time, or in scripting mode, where you run a script that consists of
multiple commands.
o Support for authentication with Active Directory Federation Services (AD FS)
in Windows Server 2019
• New capabilities:
The Az PowerShell module is based on the .NET Standard library and works with PowerShell
7 and newer on all platforms including Windows, macOS, and Linux. It's also compatible
with Windows PowerShell 5.1.
Az is the most current PowerShell module for Azure. You can log issues or feature requests
directly on the GitHub repository. You can also contact Microsoft support if you have a
support contract. Feature requests will be implemented in the latest version of Az. Critical
issues will be implemented on the last two versions of Az.
To check your PowerShell version, run the following command from within a PowerShell
session:
Before installing the Azure Az PowerShell module, you should set your PowerShell script
execution policy to RemoteSigned. You can do this by running the following command:
Methods to install the Az PowerShell
module
You can install the Azure Az PowerShell module by using one of the following methods:
To update any PowerShell module, you should use the same method used to install the
module. For example, if you originally used Install-Module, then you should use Update-
Module to get the latest version. If you originally used the MSI package, then you should
download and install the new MSI package.
To download the image and start an interactive PowerShell session, you should run the
following commands:
The new cmdlet names have been designed to be easier to learn. Instead of using AzureRm
or Azure in cmdlet names, you use Az cmdlets. For example, the old cmdlet New-
AzureRMVm has become New-AzVm. However, migration is more than just becoming
familiar with the new cmdlet names. There are renamed modules, parameters, and other
important changes.
Before taking any migration steps, check which versions of AzureRM are installed on your
system. Doing so allows you to make sure scripts are already running on the latest release
and let you know which versions of AzureRM must be uninstalled.
To check which versions of AzureRM you've installed, run the following command:
The recommended option to migrate from AzureRM to the Az PowerShell module is to use
automatic migration. For this, you need to install the AzureRM to Az migration toolkit by
running the following command:
With the AzureRM to Az migration toolkit, you can generate a plan to determine what
changes will be performed on your scripts before making any modifications to them and
before installing the Az PowerShell module.
You can also use Microsoft Visual Studio Code to migrate your existing scripts. To do so,
you first need to install the Azure PowerShell extension for Visual Studio Code. Then, you
need to perform the following steps:
• Supports external identities: Users from other Microsoft Entra tenants can
authenticate to services in your tenant with Microsoft Graph PowerShell.
• Uses least privilege: Microsoft Graph PowerShell permissions are NOT pre-
authorized and users must perform one-time request for app permissions
depending on their needs.
• Open source: Feature teams and the community can create great PowerShell
experiences and share them with everyone.
The easiest way to install the module is from the PowerShell Gallery. You can install the
module with the Install-Module cmdlet by running the following command:
You can also add beta features for Microsoft Graph SDK by running (not required):
Connecting to Microsoft Entra ID with PowerShell
If you want to connect to the Microsoft Entra service with the Microsoft Graph module for
Windows PowerShell, run the following command:
After running the previous command, you'll be prompted for your Microsoft Entra
credentials. You should use the credentials that you use to sign in to Microsoft 365 or your
Azure services. After you authenticate, you'll be able to use the cmdlets available for
Microsoft Entra management.
Create a new Azure virtual machine by
using Windows PowerShell commands
To create a new Azure virtual machine (VM) with PowerShell commands, you can use the
locally installed Windows PowerShell with Az module, or you can use the Cloud Shell
environment that's available in Azure portal. If you choose to use your locally installed
PowerShell, it's recommended that you use Windows PowerShell 7.1. You should also
install the Az module, so you can have Azure-related commands available. Also, when
using locally installed PowerShell, you first need to use the Connect-AzAccount command
to authenticate and connect to your Azure tenant. When you run this command in your
PowerShell environment, you'll be prompted to authenticate. You need to use credentials
from your Azure tenant, with privileges that allow you to create the resources needed for
Azure VMs.
The resource group is later used when creating or modifying a VM or the resources
attached to a VM.
Create an Azure VM
The New-AzVM cmdlet creates a VM in Azure. This cmdlet uses a VM object as input. Use
the New-AzVMConfig cmdlet to create a virtual machine object. When you're creating a
VM, several options are available, such as operating system image, network configuration,
and administrative credentials. You can use other cmdlets to configure the VM, such
as Set-AzVMOperatingSystem, Set-AzVMSourceImage, Add-AzVMNetworkInterface,
and Set-AzVMOSDisk.
Before you run the New-AzVM command, you need to specify the credentials that you'll
use to sign in to the newly created Azure VM. The credentials that you specify during this
process will be assigned with local administrative privileges on the VM you're creating. It's
easiest to store these credentials in a variable, before creating a new Azure VM. To do this,
run this command:
When you run this command, you'll be prompted to provide the username and password
for the Azure VM. These credentials will be stored in the $cred variable.
After you store administrative credentials, you need to define parameters for the new VM.
You don't need to provide all the parameters that New-AzVM supports. Most of them are
optional, and if you don't provide them, their default values will be selected. You can also
change most of these parameters later.
You can choose to provide VM parameters directly with the New-AzVM command, or you
can define these parameters in a variable, and then use this variable with the New-
AzVM command.
When you define VM parameters as the previous example depicts, you can then use the
following command to create a new Azure VM, based on these parameters:
Alternatively, you can also choose to provide VM parameters directly with New-AzVM as in
the following example:
Run the following commands to return the public IP address of the VM. Take note of this IP
address so you can connect to it with your browser to test web connectivity in a future step.
To create a remote desktop session with the VM, use the following command on your local
machine. Replace the IP address with the publicIPAddress of your VM. When prompted,
enter the credentials you used when creating the VM.
When you run this command, you'll be prompted for credentials to connect to the VM. In
the Windows Security window, select More choices, and then select Use a different
account. Enter the username and password you created for the VM, and then select OK.
After you connect to your Azure VM through Remote Desktop Protocol (RDP), you'll be able
to manage it the same way as any other computer.
Manage Azure virtual machines by using
Windows PowerShell commands
Besides using Windows PowerShell to create new Azure VMs, you can also use PowerShell
commands to manage, modify, and remove Azure VMs and the resources related to Azure
VMs. This topic covers some of the most common tasks for managing and modifying Azure
VMs with PowerShell.
Modifying VM sizes
The VM size determines the amount of compute resources such as CPU, GPU, and memory
that are made available to the VM. You should create VMs using a VM size that's
appropriate for the workload. If a workload increases, you can also resize existing VMs.
After a VM has been deployed, you can resize it to increase or decrease resource
allocation. Before resizing a VM, check if the size you want is available on the current VM
cluster. The Get-AzVMSize command returns a list of sizes:
If your preferred size is available, you can resize the VM from a powered-on state; however,
it's rebooted during the operation. The following example depicts how to change VM size to
the Standard_DS3_v2 size profile:
Management tasks
During the lifecycle of a VM, you might want to run management tasks such as starting,
stopping, or deleting a VM. Additionally, you might want to create scripts to automate
repetitive or complex tasks. You can use Azure PowerShell to perform many common
management tasks by using the command line or scripts.
To stop and deallocate a VM with Stop-AzVM, you can run the following command:
If you want to delete everything inside of a resource group, including VMs, you can run the
following command:
• Operating system disk. These disks can be sized up to 4 terabytes and host the VM's
operating system.
• Temporary disk. These disks use a solid-state drive that's located on the same Azure
host as the VM. Temporary disks are highly performant and might be used for
operations such as temporary data processing.
You can add additional data disks for installing applications and storing data. You should
use data disks in any situation that requires durable and responsive data storage. The size
of the VM determines how many data disks can be attached to it.
To add a data disk to an Azure VM after you create it, you need to define disk configuration
by using the New-AzDiskConfig command. You then need to use the New-
AzDisk and Add-AzVMDataDisk commands to add a new disk to the VM, as the following
example depicts:
You can create a standard, general-purpose storage account with locally redundant storage
(LRS) replication by using New-AzStorageAccount. Next, get the storage account context
that defines the storage account you want to use. When acting on a storage account,
reference the context, instead of repeatedly passing in the credentials. Use the following
example to create a storage account called mystorageaccount with LRS and blob
encryption, which is enabled by default.
Blobs are always uploaded into a container. You can organize groups of blobs the way you
organize your files on your computer in folders.
Set the container name, and then create the container by using New-AzStorageContainer.
Set the blob permissions to allow public access of the files. The container name in the
following example is quickstartblobs.
You can use the Set-AzStorageAccount cmdlet to modify an Azure Storage account. You
can use this cmdlet to modify the account type, update a customer domain, or set tags on
a Storage account.
For example, to set the storage account type you should use the following command:
To set custom domain for existing storage account, you can use the following command:
In Azure PowerShell, accessing the resources for a subscription requires changing the
subscription associated with your current Azure session. You can do this by modifying the
active session context, which is the information about which tenant, subscription, and user
the cmdlets should be run against. To change subscriptions, you need to first retrieve an
Azure PowerShell Context object with Get-AzSubscription, and then change the current
context with Set-AzContext.
The Get-AzSubscription cmdlet gets the subscription ID, subscription name, and home
tenant for subscriptions that the current account can access.
To get all Azure subscriptions active on all tenants, run the following command:
To focus on subscriptions assigned to a specific tenant, run the following command:
The Set-AzContext cmdlet sets authentication information for cmdlets that you run in the
current session. The context includes tenant, subscription, and environment information.
The next example depicts how to get a subscription in the currently active tenant and set it
as the active session:
Introduction
Imagine working for an organization that uses Azure Virtual Machines (VMs) to test its
Customer Relationship Management (CRM) software. You create these VMs from images,
including a web front-end and a SQL database.
During multiple testing iterations on a single VM, you notice that changes to configuration
files and the database lead to inconsistent results. In one instance, a bug erroneously
generated a phone call record without an associated customer in the database. This
orphaned record caused subsequent integration tests to fail, even after you fixed the bug.
Recognizing the frequency of these testing cycles, which occur multiple times per week,
you decide to deploy a new VM for each cycle to ensure the consistency and reliability of
your test environment. By developing PowerShell scripts for deployment, you streamline
the process, optimize workflow efficiency, and minimize errors through automation.
This module teaches you how to deploy and manage Azure resources using Azure
PowerShell. You learn how to use Azure PowerShell interactively for one-off tasks and how
to write PowerShell scripts to automate recurring tasks.
You seek a tool that's easy for administrators to learn, yet powerful enough to automate the
installation and configuration of multiple virtual machines. With several tools available,
your goal is to find the best one for your administrators to script the deployment of the
entire cloud infrastructure efficiently.
• Azure portal
• Azure PowerShell
• Azure CLI
These tools provide the same deployment and configuration capabilities, allowing you to
perform any task with any of the three. All are available on Windows, Linux, and macOS.
However, they differ in syntax, setup requirements, and support for automation.
The rest of this unit describes each of these tools and offers guidance on how to choose
the best one for your needs.
Azure portal
The Azure portal is a web-based interface that allows you to create, configure, and manage
the resources in your Azure subscription. As a Graphical User Interface (GUI), it offers a
user-friendly way to locate and manage resources, with wizards and tooltips to assist you
through complex management tasks.
However, Azure portal lacks automation capabilities for repetitive tasks. For instance, if you
need to set up 15 VMs, you must create each one individually, completing the wizard for
each VM. This approach can be time-consuming and error-prone for more complex tasks.
Azure PowerShell
Azure PowerShell is a collection of modules that provide PowerShell cmdlets for managing
Azure resources. It requires PowerShell, a command-line shell and scripting language.
For example, Azure PowerShell provides the New-AzVM cmdlet to create a virtual machine
in your Azure subscription. To use it, launch PowerShell and execute the following
command:
Azure PowerShell is available in two ways: inside a browser via Azure Cloud Shell or
through a local installation on Windows, Linux, or macOS. You can use PowerShell
interactively, running Azure PowerShell commands from PowerShell, or you can create and
execute scripts that consist of multiple commands.
Azure CLI
The Azure CLI is a cross-platform command-line tool that enables you to manage Azure
resources. For instance, you can create a virtual machine using the following command
(this example uses Bash line continuation characters):
The Azure CLI is also available in two ways: inside a browser via Azure Cloud Shell or
through a local installation on Windows, Linux, or macOS. You can use it interactively by
launching a shell (such as Bash, PowerShell, or cmd.exe) and entering commands at the
prompt. Alternatively, you can automate repetitive tasks by assembling the commands into
a shell script using the syntax of your chosen shell.
• Learning curve: Do you need to complete a task quickly without learning new
commands or syntax? The Azure portal is user-friendly and doesn't require learning
commands or syntax. In contrast, Azure PowerShell and the Azure CLI require an
understanding of command shell syntax and the specific requirements for each
command.
• Team skillset: Does your team have existing expertise? For instance, if your team is
already using PowerShell to manage other products like Windows, Exchange Server,
or Microsoft 365, they might find Azure PowerShell to be a more comfortable and
efficient choice.
Example
Let's revisit the scenario of choosing an administrative tool to deploy and test your CRM
application. Your administrators have two specific Azure tasks to complete:
1. Create one resource group for each category of testing (unit, integration, and
acceptance).
2. Create multiple VMs in each resource group before each round of testing.
The Azure portal is a reasonable choice for creating resource groups. Since these tasks are
one-off, you don't need scripts to create them.
Most administrators first experience Azure through the Azure portal. The portal provides a
well-organized graphical interface for managing Azure resources, making it an excellent
starting point. However, the portal doesn't support automation, which is essential for
repetitive tasks like creating multiple VMs several times a week.
• Azure PowerShell
• Azure CLI
Both tools meet your needs, but Azure PowerShell might be the best choice if your team
has existing PowerShell knowledge.
In practice, businesses typically perform a mix of one-off and repetitive tasks, often using
both the Azure portal and a scripting solution. In your CRM example, creating resource
groups using the Azure portal and automating VM creation with Azure PowerShell is a
reasonable approach.
The remainder of this module assumes Azure PowerShell is your choice for an automation
tool.
Install Azure PowerShell
In this scenario, your administrators prefer executing scripts locally on their computers
rather than using Azure Cloud Shell. Since your team uses a mixture of Windows, Linux,
and macOS, your task is to ensure Azure PowerShell works across all these devices. This
empowers your administrators to manage and automate the Azure infrastructure for your
organization from their preferred operating system on their local computers.
• The Az PowerShell module: This module must be installed to add the Azure-
specific commands to PowerShell.
After installing the appropriate version of PowerShell, you can then install
the Az PowerShell module to start managing Azure resources.
Windows
On Windows, use the Windows Package Manager (winget) to install the latest stable
version of PowerShell 7.
Linux
The package manager on Linux differs based on the distribution. PowerShell is available in
the Microsoft repository, which you must add to your package manager.
The package manager on Linux differs based on the distribution. PowerShell is available in
the Microsoft repository, which you must add to your package manager.
Red Hat Enterprise Linux (RHEL) dnf or yum depending on your RHEL version
To install PowerShell on supported versions of Ubuntu Linux, use the Advanced Packaging
Tool (apt) and the Bash command line.
macOS
On macOS, use Homebrew to install PowerShell.
The current version of the Azure PowerShell module for managing Azure resources is
named Az. It's the recommended PowerShell module for managing Azure resources with
PowerShell. It includes thousands of commands that control almost every aspect of Azure.
The Az PowerShell module is cross-platform.
You might find examples on the internet that use the AzureRM PowerShell module, which is
the previous generation of Azure PowerShell. It's deprecated, no longer maintained or
supported, and not recommended. Commands in the AzureRM PowerShell module use
the *-AzureRM* format.
You might also encounter an Azure PowerShell module named Azure, which is for
managing legacy Azure resources that use Azure Service Manager (ASM) APIs.
The Azure PowerShell module isn't recommended when creating new resources since ASM
is scheduled to retire soon.
1. In the System Tray Search Box, type PowerShell. You might see multiple shortcuts:
If PowerShell version 7 isn't installed, open Windows PowerShell and use Windows
Package Manager (Winget) to install the latest stable version of PowerShell 7:
This command installs the Az PowerShell module for your current user, which is controlled
by the Scope parameter.
This process enables you to use the full range of Azure-specific cmdlets available in
the Az PowerShell module.
In the Customer Relationship Management (CRM) example, the goal is to create three test
environments, each containing virtual machines (VMs). Use resource groups to ensure the
VMs are organized into separate environments:
• Unit testing
• Integration testing
• Acceptance testing
Since you only need to create the resource groups once, using PowerShell interactively for
this task is a reasonable choice.
When you run a command in PowerShell, it matches the command to a cmdlet and
performs the requested action.
Connect to Azure
When using a local installation of Azure PowerShell, you must authenticate before
executing any Azure PowerShell commands. The Connect-AzAccount cmdlet prompts you
for your Azure credentials and then connects to your Azure subscription.
This command opens a prompt for your Azure credentials and establishes a connection to
your Azure subscription.
You can only be active in one subscription at a time. Use the Get-AzContext cmdlet to
determine which subscription is currently active. If it's not the correct one, you can switch
subscriptions using the Set-AzContext cmdlet.
If you need to look up the Subscription ID, you can find it in the output of the Get-
AzSubscription cmdlet.
To create a resource group, use the New-AzResourceGroup cmdlet. You must specify a
name and a location, and the name must be unique within your subscription. The location
determines where the metadata for your resource group is stored, which can be important
for compliance reasons. Use the Get-AzLocation cmdlet to determine the available
locations.
You can also filter the output to show only a specific resource group:
The New-AzVM cmdlet is used to create a VM. This cmdlet has several parameters to
handle the extensive configuration settings for a VM. Most parameters have reasonable
default values, so you typically need to specify only five key items:
• Image: The operating system image to use for the VM, typically a Linux distribution
or Windows Server.
Here's an example:
You can supply these parameters as a hash table with splatting for the New-AzVM cmdlet,
as shown in the previous example. Alternatively, you could supply the parameters directly
to the New-AzVM cmdlet or use other cmdlets to configure the virtual machine, such
as Set-AzVMOperatingSystem, Set-AzVMSourceImage, Add-AzVMNetworkInterface,
and Set-AzVMOSDisk.
For example, to supply the parameters directly to the New-AzVM cmdlet, use the following
syntax:
The contents of the $vm variable is an object you can interact with. For example, you can
make changes to this object and then push the changes to Azure using the Update-
AzVM command:
Using PowerShell in interactive mode is appropriate for one-off tasks. For example, you can
create and manage resource groups interactively if they're only created once during the
lifetime of a project. Interactive mode is often quicker and easier than writing a script for
tasks that are executed only once.
Exercise - Create an Azure Resource
interactively with Azure PowerShell
In the original scenario, you must create virtual machines (VMs) to test your Customer
Relationship Management (CRM) software. When a new build is available, you want to spin
up a new VM to test the entire installation experience from a clean image. Once testing is
complete, you can delete the VM.
• Choose a location close to you from the list of available Azure sandbox
locations.
o westus2
o southcentralus
o centralus
o eastus
o westeurope
o southeastasia
o japaneast
o brazilsouth
o australiasoutheast
o centralindia
2. Enter Credentials:
When prompted, enter a username and password, following the guidelines: passwords
must be 12-123 characters long and meet three of the following four complexity
requirements: lowercase characters, uppercase characters, digits, and special
characters (Regex match [\W_]). For more information, see Linux VM FAQ.
When complete, query the VM and assign the VM object to a variable ($vm).
To view information about the VM, display the contents of the variable.
Example output:
6. Inspect VM properties:
You can inspect complex objects through the member-access operator (.). For example,
to see the properties in the VMSize object associated with
the HardwareProfile section, run the following command:
Or, to get information on one of the disks, run the following command:
Connect to the VM with SSH using the IP address from the variable. For example, if the
username is bob, use the following command:
Delete a VM
To try more commands, let's delete the VM. Follow these steps:
Use the Get-AzResource cmdlet to list all the resources in the resource group. The
results are piped to Select-Object to return specific properties:
You should see several resources, including disks, virtual networks, etc., that still exist:
The Remove-AzVM command only deletes the VM. It doesn't clean up any of the other
resources. To manually clean them up, follow these steps:
Next, let's look at how to automate these tasks using a PowerShell script.
Create Azure Resources with Azure
PowerShell using a script
Complex or repetitive tasks can be time-consuming and error-prone when performed
manually. Organizations prefer to automate these tasks to reduce costs and avoid
mistakes.
Beyond the core operation of creating a VM, your script needs to meet a few other
requirements:
• Create multiple VMs: Use New-AzVM to create multiple uniquely named VMs.
• Resource group parameter: Allow the script to accept the name of the resource
group as a parameter.
In this unit, you learn how to write and execute a PowerShell script that meets these
requirements.
PowerShell script files have a .ps1 file extension. You can create and save these files using
any text editor.
PowerShell techniques
PowerShell includes features commonly found in programming languages. You can define
variables, use branches and loops, capture command-line parameters, write functions,
add comments, and more. For the script you're focusing on, you need three key features:
variables, loops, and parameters.
Variables
In PowerShell, variables are declared using the dollar sign ($) and assigned values using the
equals (=) operator. For example:
To determine the value stored in a variable, use the $ prefix and its name:
Variables can hold objects. For example, the following definition sets
the $adminCredential variable to the object returned by the Get-Credential cmdlet:
Loops
PowerShell has several loop structures, including For, Foreach, Do-Until, Do-While,
and While. The Foreach loop is a good choice for scenarios where you need to execute a
loop over a set of values in a collection.
Parameters
When you create a PowerShell script, you can add parameters to the script and then
specify values for these parameters when executing the script. For example:
Inside the script, capture the values into variables. In this example, the parameters
are Name and Location:
You can use these parameters as input and a loop to create a set of VMs based on the given
parameters. This approach keeps the script generic and avoids repeating the same code.
Combining techniques
The combination of PowerShell language features and Azure PowerShell cmdlets provides
all the tools you need to automate Azure tasks. In the CRM example, you can create
multiple Linux VMs using a parameterized script and a loop to streamline the process. This
script allows you to perform a complex operation in a single step.
Example script
Here's an example script that demonstrates using variables, loops, and parameters to
create multiple VMs:
Executing the script
Save the script as CreateVMs.ps1 and run it from the PowerShell command line, providing
the required parameters:
PowerShell
Using this script, you can efficiently create multiple VMs in different resource groups,
automating repetitive tasks and ensuring consistency across your Azure environment.
Your company secured a booth at a large Linux trade show. You plan to set up a demo area
with three terminals, each connected to a separate Linux VM. You must delete the VMs and
re-create them at the end of each day so they start fresh every morning. Creating the VMs
manually after a long day is error-prone, so you need to write a PowerShell script to
automate the VM creation process.
To save the script, use the ellipsis (...) context menu at the top-right corner of the editor
or the Ctrl + S keyboard shortcut.
Completed script
The completed script should look like the following example:
Once you confirm your script looks like the code in the previous example, close the editor
using the ellipsis (...) context menu at the top-right corner of the editor, or
the Ctrl + Q keyboard shortcut.
Run the script
1. Execute the script using the following command:
3. Verify the VMs. Once the script finishes, verify it completed successfully by listing
the VMs in the resource group:
You successfully created a script that automates the creation of three VMs, each in a
specific resource group, ensuring they're ready for daily demos at the trade show. Although
the script is short and straightforward, it significantly speeds up a process that would
otherwise be time-consuming and error-prone if performed manually through the Azure
portal.