Azure Connected Machine Agent uninstallation

Steps to uninstall

  1. Remove VM extensions
  2. Disconnect the server from Azure Arc and Uninstall the agent

Pre-requisites

NOTE Set a short expiry date for the Service Principal, and make sure to delete it after completing the agent uninstallation on all servers.

Set-AzContext -Subscription "xxxx-xxxx-xxxx-xxxx"

1. Remove VM extensions

NOTE This script should run on a management machine or in Azure Cloud Shell

Resource group scope

$resourceGroupName= "<Name of the Resource Group where the Arc-enabled Servers are located>"
Get-AzConnectedMachine -ResourceGroupName $ResourceGroupName | ForEach-Object{
    $serverName = $_.Name
    Get-AzConnectedMachineExtension -ResourceGroupName $resourceGroupName -MachineName $serverName | foreach-object{
        $extensionName = $_.Name
        Remove-AzConnectedMachineExtension -ResourceGroupName $resourceGroupName -MachineName $serverName -Name $extensionName -NoWait -Confirm:$false
    }
}

Subscription scope

$subscriptionId= "<Id of the Subscription where the Arc-enabled Servers are located>"
Get-AzConnectedMachine -SubscriptionId $subscriptionId | ForEach-Object{
    $serverName = $_.Name
    Get-AzConnectedMachineExtension -ResourceGroupName $resourceGroupName -MachineName $serverName | foreach-object{
        $extensionName = $_.Name
        Remove-AzConnectedMachineExtension -ResourceGroupName $resourceGroupName -MachineName $serverName -Name $extensionName -NoWait -Confirm:$false
    }
}

2. Disconnect the server from Azure Arc and uninstall agent

NOTE This script should run on the Arc-enabled Servers to be disconnected

Windows

# Set the Service Principal Id and Secret variables
$ServicePrincipalId = <The Servic Principal Id>
$ServicePrincipalClientSecret = <The Servic Principal secret>

# Set location to the Azure Connected Machine Agent folder
Set-Location "C:\Program Files\AzureConnectedMachineAgent"

# Disconnecting the Azure Connected Machine Agent
$agentStatus = .\azcmagent.exe show --json | ConvertFrom-Json
if($agentStatus.status -ne "Connected"){
    .\azcmagent.exe disconnect --force-local-only
}
else{
    .\azcmagent disconnect --service-principal-id $ServicePrincipalId --service-principal-secret $ServicePrincipalClientSecret
}

# Uninstalling the Azure Connected Machine Agent msi installer
Get-ChildItem -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall | `
Get-ItemProperty |Where-Object {$_.DisplayName -eq "Azure Connected Machine Agent"} | ForEach-Object {MsiExec.exe /x "$($_.PsChildName)" /qn}

Linux

Ubuntu

#!/bin/bash

# Set the Service Principal Id and Secret variables
ServicePrincipalId=<The Servic Principal Id>
ServicePrincipalClientSecret=<The Servic Principal secret>

# Elevate to sudo privileges
sudo -s

# Disconnecting the Azure Connected Machine Agent
agentStatus=$(azcmagent show | grep "Agent Status" | cut -d ":" -f2 | awk '{$1=$1;print}')
if [ "$agentStatus" != "Connected" ]; then
    azcmagent disconnect --force-local-only
else
    azcmagent disconnect --service-principal-id $ServicePrincipalId --service-principal-secret $ServicePrincipalClientSecret
fi

# Uninstalling the Azure Connected Machine Agent
sudo apt purge azcmagent

RHEL, CentOS, Oracle Linux, and Amazon Linux

#!/bin/bash

# Set the Service Principal Id and Secret variables
ServicePrincipalId=<The Servic Principal Id>
ServicePrincipalClientSecret=<The Servic Principal secret>

# Elevate to sudo privileges
sudo -s

# Disconnecting the Azure Connected Machine Agent
agentStatus=$(azcmagent show | grep "Agent Status" | cut -d ":" -f2 | awk '{$1=$1;print}')
if [ "$agentStatus" != "Connected" ]; then
    azcmagent disconnect --force-local-only
else
    azcmagent disconnect --service-principal-id $ServicePrincipalId --service-principal-secret $ServicePrincipalClientSecret
fi

# Uninstalling the Azure Connected Machine Agent
sudo yum remove azcmagent

SLES

#!/bin/bash

# Set the Service Principal Id and Secret variables
ServicePrincipalId=<The Servic Principal Id>
ServicePrincipalClientSecret=<The Servic Principal secret>

# Elevate to sudo privileges
sudo -s

# Disconnecting the Azure Connected Machine Agent
agentStatus=$(azcmagent show | grep "Agent Status" | cut -d ":" -f2 | awk '{$1=$1;print}')
if [ "$agentStatus" != "Connected" ]; then
    azcmagent disconnect --force-local-only
else
    azcmagent disconnect --service-principal-id $ServicePrincipalId --service-principal-secret $ServicePrincipalClientSecret
fi

# Uninstalling the Azure Connected Machine Agent
sudo zypper remove azcmagent