How to use powershell to add multiple resources to a maintenance configuration in Azure Update Manager?

Somesh K Tiwari 0 Reputation points
2024-12-21T04:00:14.7033333+00:00

Hi Team, I am trying to add resources in Maintenance Configurations in Azure Update Manager using PowerShell using the following Script -

# Install the Az.Maintenance module if you haven't already
Install-Module -Name Az.Maintenance -Force

# Authenticate to Azure
Connect-AzAccount
Set-AzContext -SubscriptionId $subscriptionId

# Variables
$resourceGroupName = "yourResourceGroupName"
$maintenanceConfigName = "yourMaintenanceConfigName"
$location = "yourLocation"  # Azure region for the Maintenance Configuration (e.g., "eastus")
$vmResourceGroupName = "yourVMResourceGroupName" # Resource group containing the VMs
$subscriptionId = "yourSubscriptionId"  # Your Azure subscription ID

# List of VM names to add
$vmList = @("VM1", "VM2", "VM3") 

# Create the maintenance configuration (Guest Scope for patching inside VMs)
New-AzMaintenanceConfiguration `
    -ResourceGroupName $resourceGroupName `
    -Name $maintenanceConfigName `
    -MaintenanceScope "Guest" ` 
    -Location $location `
    -Force 

# Get the created configuration (to get its ID)
$maintenanceConfiguration = Get-AzMaintenanceConfiguration -ResourceGroupName $resourceGroupName -Name $maintenanceConfigName

# Loop through VMs and add them to the configuration
foreach ($vmName in $vmList) {
    New-AzConfigurationAssignment `
        -ResourceGroupName $vmResourceGroupName ` 
        -Location $location `
        -ResourceName $vmName `
        -ResourceType "VirtualMachines" `
        -ProviderName "Microsoft.Compute" `
        -ConfigurationAssignmentName "VMMaintenanceAssignment" ` 
        -MaintenanceConfigurationId $maintenanceConfiguration.Id 
}

Write-Host "Maintenance configuration created and VMs added."

But I am getting the following error -
The Error - "The resource type could not be found in the namespace "Microsoft.HybridCompute" for api version '2023-09-01-preview'."
Or
The Error - "The resource type could not be found in the namespace "Microsoft.Compute" for api version '2023-09-01-preview'."

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
8,172 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,708 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.