Last active
May 21, 2024 13:40
-
-
Save blakedrumm/70abcf47d0e788d91a7f277d4590f122 to your computer and use it in GitHub Desktop.
This PowerShell script retrieves Azure maintenance configurations, counts associated VMs, using Azure Maintenance and Resource Graph modules.
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
# This script retrieves maintenance configurations and the count of VMs associated with each configuration | |
# The script uses the Azure Maintenance and Azure Resource Graph modules | |
# Author: Blake Drumm ([email protected]) | |
# Date created: May 20th, 2024 | |
# Original location: https://gist.github.com/blakedrumm/70abcf47d0e788d91a7f277d4590f122 | |
# Import the required module | |
Import-Module Az.Maintenance | |
Import-Module Az.ResourceGraph | |
# Define the subscription ID and location | |
$subscriptionId = (Get-AzContext).Subscription.Id | |
$location = 'eastus' | |
# Get the maintenance configurations | |
$maintenanceConfigurations = Get-AzResource -ResourceType 'Microsoft.Maintenance/maintenanceConfigurations' | |
# Initialize an array to store the results | |
$results = @() | |
# Loop through each maintenance configuration | |
foreach ($config in $maintenanceConfigurations) { | |
# Extract the configuration name and resource ID | |
$configName = $config.Name | |
$configResourceId = $config.ResourceId | |
# Run the KQL query to get the VM count | |
$query = @" | |
maintenanceresources | |
| where ['type'] =~ "microsoft.maintenance/configurationassignments" | |
| where location in ( '$location' ) | |
| where properties.maintenanceConfigurationId =~ '$configResourceId' | |
| distinct tostring(tolower(properties.resourceId)) | |
"@ | |
$data = Search-AzGraph -Query $query | |
# Create a PSObject for the current configuration and VM count | |
$result = [PSCustomObject]@{ | |
MaintenanceConfigurationName = $configName | |
MaintenanceConfigurationResourceId = $configResourceId | |
vmCount = $data.Count | |
} | |
# Add the result to the results array | |
$results += $result | |
} | |
# Output the results | |
$results | |
<# | |
Copyright (c) Microsoft Corporation. MIT License | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment