I know that this is a recurrent subject in several blogs and some are very good indeed but I would like to write about it anyways, using a recurrent question that I have seen from customers and peers and to be paired with my previous blog Working with Azure Active Directory Graph Api from Powershell, so you have a one stop shop for both APIs from my blogs.
The full script that is mentioned in this blog can be downloaded from the TechNet Script Gallery from here.
The example I’m posting is a sample script that helps us answer the following questions related to Storage Accounts and blobs in Azure Resource Manager deployment model, what is the status of all VHD files throughout the storage accounts of my subscription? Are they attached to a VM? Which VM? Are they not attached at all?
The process that I’m using here is:
- Get an authorization token in order to list all storage accounts from a defined subscription.
- Using Azure Resource Manager REST API, list all storage accounts within subscription.
- Using Azure Storage Resource Provider REST API, get keys to be able to build the authentication signature when working with containers and blobs.
- Using Azure Blob Service REST API, list containers within each storage account, this is the time where we need to create the signature string to authenticate on Blob Service REST API, basically this string follows a well-defined format that is signed with storage account primary or secondary keys with the canonicalized string of the action being taken on a specific storage account, this signed string is called shared key and is fully described at Authentication for the Azure Storage Services article.
- Following the same pattern of item 4, we get a list of blobs within each container also using the shared key obtained from the script function called GetAuthSignedStringSa.
- For each blob we grab all needed information and build the blob Powershell Custom Object filling up its properties and adding it to a result array.
This sample script is not intended to be a full featured script or cmdlet but it at least will list all page blobs and block blobs (optionally) in all storage accounts that you have in your subscription. It shows two different ways to work with REST API, the resource manager APIs which only needs the authorization token from Azure Active Directory and the blob service API which requires another kind of authentication which is the shared key, a well-formatted string signed with the storage account key (which requires the token to be obtained) using HMAC-SHA256 signature algorithm.
This blog post assumes you already have Azure Powershell 1.0 or greater module installed in your computer, if not, you can install it from https://aka.ms/webpi-azps.
In the same way as Graph API (mentioned in the Graph API blog post) the first thing we need to start working with REST API is to obtain an authorization token by using ADAL (Active Directory Authentication Library).
Let’s get started by first defining a new version (more flexible, can be used with Graph API or REST API) of the same function that I posted previously:
Notice that this version of the function a new parameter was added, ApiEndpointUri, the value of the argument can either be https://management.core.windows.net/ for Azure Management API or https://graph.windows.net for Graph API, the focus of this blog post is the first one.
I’m only including/commenting the most important parts of the script; the fully functional script can be downloaded from here (link to a blob in my storage account which is using another type of authentication method called Shared Access Signature (SAS) tokens where I provide read only access directly to my blob for a specific period of time.
I defined a bunch of auxiliary functions mainly to help obtaining specific information from an URI used in the REST API calls to avoid lots of repetitive code. I’m not pasting the code here but it is in the full script version mentioned in the previous paragraph. Mainly the function names are:
Continuing with our example on main parts of the script on how to use REST API calls with Powershell, the next code snippet will obtain the authorization token that is used on all calls to the Resource Manager API:
After we get this token, we build the $header hashtable that is used by Invoke-RestMethod cmdlet and we perform the api call:
If you take a look on the $header hashtable you will be able to see the authorization token:
Also, lets examine $storageAccounts results, this is the list of all storage accounts based on Azure Resource Manager deployment model of my subscription, those came from the REST API call to Resource Manager provider:
At this point we have our main iteration list to go through, in the real sample script execution it will iterate over all storage accounts and all containers and list all blobs, just for the sake of simplicity I’m executing parts of the script and focusing on one storage account, called pmcstorage05, so I’m tweaking my $uriSAs URI object to filter the results for just this storage account, follows the modified version of it to get exactly that storage account and executing the API call again:
The result now is a single storage account, the one I defined in my REST API query:
From this point, most of the calls will be directed to the Blob Service REST API which will use a different authentication method but before going into this, we need a last Resource Manager REST API call, the call that will get the authentication keys from that storage account. Following code snippet defines the query (which is now a modified version from the original script), notice that the Invoke-RestMethod cmdlet now uses the POST method instead of previous GET operations on other parts of the code:
The $keys object with key1 and key2 attributes as we can see below, any of these keys can be used to build the singed Shared Key to perfom the Blob REST API calls:
The original published sample script at the aforementioned link at this point would list all containers inside the storage account (with exception of special $root which is not covered in this blog), but to make this blog as small and objective as possible, we are also going to tweak the next calls to use the vhds container directly so we skip some code.
Lets define our REST API call URI to list all blobs with their metadata (if available):
- comp=list => the list operation inside the vhds container
- include=metadata => indicates the API to get any associated metadata (we need this when a page blob is attached to a VM)
- restype=container => indicates that this operation is on a container resource type
- For more information of available options, please refer to Blob Service REST API
The value of the URI will be like this:
Now comes the part that was trickier to work with (just my opinion), this is the part where we build the string that must be signed with the storage account key. For that I built a function to that work for us, outlined below:
What basically this function do is build and sign the following string that is derived from the URI that we defined above (remember those little functions that I just mentioned? They basically do some string operations to get what we need):
This string seems odd, it contains some empty lines, this is because they follow the format, each line have a meaning, defined in Authentication for the Azure Storage Services document.
After it builds this string, it signs it with HMAC-SHA256 algorithm using UTF8 encoding and builds another hashtable for us just like this:
In possession of this header, we can go and perform our REST API call to obtain the list of blobs located at that container in that storage account. The following code snipet performs the call and converts the response to XML (there is no JSON return for this particular REST API):
The XML schema of this response looks like this (this schema is defined at List Blobs article):
And since this is converted to an XML object in Powershell, we can dig into it and obtain all information needed. For example the list of returned blobs:
And from this example we see that only blob called win1020162262259.vhd contains a Metadata object, I can tell in advance that it means that this VHD is attached to a VM, so lets use this line of code to check what properties it have:
Finally we see to which VM this VHD is attached to and disk type (if it is a DataDisk or OSDisk), etc.
Executing the script
Basically these are the most important explanations for this example, now, how to execute the full script? What type of output it provides? Can I filter it?
To execute the script, follow these steps:
- Download from here (TechNet Gallery)
- Extract it anywhere
- Open a Powershell command prompt
- Optionally authenticate to azure resource manager with
- Type user username and password of an Azure AD account or MSA account that has admin rights to the subscription or one or more resource groups (when using RBAC – Role based access control)
- Switch to the folder where you extracted the script and run the following command line (you must know beforehand your subscription ID and your Azure Active Directory tenant name)
- That will return the blob objects to a list called $blobs, just type $blobs and you will get a list of all of them:
Final question, can you play with these objects? Yes, to get a list of non-attached page blobs (VHD file for example) just use this command line:
I have a bunch of them (screen shot of just first 4):
For a list of all available REST APIs, please refer to Azure Reference documentation.
Well, that’s it for this blob, I mean blog , have fun and be careful if deleting any blob that comes as not attached, be very sure you don’t need it before deleting it .