Connecting Powershell To Azure Account

Hi,

Apart from managing your Azure setup from management portal you can do it through your PowerShell console as well, additionally it helps in automating your task too, so let’s see how to connect your PowerShell to Azure account.

1. How to list cmdlet modules in PowerShell
Windows PowerShell module exist in two states, either they are loaded or are available within your setup but aren’t loaded yet, first we will see how to list down modules which are loaded in your PowerShell, then we will see how to list down all the cmdlet available in your system but not loaded yet.

2. How to list loaded module in PowerShell
You can use the “Get-Module” cmdlet without any parameters to list down loaded module in Powershell

01-Get-Module-All

3. How to list all modules in PowerShell
To obtain list of all modules available in system but might not be loaded in Windows PowerShell console, you can use the same cmdlet “Get-Module” but with additional parameters

02-Get-Module-List-Available

4. If you do not see Azure cmdlets in your system, than you’ll have to install them, let’s see how to do that.

5. The easiest way is to do it from Microsoft Web Platform installer, the web platform installer will not only install the cmdlet, but it will also resolve all the dependencies and install them as well, so that makes it very easier for you.

So open your preferred browser and go to Microsoft Azure Downloads

Once you are in download section of command line tools you can click on install and that should download the web platform installer, rest of the installation of cmdlet then should be done from web platform installer.

03-Webinstaller-from-microsoft

6. Once you have downloaded the web installer and started the deployment the first screen would look like something as below, from there on rest of is just clicking Next button.

04-Web-installer

 

7. Once you have installed the module you can search in PowerShell for Azure module.

05-Get-command-Azure

8. Let’s connect your powershell to Azure
There are couple of ways you can add the Azure account to Windows PowerShell. You can use the “Add-AzureAccount” cmdlet, which uses Azure Active Directory authentication access tokens, or you can do it through “Import-AzurePublishSettingsFile”, which uses a management certificate, which you have to download first.

8.1
The “Add-AzureAccount” cmdlet makes your Azure Account and its subscriptions available in Windows PowerShell. It’s like logging in to your Azure account in Windows PowerShell.
When you run Add-AzureAccount, it displays an interactive windows that prompts you to sign into your Azure account. This sign-in is valid until the access token expires. When it expires, cmdlet that require access to your account prompt you to run Add-AzureAccount again.
I guess the access token TTL is one hour, but that’s just my guess.

06-Add-Azureaccount

As you can see above, you can simply write “Add-AzureAccount” cmdlet in PowerShell console without any option and it will prompt you for your username and then password.

8.2 Verify your Azure Account
Once you have successfully logged in, it would be good to verify your account and ensure you have logged in to correct account, you might have multiple accounts.

To verify your account, you can run following command.

“Get-AzureSubscription”

08-Get-AzureSubscription

8.3 Connect your Powershell to Azure using “Get-AzurePublishingsettingsFile” and “Import-AzurePublishSettingsFile”

With “Get-AzurePublishingSettingsFile” you have to download the publishing profile from Azure management portal and store it as some secure path.

Once you have downloaded the profile you have to than import in PowerShell with “Import-AzurePublishSettingsFile”, the import command would like something as below.

PS C:\Windows\system32> Import-AzurePublishSettingsFile “Path to your file you downloaded from portal”

Note: Ensure to mention your path in double quotes

8.4 Once you have managed to connect PS to Azure, then let’s verify it for last time.

08-Get-AzureSubscription

9. For some reason if you want to remove this subscription from your PowerShell then you can use the following cmdlet to remove.

#Remove-AzureSubscription

The Remove-AzureSubscription cmdlet deletes an Azure subscription from your subscription data file so Windows PowerShell can’t find it. This cmdlet does not delete the subscription from Microsoft Azure, or change the actual subscription in any way.

Required parameter is -SubscriptionName

so the final cmdlet would be

#Remove-AzureSubscription -SubscriptionName “whatever is your subscription name”

If you have any query/question write to hardeepbhamra@gmail.com

Thanks

Hardeep

Leave a Reply

Your email address will not be published. Required fields are marked *