dcsimg
Read more on "Server Virtualization Spotlight" »

Installing and Using Microsoft Azure PowerShell Version 1.0

By Nirmal Sharma (Send Email)
Posted January 4, 2016


Over a short period of time, we have seen massive improvements in the Microsoft Azure Public Cloud. Apart from using Azure Portals to manage Azure resources, you can also use Azure PowerShell cmdlets to interact with Azure resources from the command line.

Microsoft recently released Azure PowerShell Version 1.0, and while it's still in preview release, it is already being used by many Azure subscribers. Windows Server TutorialsIt is important to note that both Azure PowerShell Version 0.9.8 and Version 1.0 are in use.

We previously published a server tutorial on using Azure PowerShell version 0.9.8 that you can find here: http://www.serverwatch.com/server-tutorials/managing-windows-azure-resources-using-powershell.html.

Benefits of Using Azure PowerShell Version 1.0

While you can use previous version of Azure PowerShell cmdlets to interact with Azure resources from the command line, there are several new benefits offered by Azure PowerShell Version 1.0, including:

  • There is no need to use Switch-AzureMode if you need to work with Resource Manager resources or Resource Groups in an Azure subscription. In previous versions of Azure PowerShell you had to use the Switch-AzureMode -Name AzureResourceManager if you needed to interact with Resource Group or its resources.
  • Separation of Service Management (SM or SMA) and Resource Manager cmdlets. All of the Resource Manager cmdlets have "AzureRM" added to them, so it is easy to identify a cmdlet. Azure PowerShell Version 1.0 ships with both Service Management and Azure Resource Manager cmdlets.
  • Reduces complexity when designing scripts to interact with Azure resources. Azure PowerShell Version 1.0 provides greater flexibility when interacting with Azure resources and also helps in reducing complexity in the code.

Installing Azure PowerShell Version 1.0

To install Azure PowerShell version 1.0, first visit http://go.microsoft.com/fwlink/p/?LinkId=320376 and then run Microsoft Web Platform Installer. Once you have installed Azure PowerShell Version 1.0, search for "Microsoft Azure PowerShell" in the Start menu and click to open Azure PowerShell prompt.

The first Azure cmdlet you should run is the Login-AzureRMAccount. Login-AzureRMAccount allows you to log in to an Azure subscription before you start using other Azure cmdlets.

It is important to note that all the PowerShell cmdlets available with Version 1.0 have "AzureRM" added to them. To get a list of all Resource Manager cmdlets available in version 1.0, run the following command:

  • Get-Command -Noun AzureRM*

Using Azure PowerShell Version 1.0

Here are some examples you might find useful when working with Azure Resource Manager resources.

To get a list of Azure Storage accounts and its creation time, run below command:

  • Get-AzureRmStorageAccount | Select-Object StorageAccountName,CreationTime

In case you need to export the output of this command to a CSV file, simply add the "Export-CSV" cmdlet and a CSV file name as shown in the command below:

  • Get-AzureRmStorageAccount | Select-Object StorageAccountName,CreationTime | Export-CSV StorageAccountsWithTime.CSV

Note that you can always use SMA cmdlets with Azure RM cmdlets. For example, if you need to get a list of all page blobs (virtual machine VHD files), run this command:

  • Get-AzureRmStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob

This command gets all the virtual machine VHD files from all the storage account containers created in Azure. Note that the Get-AzureRMStorageAccount cmdlet is a RM cmdlet and Get-AzureStorageContainer and Get-AzureStorageBlob cmdlets are SMA cmdlets.

In case you need to get blob pages from a specified storage container, you can specify the container name as shown below:

  • $ContainerName="MyCustomVHDs"
  • Get-AzureRmStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob -Container "$ContainerName"

Note that this command returns block blobs in addition to listing page blobs. If you only need to list the page blobs created in all storage containers, you'll need to use a For-Each loop and IF condition as shown in the command below:

$AllVHDs=Get-AzureRmStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob
ForEach ($AllItems in $AllVHDs)
{
      $TotGBNow = 0
      IF ($AllItems.BlobType -eq "PageBlob")
     {
          $VHDLN = $AllItems.Length/1024/1024/1024
          $RString = $AllITems.Name+","+$VHDLN+" GB"
          Write-Host $RString
      }
}

In addition to getting only page blobs (VHD files), this command also converts the bytes to gigabytes.

If you need to get the total GBs used by all or only specific storage accounts, use the following PowerShell commands.

To get GBs used by all storage accounts created in an Azure subscription, run this command:

$A=Get-AzureRmStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob | Measure-Object Length -Sum | select-object sum
$TotalGB = $A.Sum/1024/1024/1024
Write-Host $TotalGB "GB"

And to get GBs used by a specific storage account, run this command:

$StorageAccount = "MyStorageAccount"
$A=Get-AzureRmStorageAccount -Name $StorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob | Measure-Object Length -Sum | select-object sum
$TotalGB = $A.Sum/1024/1024/1024
Write-Host $TotalGB "GB"

If you are using the new Azure resource model (ARM), you would have first created storage accounts in the Resource Groups. Since Azure PowerShell version 1.0 has been designed keeping the new Azure resource model in mind, all cmdlets in version 1.0 support specifying a Resource Group name wherever applicable.

If you have created multiple Resource Groups in Azure and need to get storage account details or run any of the above series of commands against a specific Resource Group, use the "-ResourceGroupName" parameter. For example, to get a list of all the storage accounts created in a specific Resource Group, use the command below:

$ResGroupName = "MyResourceGroup"
Get-AzureRmStorageAccount -ResourceGroupName $ResGroupName

And if you wish to collect total GBs used by a storage account in a specific Resource Group, execute the following:

$ResGroupName = "MyResourceGroup"
$StorageAccount = "MyStorageAccount"
$A=Get-AzureRmStorageAccount -ResourceGroupName $ResGroupName -Name $StorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob | Measure-Object Length -Sum | select-object sum
$TotalGB = $A.Sum/1024/1024/1024
Write-Host $TotalGB "GB"


Nirmal Sharma is a MCSEx3, MCITP and Microsoft MVP in Directory Services. He specializes in directory services, Microsoft Azure, Failover clusters, Hyper-V, System Center and Exchange Servers, and has been involved with Microsoft technologies since 1994. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles on various sites and contributing to Health Packs for ADHealthProf.ITDynamicPacks.Net solutions. Nirmal can be reached at nirmal_sharma@mvps.org.

Follow ServerWatch on Twitter and on Facebook

Page 1 of 1

Read more on "Server Virtualization Spotlight" »

Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.


 

 


Thanks for your registration, follow us on our social networks to keep up-to-date