domain object

Ankita Rani Patro 181 Reputation points
2024-01-16T20:44:11.29+00:00

Hi ,

Looking for some runbook to remove those record,and stale ad object when VMSS scale in. Thanks In advance

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,158 questions
{count} votes

Accepted answer
  1. Gulnaz Mushtaq 410 Reputation points MVP
    2024-01-17T18:15:58.1366667+00:00

    Here’s a basic example of how you might set up an Azure Automation Runbook to handle this:

    # Import the module
    Import-Module Az.Accounts
    Import-Module Az.Compute
    
    # Connect to Azure with Managed Identity
    $AzureContext = (Connect-AzAccount -Identity).context
    
    # Specify the VMSS and resource group
    $vmssName = "<Your VMSS Name>"
    $resourceGroupName = "<Your Resource Group Name>"
    
    # Get the VMSS
    $vmss = Get-AzVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $vmssName
    
    # Loop through the VMSS instances
    foreach ($instance in $vmss.VirtualMachines) {
        # Check if the instance is in a 'Deleting' state
        if ($instance.ProvisioningState -eq 'Deleting') {
            # Get the instance ID
            $instanceId = $instance.InstanceId
    
            # Remove the domain object associated with the instance
            # Note: You'll need to replace this with the actual command to remove your domain object
            # Remove-DomainObject -InstanceId $instanceId
    
            # Output the instance ID
            Write-Output "Removed domain object for instance: $instanceId"
        }
    }
    

    Please replace <Your VMSS Name> and <Your Resource Group Name> with your actual VMSS name and resource group name. Also, replace Remove-DomainObject with the actual command to remove your domain object. This script connects to Azure using Managed Identity, retrieves the specified VMSS, and loops through its instances. If an instance is in a ‘Deleting’ state (indicating that it’s being scaled in), the script removes the associated domain object. Remember to test this script thoroughly and make any necessary adjustments to fit your specific use case. Always ensure you have appropriate error handling in place when working with automation scripts.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.