What is PowerShell?
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.
Initially a windows component only known as Windows PowerShell, it was made open source and cross platform on 18th August with the introduction of PowerShell core.
How to use PowerShell?
- Tasks are executed by command-lets (cmdlets).
- Each command-let contains verb which describes action that will be taken
- All PowerShell command-lets are parameterized to ensure maximum flexibility.
Azure PowerShell Review
Azure PowerShell is a set of cmdlets designed to help with management of Azure Resources directly from the PowerShell command line.
Most common scenarios are:
- Day to day operation such as Automation of repititive tasks, Scheduling, deployment etc.
- Achieving things that are hard otherwise
Installing & Configuring PowerShell Az Module
There are two modules bundle available.
- AzureRm (Azure Resource Management)
- Az (new modules; recommended)
Let’s install and import Az module and connect to our Azure Portal.
Install-Module Az -Force -AllowClobber Import-Module Az
Log into Azure
To connect to Azure, use the Connect-AzAccount cmdlet:
# Device Code login - It provides a link to sign into Azure via web browser Connect-AzAccount
Now you will asked to provide your Azure Portal credentials. Please provide credentials and you are all set to work.
To log into a specific cloud such as (AzureChinaCloud, AzureCloud, AzureGermanCloud, AzureUSGovernment), use the -Environment parameter:
Connect-AzAccount -Environment AzureChinaCloud
Getting and setting Azure PowerShell Session Context
A session context persists login information across Azure PowerShell modules and PowerShell instances. To view the context you are using in your current session which contains the subscription and tenant then use the Get-AzContext cmdlet:
To lists all available Azure PowerShell contexts in the current PowerShell session, use below cmdlet;
To get the subscriptions in a tenant, use the Get-AzSubscription cmdlet:
To get all of the Azure subscriptions in a specific Azure tenant then use below cmdlet:
Get-AzSubscription -TenantId $TenantId
To change the subscription that you are using for your current context, use the Set-AzContext cmdlet. You can set the Azure PowerShell context to a specific Azure subscription by below commands:
Set-AzContext -Subscription $SubscriptionName -Name 'MyContext'
You can set the Azure PowerShell context using piping
Get-AzSubscription -SubscriptionName $SubscriptionName | Set-AzContext -Name 'MyContext'
Use the Get-Command cmdlet to discover cmdlets within a specific module, or cmdlets that follow a specific search pattern: You can list all cmdlets in the Az.Accounts module
Get-Command -Module Az.Accounts Get-Command -Name '*VirtualNetwork*' Get-Command -Module Az.Compute -Name '*VM*'
Cmdlet help and examples
Help content can be viewed for a cmdlet by using the Get-Help cmdlet:
Get-Help -Name Get-AzSubscription Get-Help -Name Get-AzSubscription -Examples Get-Help -Name Get-AzSubscription -Full Get-Help -Name Get-AzSubscription -Online