This project contains several scripts that demonstrate how to use a simple technique to extract, transform, and generate an inventory of virtual machines (VMs) associated with a subscription. This will include VMs using both the Azure Resource Manager (ARM) and Classic (ASM) deployment models.
Read the accompanying How-To Guide:
There are three main parts to the example code:
An extraction process to pull the information on VMs directly from the Azure Management APIs using either PowerShell on Windows or the Azure CLI tool on Linux. This process produces several detailed JSON files.
A transform process using Python scripts to combine and regularize the information extracted from the APIs, and output that data to a combined JSON file.
An Inventory process to convert the transformed JSON file into easily readable CSV files.
Python 3.0 or higher
Azure PowerShell (if extracting using PowerShell for Windows)
The jq JSON processor is required if extracting using Linux
Save the samplecode folder to a location on your local machine. This folder contains a sample pyproj file and Visual Studio solution file for the project. It also contains the following sub-folders:
extract - contains the PowerShell and Bash scripts used to extract information from the Azure Management APIs
transform - contains the transform Python script
inventory - contains the inventory Python script that will generate human readable CSV files
data - location where extracted JSON data and output CSVs will be stored
sampledata - contains pre-generated sample files
JMESPath.py library for JSON processing in Python
Admin or Co-admin rights to the subscription you want to extract information from.
Extraction on Windows
To run the PowerShell scripts on Windows, simply open PowerShell and navigate to the samplecode\extract\windows folder and run:
.\extract.ps1 <Subscripton ID>
The script will ask you to login two times: once for ARM mode and again for ASM mode. On completion, several files will be generated with the VM and storage account information for the subscription you specified. These files will be stored in the samplecode\data\<Subscription ID> folder.
Extraction on Linux
Before running the extraction script, you need to login to the Azure CLI apps. In CLI 2.0 you can log on to a subscription using:
In CLI 1.0, you can log on to a subscription using:
Once both are logged in navigate to the samplecode/extract/linux folder and run:
bash extract.sh <Subscripton ID>
On completion, several files will be generated with the VM and storage account information for the subscription you specified. These files will be stored in the samplecode/data/<Subscription ID> folder.
Navigate to the folder samplecode/transform and run:
python transform.py <Subscripton ID>
This will load the data from the specified subscription and generate a combined transformeddata.json file in the samplecode/data/<Subscription ID> folder.
Navigate to the folder samplecode/inventory and run:
python inventory.py <Subscripton ID>
This will load the transformeddata.json file and generate two CSV files in the samplecode/data/<Subscription ID> folder:
vmInventory.csv - a list of VMs and information about them
storageAccountInventory.csv - a list of all storage drives used by the VMs