Powershell support for azure update manager

Jagadish B 0 Reputation points
2024-08-14T16:29:39.85+00:00

I would like to get the powershell scripts that supports 1. Creating maintenance configuration for azure virtual machines with guest scope 2. Add specific vms of a resource group to it. 3. Remove vms from existing configuration

Azure Update Manager
Azure Update Manager
An Azure service to centrally manages updates and compliance at scale.
376 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 22,031 Reputation points Volunteer Moderator
    2024-08-14T17:24:37.91+00:00

    Hello Jagadish B,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that you would like to get the PowerShell scripts that support and create a maintenance configuration for Azure virtual machines with guest scope.

    You must have enabled PowerShell Modules and ensure they are running with latest module: For example.

    Install-Module -Name PowerShellGet -Repository PSGallery -Force
    Install-Module -Name Az.Maintenance
    # Confirm that you’re running the latest version of Az.Maintenance (version 1.2.0)
    Get-Module -ListAvailable -Name Az.Maintenance
    

    To answer the specific questions:

    1. Creating maintenance configuration for azure virtual machines with guest scope
    #trigger an update assessment on your Azure VM
    az vm assess-patches -g MyResourceGroup -n MyVm
    
    # Create the maintenance configuration
    New-AzUpdateManagementMaintenanceConfiguration -ResourceGroupName $resourceGroupName `
        -Name $maintenanceConfigName `
        -Location $location `
        -Schedule $schedule `
        -MaintenanceWindow $maintenanceWindow `
        -Scope "Guest"
    
    

    Change MyResourceGroup and MyVm with your actual resource group and VM names

    1. Add specific VMs of a resource group to it.
    # Combine the following commands into a script to create an update schedule
    New-AzAutomationSchedule
    New-AzAutomationUpdateManagementAzureQuery
    New-AzAutomationSoftwareUpdateConfiguration
    
    # Create a new automation schedule
    $schedule = New-AzAutomationSchedule -Name "MyUpdateSchedule" -ResourceGroupName "MyResourceGroup" -AutomationAccountName "MyAutomationAccount" -StartTime (Get-Date).AddDays(1) -DayInterval 7
    
    # Define an Azure query to select the VMs
    $query = New-AzAutomationUpdateManagementAzureQuery -ResourceGroupName "MyResourceGroup" -Name "MyQuery" -Schedule $schedule
    
    # Create a new software update configuration
    New-AzAutomationSoftwareUpdateConfiguration -ResourceGroupName "MyResourceGroup" -AutomationAccountName "MyAutomationAccount" -Name "MyUpdateConfiguration" -UpdateConfiguration $query
    
    
    # Add VMs to the maintenance configuration
    foreach ($vmName in $vmNames) {
        Add-AzUpdateManagementMaintenanceConfigurationVM `
            -ResourceGroupName $resourceGroupName `
            -MaintenanceConfiguration $maintenanceConfig `
            -VirtualMachineName $vmName
    }
    
    
    1. Remove vms from existing configuration

    No specific command, but you can create a new configuration without the VMs you want to exclude and apply it to the remaining VMs. In some cases, you will find the below snippet to work.

    # Create a new configuration excluding specific VMs
    $newConfig = New-AzAutomationSoftwareUpdateConfiguration -ResourceGroupName "MyResourceGroup" -AutomationAccountName "MyAutomationAccount" -Name "NewUpdateConfiguration" -UpdateConfiguration $query
    # Apply the new configuration to the remaining VMs
    # (You will need to adjust the scope or parameters to exclude certain VMs.)
    
    #Alternatively: 
    # Define variables
    $resourceGroupName = "YourResourceGroupName"
    $maintenanceConfigName = "YourMaintenanceConfigName"
    $vmNames = @("VM1", "VM2") # Names of the VMs you want to remove
    # Get the maintenance configuration
    $maintenanceConfig = Get-AzUpdateManagementMaintenanceConfiguration `
        -ResourceGroupName $resourceGroupName `
        -Name $maintenanceConfigName
    # Remove VMs from the maintenance configuration
    foreach ($vmName in $vmNames) {
        Remove-AzUpdateManagementMaintenanceConfigurationVM `
            -ResourceGroupName $resourceGroupName `
            -MaintenanceConfiguration $maintenanceConfig `
            -VirtualMachineName $vmName
    }
    

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam

    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.