azure virtual machines

Azure – Install software on Azure Virtual Machine using Azure Custom Script Extension (CSE)

You may need to install a software (exe, MSI etc..) on your Azure virtual machines as per the on-boarding process. The traditional way of doing this is to RDP to your virtual machine and then install the software.

Azure and PowerShell make this task simple by introducing “Custom Script Extension (CSE)” for Azure Virtual machines. Using CSE you can install the executables without login into the servers. The process also reduces human effort by a lot, hence increasing the ROI for your team.

As an example, let us see how to deploy a BigFix client into an Azure Windows Virtual Machine:

The process requires two scripts:

Script 1: installAgent.ps1

This script does the silent installation of the agents. This script must be uploaded into Azure Storage Account along with the exe/MSI.

Script 2: triggerCSE.ps1

This script installs the CSE on the Windows Azure virtual machine. Checks if the Virtual Machine is STOPPED. If it is stopped, it will start the virtual machine, install the CSE, and then it will stop the virtual machine.

Steps to be followed

  1. Upload all the necessary files (BigFix installation files) into Azure Storage account and provide Anonymous access to the container.
  2. Upload the installAgent.ps1 PowerShell script into Azure Storage account and provide Anonymous access to the container.
  3. Execute the triggerCSE.ps1 from your laptop or you can completely automate the solution using Azure Automation Account.

installAgent.ps1

# Script to install Big Fix agents in Singapore region

# Create a directory to hold BigFix files

new-item 'c:\bigfix' -ItemType directory -force


# Copy BigFix files from Azure storage to local directory

Invoke-WebRequest -Uri https://manjutool.blob.core.windows.net/wpbigfixupdatedsingapore/clientsettings.cfg -outfile 'c:\bigfix\clientsettings.cfg'

Invoke-WebRequest -Uri https://manjutool.blob.core.windows.net/wpbigfixupdatedsingapore/masthead.afxm -outfile 'c:\bigfix\masthead.afxm'

Invoke-WebRequest -Uri https://manjutool.blob.core.windows.net/wpbigfixupdatedsingapore/BigFix-BES-Client-9.5.7.94.exe -outfile 'c:\bigfix\setup.exe'


# Execute the setup file

$arguments = "/S /v/qn"

$filepath = "c:\bigfix\setup.exe"

Start-Process $filepath $arguments -wait


triggerCSE.ps1

##### Installing BigFix client on virtual machine #####




        # Declaring variables




        # storage account name where the custom script is stored

        $storage_account_name = "<INSTERT_STORAGE_ACCOUNT_NAME>"

        # storage account key of where the custom script is stored

        $storage_account_key = "<INSERT_STORAGE_ACCOUNT_KEY>"

        # custom script file name

        $bigfix_file_name = "installAgent.ps1"

        # container name where the custom script is stored

        $bigfix_container_name_singapore = "<INSERT_AZURE_STORAGE_CONTAINER_NAME>"

        # Assuming the state of the virtual machine is not de-allocated

        $is_dellocated = $false


        $resource_group = "<INSERT_AZURE_VIRTUAL_MACHINE_RESOURCE_GROUP_NAME>"

        $vm_name = "<INSERT_AZURE_VIRTUAL_MACHINE_NAME>"




        # Checking if the Webhook data has the Resource Group and Virtual Machine.

        if($resource_group -eq $null -or $vm_name -eq $null){

            "Either Resource Group or Virtual Machine name, not present. This could be because the input variables could be misspelled. Make sure the input names are - 'ResourceGroup' and 'VirtualMachine'. " | write-output

            exit

        }

      
        #### Checking if the Virtual Machine is a Windows machine ########

        # Obtaining the Virtual Machine object

        $vm = get-azurermvm -ResourceGroupName $resource_group -Name $vm_name




        # Obtaining the Virtual Machine status object

        $vm_status = get-azurermvm -ResourceGroupName $resource_group -Name $vm_name -Status




        "Displaying the status of Virtual machine...." | write-output

        $vm_status.Statuses[1].DisplayStatus | write-output

        "" | write-output

        "" | write-output

        "Checking if the VM is Windows or not. Expect some output below if the Virtual machine is Windows... If you DONOT GET ANY OUTPUT, STOP EXECUTING..." | write-output

        $vm.OSProfile.WindowsConfiguration | write-output

       

        if($vm.OSProfile.WindowsConfiguration -eq $null){

            "The Virtual machine is either a custom image or is not Windows Virtual Machine. Cannot proceed with installing Custom Script Extenstion.. " | write-output

            exit

        }


<#




NOTE: IF THE VIRTUAL MACHINE IS STOPPED-DEALLOCATED, THIS SCRIPT WILL START THE VIRTUAL MACHINE, INSTALL AGENTS AND WILL DE-ALLOCATE IT




#>

        ######## Checking the status of the Virtual Machine ########

        <#

            VM Generalized --> Do not take any action. Exit Execution

            VM Deallocated --> Start the Virtual Machine

            VM Running --> Do not take any action, Proceed with Execution

        #>


        if($vm_status.Statuses[1].DisplayStatus -eq "VM Generalized"){

            "Virtual Machine is in the GENERALIZED state. Do not proceed further... " | write-output

            "" | write-output

            "" | write-output

exit

        }


        if($vm_status.Statuses[1].DisplayStatus -eq "VM deallocated"){

            "Virtual Machine is STOPPED. Starting the virtual machine... " | write-output

            $is_dellocated = $true

            $vm | Start-AzureRmVM

            "Successfully started Virtual Machine.." | write-output

            ""| write-output

            "" | write-output

        }


        if($vm_status.Statuses[1].DisplayStatus -eq "VM running"){

            "Virtual Machine is already RUNNING. Proceeding with agents installation" | write-output

            "" | write-output

            "" | write-output

        }


      

        # Checking if the virtual machine already has a Custom Script Extension




        $vm = get-azurermvm -ResourceGroupName $resource_group -Name $vm_name

        $vm_status = get-azurermvm -ResourceGroupName $resource_group -Name $vm_name -Status

        $vm_extensions = $vm.Extensions


        foreach($vm_extensions_iterator in $vm_extensions){

            if($vm_extensions_iterator.VirtualMachineExtensionType -eq "CustomScriptExtension"){

                "Removing the CSE..." | write-output

                Remove-AzureRmVMCustomScriptExtension -Name $vm_extensions_iterator.Name -ResourceGroupName $resource_group -VMName $vm_name -force

                "Removed  the CSE " | write-output

                "" | write-output

                "" | write-output

            }




        }


        # Re-creating the Virtual Machine object, since one of the above condition - starts the virtual machine

        $vm = get-azurermvm -ResourceGroupName $resource_group -Name $vm_name

        $vm_status = get-azurermvm -ResourceGroupName $resource_group -Name $vm_name -Status


        ########### Installing BIGFIX client via Azure Custom Script Extension ###########

        if($vm_status.Statuses[1].DisplayStatus -eq "VM running" -and $vm.OSProfile.WindowsConfiguration -ne $null){

            "Installing BigFix extension..." | write-output

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

            Set-AzureRmVMCustomScriptExtension -ResourceGroupName $resource_group -Location $vm.Location -VMName $vm_name -Name "ibm_bigfix_agent_install_extension" -TypeHandlerVersion "1.1" -StorageAccountName $storage_account_name -StorageAccountKey $storage_account_key -FileName $bigfix_file_name -ContainerName $bigfix_container_name_singapore

        }


        "waiting for 10 seconds..." | write-output

        "" | write-output

        "" | write-output


        Start-Sleep -s 10


        ######## Stopping the Virtual machine that we had started ########



        if($is_dellocated -eq $true){

            "We had started the virtual machine before installing the BigFix agent. STOPPING the virtual machine to preserve the initial state..." | write-output


            $vm | Stop-AzureRmVM -force

            "Successfully stopped the virtual machine" | write-output

            "" | write-output

            "" | write-output

        }

As an enhancement, you can add additional checks, create a log file and have it uploaded to another Storage Account. Or, create an Azure Storage Table, and write the updates to it tracking how many virtual machines the CSE is installed.

Click here to download my PowerShell scripts for Free !!

Click here for Azure tutorial videos !!

Advertisement

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 – 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 !!