Author: Manjunath

Manjunath is an IT enthusiast with experience in Cloud services platform and PowerShlell scripting. He blogs about public cloud platform and scripting techniques.

PowerShell – Delete Azure blobs older than X number of days

As a cost optimization strategy, organizations decide to retain data that are certain days old and delete the old data.

The same strategy can be implemented in Azure Storage. Let’s say if our application requires data that are 60 days old, then our approach is to retain only 60 days of data. And delete any blob that is older than 60 days.

This script deletes Azure blobs that are older than X days. Here ‘X’ is the number of days that you want to retain the data. (60, as stated in my example)

Download the script

You can create an Azure Automation Runbook from this script and schedule it to run every day. So, you will not be billed for the unwanted data.

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

PowerShell – Fetch Azure Page Blobs from an Azure subscription

This script fetches the details of PAGE BLOB across the Azure subscription and saves it as a CSV file. The CSV file will be saved under the location from where the script was run.

The general use case could be to understand how many VHD files are present in your subscription. These could be your OS Disks, Datadisks or your VM snapshots.

Download Script Link

If you are looking for a script that generates a report for “unattached” managed and un-managed disks, then please visit the below link:

AZURE – GENERATE REPORT FOR UNATTACHED AZURE DISKS (MANAGED AND UN-MANAGED)

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

Azure – Generate report for unattached Azure disks (managed and un-managed)

When you delete a virtual machine (VM) in Azure, by default, any disks that are attached to the VM aren’t deleted. This feature helps to prevent data loss due to the unintentional deletion of VMs. After a VM is deleted, you will continue to pay for unattached disks.

Unattached MANAGED disks:

When a managed disk is attached to a VM, the ManagedBy property contains the resource ID of the VM. When a managed disk is unattached, the ManagedBy property is null. The script examines all the managed disks in an Azure subscription. When the script locates a managed disk with the ManagedBy property set to null, the script determines that the disk is unattached.

Unattached UN-MANAGED disks:

When an unmanaged disk is attached to a VM, the LeaseStatus property is set to Locked. When an unmanaged disk is unattached, the LeaseStatus property is set to Unlocked. The script examines all the unmanaged disks in all the Azure storage accounts in an Azure subscription. When the script locates an unmanaged disk with a LeaseStatus property set to Unlocked, the script determines that the disk is unattached.

SCRIPT:

Download the script here

PowerShell script to generate a report of unattached VHD disks. This script will create two files – unattached_managed_disks.csv, unattached_un_managed_disks.csv

These two files will contain details about VHD files that are not attached to an Azure virtual machine.

NOTE: You have to login into your account before running the script. “login-azurermaccount” to log in to your account.

You can use the generated CSV to better manage your Azure infrastructure. Understand why the disks are not in use and take an informed decision on whether you want to delete or re-use them. Thus helping you to identify resources that are not being utilized and to reduce cost.

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

Azure – Who de-allocated my virtual machine?

Many a time we might want to know details about certain operations performed on our Azure resources.

Once such case study would be to track how many virtual machines are being de-allocated by users, so we can make a decision on not to monitor them.

I have written a simple script that would make the tracking easy.

Download the script

This script will fetch information of certain Azure operation against Azure resources and create a CSV file. Specifically, this script will create a CSV file that contains a list of Azure operations that de-allocates an Azure virtual machine.

You may alter the IF condition statement to produce desired results.

Example, fetch operational logs for Azure Storage only. Or fetch operational logs for re-start VM or any operation on any Azure resource.

The CSV file will be saved in the same folder from where you run the script and will be saved as “Azure_activity_logs.csv”

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

Azure – Audit report (Azure automation runbook)

The PowerShell script is an Azure automation runbook that pulls the below data and populates the data into a CSV file. The script then summarizes the data into an email’s body and sends an email to the recipient with the CSV files as attachments.

If the Azure automation runbook is scheduled to run every day, you will get a summary/high-level view of what is happening in your environment to your email box. The email could be the first report any organization’s high management would desire to look at.

1. Count of De-allocated Azure virtual machines

2. Count of Running Azure virtual machines

3. Count of Stopped Azure virtual machines

4. Count of Azure virtual machines that do not have native backup configured (Azure Back up and Recovery service)

5. Count of Inbound Security rules that causes vulnerability

Download the script

Sample Summary:

Screenshot from 2018-06-04 19-13-52

Email is sent via SendGrid service. You need to update the script with your SendGrid credentials.

You may choose a “Free Tier” pricing for SendGrid. Below is documentation to create a SendGrid account:

https://docs.microsoft.com/en-us/azure/sendgrid-dotnet-how-to-send-email

Note: The script is an Azure Automation runbook. You have to run it from an Azure Automation account.

If you would like me to add more data that would be useful as an Azure audit report, please let me know.

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

Azure – Install exe files (BigFix) on Azure windows virtual machine using Azure Custom Script Extension (CSE)

What is custom script extension?

The Custom Script Extension downloads and executes scripts on Azure virtual machines. This extension is useful for post-deployment configuration, software installation, or any other configuration/management task. Scripts can be downloaded from Azure storage or GitHub, or provided to the Azure portal at extension runtime. The Custom Script extension integrates with Azure Resource Manager templates, and can also be run using the Azure CLI, PowerShell, Azure portal, or the Azure Virtual Machine REST API.

This document details on how to use Custom Script Extension using the Azure PowerShell Module against an already provisioned Azure Windows virtual machine to install BigFix client.

Pre-requisites:

Operating System

The Custom Script Extension for Windows can be run on Windows 10 Client, Windows Server 2008 R2, 2012, 2012 R2, and 2016 releases.

Script Location

The script needs to be stored in Azure Blob storage, or any other location accessible through a valid URL.

Internet Connectivity

The Custom Script Extension for Windows requires that the target virtual machine is connected to the internet.

The BigFix client files are stored in the storage account:

1

We shall be naming the extension as “bigfixinstallextension.” Make sure that an extension with the same name already does not exist.

Step 1: Get the Azure virtual machine config object

$vm = get-azurermvm -ResourceGroupName “datadog-test” -Name “dg-private-1”

Step 2: Query the Virtual Machine object for existing extensions:

$vm.Extensions

You should see an output similar to below if it does not have any custom extensions.

2

Note: any azure virtual machine will have one default extension – “MicrosoftMonitoringAgent.” This is because Azure installs “Microsoft Monitoring Agent” on every virtual machine. Make sure, the virtual machine does not have another extension with the name “ bigfixinstallextension.” If it does have, we have to remove that extension.

Below link provides an Azure Powershell cmdlet to remove the extension:

https://docs.microsoft.com/en-us/powershell/module/azurerm.compute/remove-azurermvmextension?view=azurermps-5.5.0

Once, we have confirmed that a custom extension with name “ bigfixinstallextension” does not exists, we can proceed in adding one. Below is the powershell code:

# Resource group of virtual machine

$resource_group = “datadog-test”

# location of virtual machine

$location = “East US 2”

# azure virtual machine name

$vm_name = “dg-private-1”

# storage account name where the custom script is stored

$storage_account_name = “xxxx”

# storage account key of where the custom script is stored

$storage_account_key = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”

# custom script file name

$file_name = “azure_custom_script_execution_install_bigfix.ps1”

# container name where the custom script is stored

$container_name = “msifiles”

# extension name for the custom script extension

$extension_name = “bigfixinstallextension”

# azure powershell cmdlet to execute add the custom script extension and to execute the powershell file

Set-AzureRmVMCustomScriptExtension -ResourceGroupName $resource_group -Location $location -VMName $vm_name -Name $extension_name -TypeHandlerVersion “1.1” -StorageAccountName $storage_account_name -StorageAccountKey $storage_account_key -FileName $file_name -ContainerName $container_name

Output:

4

Now login to the Azure windows virtual machine to confirm if the BigFix client is installed and running:

5

The downloaded file can be found inside the virtual machine at the below file path:

C:\Packages\Plugins\Microsoft.Compute.CustomScriptExtension\1.9\Downloads\1

Extension execution output is logged to files found under the following directory on the target virtual machine. For troubleshooting.

C:\WindowsAzure\Logs\Plugins\Microsoft.Compute.CustomScriptExtension

Explaining the PowerShell scriptazure_custom_script_execution_install_bigfix.ps1

This script gets executed as part of the Custom Script Execution. And it is responsible for installing the BigFix agent.

Below is the code:

# Create a directory to hold BigFix files

new-item ‘c:\bigfix’ -ItemType directory

# Copy BigFix files from Azure storage to local directory

Invoke-WebRequest -Uri https://customsc.blob.core.windows.net/msifiles/clientsettings.cfg -outfile ‘c:\bigfix\clientsettings.cfg’

Invoke-WebRequest -Uri https://customsc.blob.core.windows.net/msifiles/masthead.afxm -outfile ‘c:\bigfix\masthead.afxm’

Invoke-WebRequest -Uri https://customsc.blob.core.windows.net/msifiles/setup.exe -outfile ‘c:\bigfix\setup.exe’

# Execute the setup file

$arguments = “/S /v/qn”

$filepath = “c:\bigfix\setup.exe”

Start-Process $filepath $arguments -wait

Execution Flow:

1. Create a directory to hold big fix files.

2. Copy the three files associated with BigFix installation to the directory created in Step 1.

3. Execute the setup file in silent mode.

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

PowerShell – Create local user and add them to administrator group

Creating a local user in a windows server and adding them to local administrator group seems to be a simple task. However, when you have to add many users, the manual task becomes very tedious.

The obvious solution is automation. We shall use PowerShell scripting to achieve this. Below is the script that we can use to run. This script will create local users and add them local administrator group.

# Create an array to contain user name
$user_list = @("aabb","bbcc","ccdd")

# Create an array to contain user full name
$user_fullname_list = @("aa bb", "bb cc", "cc dd")

# Iterate over the user names
for($i=0; $i -lt $user_list.Length; $i++){

# Create the users
 $Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"
 $LocalAdmin = $Computer.Create("User", $user_list[$i])
 $LocalAdmin.SetPassword("Password01")
 $LocalAdmin.SetInfo()
 $LocalAdmin.FullName = $user_fullname_list[$i]
 $LocalAdmin.SetInfo()
 $LocalAdmin.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
 $LocalAdmin.SetInfo()

# Add the users to administrators group
 $group = [ADSI]"WinNT://$Env:COMPUTERNAME/Administrators,group"
 $group.Add("WinNT://"+$Env:COMPUTERNAME+"/"+$user_list[$i])

}

Further enhancement, is to save the user_list and user_fullname_list into a text file and then read the data from the files into the script.

Click here to download my PowerShell scripts for Free !!

Visit my youtube channel for videos on Azure:

https://www.youtube.com/channel/UCcpEleEBpulMrmIqBDTkfSg

Azure – Collecting performance metrics for Azure virtual machines

Azure Monitor provides several ways to interact with metrics, including charting them in the portal, accessing them through the REST API, or querying them using PowerShell or CLI.

In this blog, we shall learn how to fetch the metrics for our Azure Virtual Machines using PowerShell. The script that I provide can be used as a utility to generate quick reports.

Below is the script:

<#
AUTHOR:
Manjunath Rao
DATE:
February 21, 2018
DESCRIPTION:
The script will generate performance metrics (as recorded by Azure agent) from Azure virtual machines and then populate into an excel sheet.
REFERENCE:
https://docs.microsoft.com/en-us/azure/monitoring-and-diagnostics/monitoring-supported-metrics
#>
$ErrorActionPreference = “SilentlyContinue”
# Login to Azure Account
try
{
Login-AzureRmAccount -ErrorAction Stop
}
catch
{
# The exception lands in [Microsoft.Azure.Commands.Common.Authentication.AadAuthenticationCanceledException]
Write-Host “User Cancelled The Authentication” -ForegroundColor Yellow
exit
}
# Prompting the user to select the subscription
Get-AzureRmSubscription | Out-GridView -OutputMode Single -Title “Please select a subscription” | ForEach-Object {$selectedSubscriptionID = $PSItem.SubscriptionId}
Write-Host “You have selected the subscription: $selectedSubscriptionID. Proceeding with fetching the inventory. `n” -ForegroundColor green
# Setting the selected subscription
Select-AzureRmSubscription -SubscriptionId $selectedSubscriptionID
# Get the list of resource groups
$resourcegroup_list = (get-azurermresourcegroup).resourcegroupname
try{
# Create an Excel COM Object
$excel = New-Object -ComObject excel.application
}catch{
Write-Host “Something went wrong in creating excel. Make sure you have MSOffice installed to access MSExcel. Please try running the script again. `n” -ForegroundColor Yellow
}
# Create a Workbook
$workbook = $excel.Workbooks.Add()
# Creating a directory overrides if any directory exists with the same name
Write-Host “Creating a directory: C:\AzurePerformanceMetrics. This operation will override if you have a directory with the same name. `n” -ForegroundColor Yellow
New-Item C:\AzurePerformanceMetrics -Type Directory -Force
Write-Host “Creating the Performance Metrics worksheet…” -ForegroundColor Green
# Adding worksheet
$workbook.Worksheets.Add()
# Creating the “Virtual Machine” worksheet and naming it
$VirtualMachineWorksheet = $workbook.Worksheets.Item(1)
$VirtualMachineWorksheet.Name = ‘Virtual Machine perf metrics’
# Headers for the worksheet
$VirtualMachineWorksheet.Cells.Item(1,1) = ‘Resource Group Name’
$VirtualMachineWorksheet.Cells.Item(1,2) = ‘VM Name’
$VirtualMachineWorksheet.Cells.Item(1,3) = ‘Location’
$VirtualMachineWorksheet.Cells.Item(1,4) = ‘Percentage CPU’
$VirtualMachineWorksheet.Cells.Item(1,5) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,6) = ‘Network IN’
$VirtualMachineWorksheet.Cells.Item(1,7) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,8) = ‘Network Out’
$VirtualMachineWorksheet.Cells.Item(1,9) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,10) = ‘Disk Read Bytes’
$VirtualMachineWorksheet.Cells.Item(1,11) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,12) = ‘Disk Write Bytes’
$VirtualMachineWorksheet.Cells.Item(1,13) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,14) = ‘Disk Read Operations/Sec’
$VirtualMachineWorksheet.Cells.Item(1,15) = ‘Units’
$VirtualMachineWorksheet.Cells.Item(1,16) = ‘Disk Write Operations/Sec’
$VirtualMachineWorksheet.Cells.Item(1,17) = ‘Units’
# Cell Counter
$row_counter = 3
$column_counter = 1
foreach($resourcegroup_list_iterator in $resourcegroup_list){
#write-output “RG: ” $resourcegroup_list_iterator
$vm_list = get-azurermvm -ResourceGroupName $resourcegroup_list_iterator
foreach($vm_list_iterator in $vm_list){
write-host “Fetching performance metrics for the virtual machine: ” $vm_list_iterator.Name -ForegroundColor cyan
$percentage_cpu_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Percentage CPU” # Percentage
$network_in_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Network IN” # Bytes
$network_out_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Network Out” # Bytes
$disk_read_bytes_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Read Bytes” # Bytes Per Second
$disk_write_bytes_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Write Bytes” # Bytes Per Second
$disk_read_operations_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Read Operations/Sec” # Count Per Second
$disk_write_operations_data = get-azurermmetric -ResourceId $vm_list_iterator.id -TimeGrain 00:01:00 -MetricName “Disk Write Operations/Sec” # Count Per Second
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $vm_list_iterator.ResourceGroupName.ToString()
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $vm_list_iterator.Name.ToString()
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $vm_list_iterator.Location.ToString()
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $percentage_cpu_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Percentage”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $network_in_data.Data[-2].Total
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $network_out_data.Data[-2].Total
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_read_bytes_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes Per Second”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_write_bytes_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Bytes Per Second”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_read_operations_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Count Per Second”
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = $disk_write_operations_data.Data[-2].Average
$VirtualMachineWorksheet.Cells.Item($row_counter,$column_counter++) = “Count Per Second”
$row_counter = $row_counter + 1
$column_counter = 1
}
Write-Output ” ”
}
# Checking if the Inventory.xlsx already exists
if(Test-Path C:\AzurePerformanceMetrics\Performance_metrics.xlsx){
Write-Host “C:\AzurePerformanceMetrics\Performance_metrics.xlsx already exitst. Deleting the current file and creating a new one. `n” -ForegroundColor Yellow
Remove-Item C:\AzurePerformanceMetrics\Performance_metrics.xlsx
# Saving the workbook/excel file
$workbook.SaveAs(“C:\AzurePerformanceMetrics\Performance_metrics.xlsx”)
}else {
# Saving the workbook/excel file
$workbook.SaveAs(“C:\AzurePerformanceMetrics\Performance_metrics.xlsx”)
}
$excel.Quit()

Click here to download my PowerShell scripts for Free !!