PowerShell Guide Azure
PowerShell Guide Azure
Introduction:
Welcome to the PowerShell Reference Guide. This guide will provide you with a reference to
key PowerShell commands necessary for Azure administrators as well as required to pass the
Azure Administrator certification exams from Microsoft.
If you are completely new to PowerShell, we highly recommend you check out the Microsoft
Azure PowerShell Overview which has a number of tutorials and guides for learning the basics.
This guide is made up of several PowerShell commands which have been reference from the
Microsoft documentation and other sources. Before running any of these commands in
production, please be sure to test them out in an Azure test account. Some commands are
destructive in nature (e.g. removing resource groups, tags etc.) and you need to make sure you
fully understand the commands that you execute.
The guide is divided up into the following sections:
Downloading PowerShell:
Always make sure you have the latest version of PowerShell installed
https://azure.microsoft.com/en-gb/downloads/
All Azure administrators will require PowerShell along with the AzureRM module installed on
their laptops.
Installing AzureRM Module (Windows Example)
Installing Azure PowerShell from the PowerShell Gallery requires elevated privileges. Run the
following command from an elevated PowerShell session (Search for PowerShell à Right Click
à Run as Administrator)
By default, the PowerShell gallery is not configured as a Trusted repository for PowerShellGet.
You will see the following prompts. Enter Yes to all.
Untrusted repository
Make sure to choose yes when prompted to install modules from the untrusted repositories.
You can make these repos trusted by using the Set-PSRepository cmdlet and changing the
installation policy if you desire given that the source is PSGallery.
Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
Upon entering this command, you will be presented with a popup window to complete your login
process and any MFA requirements.
Subscription Selection
Resource Groups
Retrieving Resource Groups
Adding Tags
($resourcetags.ContainsKey($key)) {
$resourcetags.Remove($key) }
}
$resourcetags += $g.Tags
Set-AzureRmResource -Tag
$resourcetags -ResourceId $r.ResourceId -Force
}
}
}
Governance
Create Policies
Example:
$definition = New-AzureRmPolicyDefinition `
-Name denyRegions `
-DisplayName "Deny specific regions" `
-Policy
'https://githublocation.com/azurepolicy.rules.js
on'
$definition = New-AzureRmPolicyDefinition `
-Name denyCoolTiering `
-Description "Deny cool access tiering for
storage" `
-Policy "c:\policies\coolAccessTier.json"
Assign Policies
Resource Locks
Storage
Retrieving Storage Accounts
-Access Tier
• Hot
• Cold
Create a storage container 1. Get the storage account and store it as a variable
in a storage account (using
the storage account object)
Ø $storageaccount = Get-AzureRmStorageAccount -
ResourceGroupName "slstoragerg" -AccountName
"slstorageaccount"
Ø $storageaccount
This will show you the storage account object you stored in
the variable $storageaccount
Ø New-AzureRmStorageContainer -StorageAccount
$accountObject -ContainerName "slContainer" -
Ø $storageaccount = Get-AzureRmStorageAccount -
ResourceGroupName "slstoragerg" -AccountName
"slstorageaccount"
Task Command
Create a VM – Simplified
I put this command here as it is a quick way to create a VM, but you are far better off using VM
configurations to create your VMs with more specific parameters applied. Try out both of them and you
will see the difference.
Task Command
Use the following tasks to create a new VM configuration before creating your Virtual Machine based on
that config.
Task Command
All resources are created in the resource group. Before you run this command,
run New-AzureRmVMConfig, Set-AzureRmVMOperatingSystem, Set-
AzureRmVMSourceImage, Add-AzureRmVMNetworkInterface, and Set-
AzureRmVMOSDisk.
VM Operations
Task Command
Networking
Get/List Networking
Gets information about the subnet in the specified virtual network. The $vnet
value represents the object returned by Get-AzureRmVirtualNetwork you used
previously.
Note: Make sure to create the subnets first as per the previous command above.
The public IP address uses the domain name that you previously tested and is used by
the frontend configuration of the load balancer.
The frontend configuration includes the public IP address that you previously created for
incoming network traffic.
Contains health probes used to check availability of virtual machines instances in the
backend address pool.
Contains rules that assign a public port on the load balancer to a port in the backend
address pool.
Contains rules mapping a public port on the load balancer to a port for a specific virtual
machine in the backend address pool.
LoadBalancerInboundNatRule $loadBalancer.InboundNatRules[0]
Create a network interface using the public IP address and virtual network subnet that
you previously created.
1. Open PowerShell
Connect to Azure AD
$PasswordProfile.Password = "Password"
3. Create User
Service Principal Creation First you need to create your application registration in AzureAD then
you retrieve it with this command.
Get-AzureRmADApplication -DisplayNameStartWith
slappregistration
Once you have the application ID for the App registration, you can use
it to create the SPN (Service Principal)
New-AzureRmADServicePrincipal -ApplicationId
11111111-1111-1111-1111-11111111111 -Password
$securePassword