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,178 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

1 answer

Sort by: Most helpful
  1. anashetty 1,150 Reputation points Microsoft Vendor
    2024-12-23T02:35:48.21+00:00

    Hi Somesh K Tiwari,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    Once check if your provider is registered or not. Please check this for detailed steps Resolve errors for resource provider registration.
    Please check if there is any case sensitive for Resource type. Try changing the resource type into lowercase and try Updating to the latest version on Az modules.
    Try adding if else statement, where you can get a warning if vm name is not found in resource group.
    You can use Python to manage Azure Maintenance configurations using the azure-mgmt-maintenance library. Install the required libraries and packages then Authenticate with Azure. This might help you for Maintenance configuration MaintenanceConfigurationsOperations Class

    If you have any further queries, please do let us know.

    0 comments No comments

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.