Inhalt

Report des Log Analytics Workspace für alle Azure VMs

Inhalt

Manchmal möchte man nur wissen, an welchen Log Analytics Workspace (OMS für die älteren Leute da draußen) eine VM ihre Log Daten sendet. Oder sogar alle von euren Azure VMs auf einmal?

Mit dem folgenden Skript ist diese Aufgabe kinderleicht. Und dank RamblingCookieMonster und seinem PSExcel Modul kannst du das Ergebnis direkt an alle Excelliebhaber versenden.

ReportLogAnalyticsWorkspacePerVM.ps1

#region Report: All VMs including the status, and OMS workplace
$Subscriptions = Get-AzureRmSubscription | Where-Object { $_.Name -in ("SUB01","SUB02") }
$OmsWorkspaces = @()
ForEach ($Subscription in $Subscriptions) {
    $SubscriptionName = $Subscription.Name
    Set-AzureRmContext -SubscriptionName "$SubscriptionName" | Out-Null
    $OmsWorkspaces += Get-AzureRmOperationalInsightsWorkspace
}
$Report = ForEach ($Subscription in $Subscriptions) {
    $SubscriptionName = $Subscription.Name
    Set-AzureRmContext -SubscriptionName "$SubscriptionName" | Out-Null
    $RGs = Get-AzureRMResourceGroup
    foreach ($RG in $RGs) {
        $VMs = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName
        foreach ($VM in $VMs) {
            # Query OMS Workspace
            $VM = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name
            $ExtensionName = $VM.Extensions | Where-Object { $_.VirtualMachineExtensionType -in ("MicrosoftMonitoringAgent", "OmsAgentForLinux") } | Select-Object -ExpandProperty Name -First 1
            if ( $ExtensionName ) {
                $ExtensionInformation = Get-AzureRmVMExtension -ResourceGroupName $VM.ResourceGroupName -VMName $VM.Name -Name $ExtensionName
                $OmsWorkspaceId = ($ExtensionInformation.PublicSettings | ConvertFrom-Json).workspaceId
                if ( $OmsWorkspaceId ) {
                    $OmsWorkspace = $OmsWorkspaces | Where-Object { $_.CustomerId -eq $OmsWorkspaceId }
                    $OmsWorkspaceName = "$($OmsWorkspace.Name)"
                } else {
                    $OmsWorkspaceName = "n/a"
                    $OmsWorkspaceId = "n/a"
                }
            } else {
                $OmsWorkspaceName = "n/a"
                $OmsWorkspaceId = "n/a"
            }
            # VM Status (running/deallocated/stopped)
            $VMDetail = Get-AzureRmVM -ResourceGroupName $RG.ResourceGroupName -Name $VM.Name -Status
            $VMStatusDetail = $VMDetail.Statuses.DisplayStatus -match "^VM .*$"
            New-Object psobject -Property @{
                "VMName"           = $VM.Name
                "OSType"           = $VM.StorageProfile.OSDisk.OSType
                "ResourceGroup"    = $RG.ResourceGroupName
                "VMStatus"         = "$VMStatusDetail"
                "SubscriptionName" = $SubscriptionName
                "OmsWorkspaceName" = $OmsWorkspaceName
                "OmsWorkspaceId"   = $OmsWorkspaceId
            }
            Remove-Variable -Name OmsWorkspaceId -ErrorAction SilentlyContinue
            Remove-Variable -Name ExtensionName -ErrorAction SilentlyContinue
        }
    }
}
# Export as XLSX file
Import-Module PSExcel
$Filename = "LogAnalytics_Report_" + (Get-Date).ToString("yyyy-MM-dd_HHmmss")
$Report | Export-XLSX -Path "~/$Filename.xlsx" -Table -Autofit