Auditing Azure VMs Add Results to Azure Tables | Azure PowerShell

Having read Paulo Marques article Working with Azure Storage Tables from PowerShell I decided to make the edits to my auditing scripts and push the results into Azure Tables to act as a repository I have the ability to keep but also one that gives me more options. Moving forward we can look to update or pull this information out on demand or use it as a basis of a comparison.  I find it quite useful to have an independent record of the starting and end state of an environment pre and post any work undertaken.

There are a number of ways you can audit an Azure environment. With most of my customers I have implemented OMS, often using a combination of paid and free tiers to achieve the reporting they need to meet their own requirements and standards.

I’m a big fan of OMS, this script represent only one way to gather information and a chance to try something new in PowerShell.

To get started you’ll need to follow the instructions in Paulo’s article to install the correct module and from there I suggest following his guide as this will give you a good understanding how the commands operate. Once competed it is a straight forward process to integrate this in to any auditing script you currently have. The example below already has a table created.


# Variables for the environment set up 
# PLEASE NOTE the Azure Table has already been set up
$subscriptionName = "Subscription Name"
$resourceGroup = "Resource Group"
$storageAccount = "Storage Account"
$tableName = "Table"
$PartitionKey = "Partition Key"
$table = Get-AzureStorageTableTable -resourceGroup $resourceGroup -tableName $tableName -storageAccountName $storageAccount


# Call the status of each server and upload into the Azure Table
$rowcounter = 1 
$RGs = Get-AzureRMResourceGroup 
  
  foreach($RG in $RGs) 
   { 
     $VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName 
     foreach($VM in $VMs)
      
     { 
      $VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status 
      
      foreach ($VMStatus in $VMDetail.Statuses) 
       {                                             
        $VMStatusDetail = $VMStatus.DisplayStatus                                               
                                           
       }
    
        Add-StorageTableRow -table $table `
        -partitionKey $PartitionKey `
        -rowKey ([guid]::NewGuid().tostring()) `
        -property @{"ResourceGroup"=$RG.ResourceGroupName;"computerName"=$VM.name;"status"=$VMStatusDetail}
       $rowcounter++  

       } 
    }

This second example updates the values in the Azure Table. To do this we have to pull out the Computer / Server status from the Table and add this to the collected information as before.

# Variables for the environment set up 
# PLEASE NOTE the Azure Table has already been set up
$subscriptionName = "Wade - Internal Consumption"
$resourceGroup = "rg-ause-test-platform"
$storageAccount = "rgausetestplatform626"
$tableName = "table01"
$PartitionKey = "AUSSite"
$table = Get-AzureStorageTableTable -resourceGroup $resourceGroup -tableName $tableName -storageAccountName $storageAccount


# Call the status of each server and upload into the Azure Table
$rowcounter = 1 
$RGs = Get-AzureRMResourceGroup 
  
  foreach($RG in $RGs) 
   { 
     $VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName 
     foreach($VM in $VMs)
      
     { 
      $VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status 
      
      foreach ($VMStatus in $VMDetail.Statuses) 
       {                                             
        $VMStatusDetail = $VMStatus.DisplayStatus                                               
                                           
       }
    
# Creating the filter and getting original entity
[string]$filter = [Microsoft.WindowsAzure.Storage.Table.TableQuery]::GenerateFilterCondition("computerName ",[Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::Equal,$VM.Name)
$computer = Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $filter

# Changing values
$computer.status = $VMStatusDetail

# Updating the content
$computer | Update-AzureStorageTableRow -table $table

# Getting the entity again to check the changes
Get-AzureStorageTableRowByCustomFilter -table $table -customFilter $filter 

       } 
    }

Remember there is always a better way to do things and the only way we find out is if you have a go at sharing. I look forward to your versions and updates. Happy scripting!

Disclaimer:  Please note although I work for Microsoft the information provided here does not represent an official Microsoft position and is provided as is.

(Visited 59 times, 7 visits today)

Leave a Reply

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