Last active
February 28, 2019 20:18
-
-
Save f-bader/3fffb91c97ab22f610f905baf42f761b to your computer and use it in GitHub Desktop.
This script creates a list of all Azure VMs including their Log Analytics Workspace Name and ID and the current status of the VM (running/deallocated/stopped)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment