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.
After you login to Azure, run the powershell script saved in previous step.
You will receive a csv file output like below: