Managing Azure Subscription using Azure PowerShell
What is Azure PowerShell
Azure PowerShell is an extension of Windows PowerShell to provide cmdlets for the management of Azure resources and cloud services. It is more manageable than the portal, for instance, if we want to stop all VMs in one go, we cannot do that from the portal we need to stop one by one but from Azure PowerShell, we can stop all the VMs in one go.
Why use Azure PowerShell to Manage Azure Resources
- Provision and manage environment quickly.
- Some features are only available to PowerShell.
- Perform multiple operations simultaneously.
- DSC(Desired State Configuration) and Automation.
we will discuss DSC(Desired State Configuration) and automation in the upcoming post.
Install Azure PowerShell
Prerequisites
- .Net framework 4.5 or higher: we can check the .net version by following the PowerShell command.
Get-WindowsFeature *framework*
If the .net framework is not installed then we can install using the below PowerShell command.
Install-WindowsFeature Net-framework-45-core.
2. PowerShell version should be 4 or higher, we can check the PowerShell version by the command.
Get-Host
Microsoft Web Platform Installer
Download Microsoft Web Platform Installer from here and install it on the management computer. The current Web Platform Installer version is 5.0, click on the Free Download button.
Run the setup and click on Add button for Microsoft Azure PowerShell.
Click on the Install button.
Accept the license agreement.
The installation has started.
Azure PowerShell has been installed successfully.
Verify Azure PowerShell has been installed
To verify that Azure PowerShell has been installed in the management computer by the below command.
Get-Module -listavailable *azure*
We can see the Azure PowerShell module has been installed.
Importing Azure Publish Settings File
Get PublishSettings File
To make a secure connection between the management computer and Azure Subscription we need to import publish settings file by the below command.
Get-AzurePublishSettingsFile
After hitting the Enter key, it will prompt you to log in to Azure Subscription. Login with your valid azure account.
After providing the subscription credential, the file will be downloaded.
Import the PublishSettings file
Copy the publish settings file into the C drive of the local computer.
Import the certificate using the below PowerShell command. If we have more than one subscription then we need to repeat the step for each subscription. we don’t need local account privilege to run the command.
Import-AzurePublishSettingsFile -PublishSettingsFile ‘C:\Visual Studio Enterprise-7-16-2017-credentials.publishsettings’
Now, the management computer is securely connected to the Azure subscription. Afterward, we can run the azure command in the PowerShell console without any authentication as it is already authenticated for the computers.
This certificate will only allow to access ASM(Azure Service Manager) commands but not ARM(Azure Resource Manager) commands.
Note: who has not known the difference between ASM and ARM command, ARM command contains Rm
ASM command: Get-AzureVm, ASR Command: Get-AzureRmVm
We can see in the above image Get-AzureVm command is working but Get-AzureRmVm command is not working, to access the ARM command we need to log in with Login-AzureRmAccount.
Access Azure Resource Manager (ARM)
To access resources from ARM run the below command and provide subscription credentials.
Now, we have successfully signed in to ARM and we can access the resources as shown below.
To provision and manage VMs using Azure PowerShell visit this link.