Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

Azure VM Monitoring agent status using powershell

Posted on August 30, 2019February 27, 2022

Update : updated script to support Azure Az powershell module.

Hey friends! Today I am going to share a new Powershell script I created to generate a report of Azure VM Monitoring Agent extension configuration. I usually run this script to find out how our Azure VMs have been setup with Azure Log Analytics Workspace. This report includes VM power status along with Workspace details. We also run this script to see if we have deployed any VM with a wrong Workspace and need to move to the correct one. Kind of housekeeping you can say!

I hope you are already familiar with Azure Log Analytics Workspace. If not, refer Microsoft Documentation to learn more about Azure monitoring.

Before start using this script, make sure you have Azure Az Module installed and imported on the system. For more information on how to install and configure Az module refer following article : https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az?view=azps-5.5.0

Share your comment if it helps or if you face any issue.

$date = Get-Date -UFormat "%m-%d-%Y"
$currentDir = $(Get-Location).Path
$oFile = "$($currentDir)\AzureMMAAgentStatus_$($date).csv"

if(Test-Path $oFile){
	Remove-Item $oFile -Force
}

"SubscriptionName,VMName,ResourceGroupName,OSType,VMPowerStatus,WorkspaceId,WorkspaceName" | Out-File $oFile -Append -Encoding ASCII
Get-AzSubscription | ForEach-Object{
	$subscriptionId = $subscriptionName = ""
	$subscriptionId = $_.SubscriptionId
	$subscriptionName = $_.Name
	Set-AzContext -SubscriptionId $subscriptionId
    Get-AzResourceGroup | ForEach-Object{
        $rgName = $_.ResourceGroupName
        Get-AzVM -ResourceGroupName $rgName | ForEach-Object{
            $mmaExtensionDetails = $vmName = $osType = $vmStatusDetails = $linuxMMAExtensionDetails = $workspaceName = $workspaceId = $windowsMMAExtensionDetails = $linuxOMSExtensionDetails = $OMSExtensionDetails = ''
            $VMStatusDetail = "Powered On"
            $vmName = $_.Name
            $osType = $_.StorageProfile.OsDisk.OsType
            $vmStatusDetails = Get-AzVM -ResourceGroupName $rgName -Name $vmName -Status
            foreach ($VMStatus in $vmStatusDetails.Statuses)
            { 
                if($VMStatus.Code.CompareTo("PowerState/deallocated") -eq 0)
                {
                    $VMStatusDetail = $VMStatus.DisplayStatus
                }
            }
            $mmaExtensionDetails = Get-AzVMExtension -ResourceGroupName $rgName -Name MicrosoftMonitoringAgent -VMName $vmName -ErrorAction "SilentlyContinue"
            $windowsMMAExtensionDetails = Get-AzVMExtension -ResourceGroupName $rgName -Name windowsMMAAgent -VMName $vmName -ErrorAction "SilentlyContinue"
            $linuxMMAExtensionDetails = Get-AzVMExtension -ResourceGroupName $rgName -Name linuxMMAAgent -VMName $vmName -ErrorAction "SilentlyContinue"
            $linuxOMSExtensionDetails = Get-AzVMExtension -ResourceGroupName $rgName -Name OmsAgentForLinux -VMName $vmName -ErrorAction "SilentlyContinue"
            $OMSExtensionDetails = Get-AzVMExtension -ResourceGroupName $rgName -Name 'OMS.Monitoring' -VMName $vmName -ErrorAction "SilentlyContinue"
            #MicrosoftMonitoringAgent OMS.Monitoring
            if(!([string]::IsNullOrEmpty($mmaExtensionDetails))){
                $workspaceId = $mmaExtensionDetails.PublicSettings | ConvertFrom-Json | Select-Object workspaceId -ExpandProperty workspaceId
                $workspaceName = Get-AzOperationalInsightsWorkspace | Where-Object{$_.CustomerId -eq $workspaceId} | Select-Object Name -ExpandProperty Name
            }
            #windowsMMAAgent
            if(!([string]::IsNullOrEmpty($windowsMMAExtensionDetails))){
                $workspaceId = $windowsMMAExtensionDetails.PublicSettings | ConvertFrom-Json | Select-Object workspaceId -ExpandProperty workspaceId
                $workspaceName = Get-AzOperationalInsightsWorkspace | Where-Object{$_.CustomerId -eq $workspaceId} | Select-Object Name -ExpandProperty Name
            }
            #linuxMMAAgent OmsAgentForLinux
            if(!([string]::IsNullOrEmpty($linuxMMAExtensionDetails))){
                $workspaceId = $linuxMMAExtensionDetails.PublicSettings | ConvertFrom-Json | Select-Object workspaceId -ExpandProperty workspaceId
                $workspaceName = Get-AzOperationalInsightsWorkspace | Where-Object{$_.CustomerId -eq $workspaceId} | Select-Object Name -ExpandProperty Name
            }
            #OmsAgentForLinux
            if(!([string]::IsNullOrEmpty($linuxOMSExtensionDetails))){
                $workspaceId = $linuxOMSExtensionDetails.PublicSettings | ConvertFrom-Json | Select-Object workspaceId -ExpandProperty workspaceId
                $workspaceName = Get-AzOperationalInsightsWorkspace | Where-Object{$_.CustomerId -eq $workspaceId} | Select-Object Name -ExpandProperty Name
            }
            #OmsAgentForLinux
            if(!([string]::IsNullOrEmpty($OMSExtensionDetails))){
                $workspaceId = $OMSExtensionDetails.PublicSettings | ConvertFrom-Json | Select-Object workspaceId -ExpandProperty workspaceId
                $workspaceName = Get-AzOperationalInsightsWorkspace | Where-Object{$_.CustomerId -eq $workspaceId} | Select-Object Name -ExpandProperty Name
            }
            "$subscriptionName,$vmName,$rgName,$osType,$VMStatusDetail,$workspaceId,$workspaceName"  | Out-File $oFile -Append -Encoding ASCII
        }
    }
}

Download above script and save it with a .ps1 file extension. Open powershell console. After that, login to your Azure account using – Login-AzAccount. It will prompt you to enter your Azure credentials.

This image has an empty alt attribute; its file name is image-3.png
Login to Azure Account

After you login to Azure, run the powershell script saved in previous step.

Run Powershell script

You will receive a csv file output like below:

CSV file output

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes