MMA/OMS 探索和移除公用程式
將機器移轉至 Azure 監視器代理程式 (AMA) 之後,請根據作業系統移除舊版 Log Analytics 代理程式,以避免記錄重複。 舊版探索和移除公用程式可以從單一訂用帳戶中移除 Azure 虛擬機器 (VM)、Azure 虛擬機器擴展集 (VMSS) 和 Azure Arc 伺服器的延伸模組。
公用程式可在兩個步驟中運作:
探索:公用程式會建立所有已安裝簡單 CSV 檔案中舊版代理程式的機器清查。 建議您在執行公用程式時,不要建立任何新的 VM。
移除:公用程式會從 CSV 檔案中列出的機器中移除舊版代理程式。 您應該編輯 CSV 檔案中的機器清單,以確保只有您想要從中移除代理程式的機器存在。
注意
拿掉不適用於使用 MSI 安裝程式安裝的 MMA 代理程式。 它只適用 VM 延伸模組。
必要條件
在已連線網際網路的機器上執行所有設定步驟。 您需要:
- Windows 10 或更新版本,或 Windows Server 2019 或更新版本。
- PowerShell 7.0 或更新版本,可啟用平行執行以加速處理。
- 必須安裝 Azcli,才能與 Azure Graph API 通訊。
- 以系統管理員身分開啟 PowerShell:
- 執行命令:
Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile AzureCLI.msi
。 - 執行命令:
Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'
。 - 最新版的 Azure CLI 將會下載並安裝。
步驟 1 登入和設定訂用帳戶
此工具一次可使用一個訂用帳戶。 您必須登入並設定訂用帳戶才能進行移除作業。 以系統管理員身分開啟 PowerShell 命令提示字元並登入。
az login
接下來,您必須設定訂用帳戶。
Az account set --subscription {subscription_id or “subscription_name”}
步驟 2 複製指令碼
您會使用下列指令碼來移除代理程式。 在您的本機目錄中開啟名為 LogAnalyticsAgentUninstallUtilityScript.ps1 的檔案,並將指令碼複製到檔案中。
# This is per subscription, the customer has to set the az subscription before running this.
# az login
# az account set --subscription <subscription_id/subscription_name>
# This script uses parallel processing, modify the $parallelThrottleLimit parameter to either increase or decrease the number of parallel processes
# PS> .\LogAnalyticsAgentUninstallUtilityScript.ps1 GetInventory
# The above command will generate a csv file with the details of Vm's and Vmss and Arc servers that has log analyice Agent extension installed.
# The customer can modify the the csv by adding/removing rows if needed
# Remove the log analytics agent by running the script again as shown below:
# PS> .\LogAnalyticsAgentUninstallUtilityScript.ps1 UninstallExtension
# This version of the script requires Powershell version >= 7 in order to improve performance via ForEach-Object -Parallel
# https://docs.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.1
if ($PSVersionTable.PSVersion.Major -lt 7)
{
Write-Host "This script requires Powershell version 7 or newer to run. Please see https://docs.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.1."
exit 1
}
$parallelThrottleLimit = 16
function GetArcServersWithLogAnalyticsAgentExtensionInstalled {
param (
$fileName
)
$serverList = az connectedmachine list --query "[].{ResourceId:id, ResourceGroup:resourceGroup, ServerName:name}" | ConvertFrom-Json
if(!$serverList)
{
Write-Host "Cannot get the Arc server list"
return
}
$serversCount = $serverList.Length
$vmParallelThrottleLimit = $parallelThrottleLimit
if ($serversCount -lt $vmParallelThrottleLimit)
{
$serverParallelThrottleLimit = $serversCount
}
$serverGroups = @()
if($serversCount -eq 1)
{
$serverGroups += ,($serverList[0])
}
else
{
# split the list into batches to do parallel processing
for ($i = 0; $i -lt $serversCount; $i += $vmParallelThrottleLimit)
{
$serverGroups += , ($serverList[$i..($i + $serverParallelThrottleLimit - 1)])
}
}
Write-Host "Detected $serversCount Arc servers in this subscription."
$hash = [hashtable]::Synchronized(@{})
$hash.One = 1
$serverGroups | Foreach-Object -ThrottleLimit $parallelThrottleLimit -Parallel {
$len = $using:serversCount
$hash = $using:hash
$_ | ForEach-Object {
$percent = 100 * $hash.One++ / $len
Write-Progress -Activity "Getting Arc server extensions Inventory" -PercentComplete $percent
$serverName = $_.ServerName
$resourceGroup = $_.ResourceGroup
$resourceId = $_.ResourceId
Write-Debug "Getting extensions for Arc server: $serverName"
$extensions = az connectedmachine extension list -g $resourceGroup --machine-name $serverName --query "[?contains(['MicrosoftMonitoringAgent', 'OmsAgentForLinux', 'AzureMonitorLinuxAgent', 'AzureMonitorWindowsAgent'], properties.type)].{type: properties.type, name: name}" | ConvertFrom-Json
if (!$extensions) {
return
}
$extensionMap = @{}
foreach ($ext in $extensions) {
$extensionMap[$ext.type] = $ext.name
}
$extensionName = ""
if ($extensionMap.ContainsKey("MicrosoftMonitoringAgent")) {
$extensionName = $extensionMap["MicrosoftMonitoringAgent"]
}
elseif ($extensionMap.ContainsKey("OmsAgentForLinux")) {
$extensionName = $extensionMap["OmsAgentForLinux"]
}
if ($extensionName) {
$amaExtensionInstalled = "False"
if ($extensionMap.ContainsKey("AzureMonitorWindowsAgent") -or $extensionMap.ContainsKey("AzureMonitorLinuxAgent")) {
$amaExtensionInstalled = "True"
}
$csvObj = New-Object -TypeName PSObject -Property @{
'ResourceId' = $resourceId
'Name' = $serverName
'Resource_Group' = $resourceGroup
'Resource_Type' = "ArcServer"
'Install_Type' = "Extension"
'Extension_Name' = $extensionName
'AMA_Extension_Installed' = $amaExtensionInstalled
}
$csvObj | Export-Csv $using:fileName -Append -Force | Out-Null
}
# az cli sometime cannot handle many requests at same time, so delaying next request by 2 milliseconds
Start-Sleep -Milliseconds 2
}
}
}
function GetVmsWithLogAnalyticsAgentExtensionInstalled
{
param(
$fileName
)
$vmList = az vm list --query "[].{ResourceId:id, ResourceGroup:resourceGroup, VmName:name}" | ConvertFrom-Json
if(!$vmList)
{
Write-Host "Cannot get the VM list"
return
}
$vmsCount = $vmList.Length
$vmParallelThrottleLimit = $parallelThrottleLimit
if ($vmsCount -lt $vmParallelThrottleLimit)
{
$vmParallelThrottleLimit = $vmsCount
}
if($vmsCount -eq 1)
{
$vmGroups += ,($vmList[0])
}
else
{
# split the vm's into batches to do parallel processing
for ($i = 0; $i -lt $vmsCount; $i += $vmParallelThrottleLimit)
{
$vmGroups += , ($vmList[$i..($i + $vmParallelThrottleLimit - 1)])
}
}
Write-Host "Detected $vmsCount Vm's in this subscription."
$hash = [hashtable]::Synchronized(@{})
$hash.One = 1
$vmGroups | Foreach-Object -ThrottleLimit $parallelThrottleLimit -Parallel {
$len = $using:vmsCount
$hash = $using:hash
$_ | ForEach-Object {
$percent = 100 * $hash.One++ / $len
Write-Progress -Activity "Getting VM extensions Inventory" -PercentComplete $percent
$resourceId = $_.ResourceId
$vmName = $_.VmName
$resourceGroup = $_.ResourceGroup
Write-Debug "Getting extensions for VM: $vmName"
$extensions = az vm extension list -g $resourceGroup --vm-name $vmName --query "[?contains(['MicrosoftMonitoringAgent', 'OmsAgentForLinux', 'AzureMonitorLinuxAgent', 'AzureMonitorWindowsAgent'], typePropertiesType)].{type: typePropertiesType, name: name}" | ConvertFrom-Json
if (!$extensions) {
return
}
$extensionMap = @{}
foreach ($ext in $extensions) {
$extensionMap[$ext.type] = $ext.name
}
$extensionName = ""
if ($extensionMap.ContainsKey("MicrosoftMonitoringAgent")) {
$extensionName = $extensionMap["MicrosoftMonitoringAgent"]
}
elseif ($extensionMap.ContainsKey("OmsAgentForLinux")) {
$extensionName = $extensionMap["OmsAgentForLinux"]
}
if ($extensionName) {
$amaExtensionInstalled = "False"
if ($extensionMap.ContainsKey("AzureMonitorWindowsAgent") -or $extensionMap.ContainsKey("AzureMonitorLinuxAgent")) {
$amaExtensionInstalled = "True"
}
$csvObj = New-Object -TypeName PSObject -Property @{
'ResourceId' = $resourceId
'Name' = $vmName
'Resource_Group' = $resourceGroup
'Resource_Type' = "VM"
'Install_Type' = "Extension"
'Extension_Name' = $extensionName
'AMA_Extension_Installed' = $amaExtensionInstalled
}
$csvObj | Export-Csv $using:fileName -Append -Force | Out-Null
}
# az cli sometime cannot handle many requests at same time, so delaying next request by 2 milliseconds
Start-Sleep -Milliseconds 2
}
}
}
function GetVmssWithLogAnalyticsAgentExtensionInstalled
{
param(
$fileName
)
# get the vmss list which are successfully provisioned
$vmssList = az vmss list --query "[?provisioningState=='Succeeded'].{ResourceId:id, ResourceGroup:resourceGroup, VmssName:name}" | ConvertFrom-Json
$vmssCount = $vmssList.Length
Write-Host "Detected $vmssCount Vmss in this subscription."
$hash = [hashtable]::Synchronized(@{})
$hash.One = 1
$vmssList | Foreach-Object -ThrottleLimit $parallelThrottleLimit -Parallel {
$len = $using:vmssCount
$hash = $using:hash
$percent = 100 * $hash.One++ / $len
Write-Progress -Activity "Getting VMSS extensions Inventory" -PercentComplete $percent
$resourceId = $_.ResourceId
$vmssName = $_.VmssName
$resourceGroup = $_.ResourceGroup
Write-Debug "Getting extensions for VMSS: $vmssName"
$extensions = az vmss extension list -g $resourceGroup --vmss-name $vmssName --query "[?contains(['MicrosoftMonitoringAgent', 'OmsAgentForLinux', 'AzureMonitorLinuxAgent', 'AzureMonitorWindowsAgent'], typePropertiesType)].{type: typePropertiesType, name: name}" | ConvertFrom-Json
if (!$extensions) {
return
}
$extensionMap = @{}
foreach ($ext in $extensions) {
$extensionMap[$ext.type] = $ext.name
}
$extensionName = ""
if ($extensionMap.ContainsKey("MicrosoftMonitoringAgent")) {
$extensionName = $extensionMap["MicrosoftMonitoringAgent"]
}
elseif ($extensionMap.ContainsKey("OmsAgentForLinux")) {
$extensionName = $extensionMap["OmsAgentForLinux"]
}
if ($extensionName) {
$amaExtensionInstalled = "False"
if ($extensionMap.ContainsKey("AzureMonitorWindowsAgent") -or $extensionMap.ContainsKey("AzureMonitorLinuxAgent")) {
$amaExtensionInstalled = "True"
}
$csvObj = New-Object -TypeName PSObject -Property @{
'ResourceId' = $resourceId
'Name' = $vmssName
'Resource_Group' = $resourceGroup
'Resource_Type' = "VMSS"
'Install_Type' = "Extension"
'Extension_Name' = $extensionName
'AMA_Extension_Installed' = $amaExtensionInstalled
}
$csvObj | Export-Csv $using:fileName -Append -Force | Out-Null
}
# az cli sometime cannot handle many requests at same time, so delaying next request by 2 milliseconds
Start-Sleep -Milliseconds 2
}
}
function GetInventory
{
param(
$fileName = "LogAnalyticsAgentExtensionInventory.csv"
)
# create a new file
New-Item -Name $fileName -ItemType File -Force
Start-Transcript -Path $logFileName -Append
GetVmsWithLogAnalyticsAgentExtensionInstalled $fileName
GetVmssWithLogAnalyticsAgentExtensionInstalled $fileName
GetArcServersWithLogAnalyticsAgentExtensionInstalled $fileName
Stop-Transcript
}
function UninstallExtension
{
param(
$fileName = "LogAnalyticsAgentExtensionInventory.csv"
)
Start-Transcript -Path $logFileName -Append
Import-Csv $fileName | ForEach-Object -ThrottleLimit $parallelThrottleLimit -Parallel {
if ($_.Install_Type -eq "Extension")
{
$extensionName = $_.Extension_Name
$resourceName = $_.Name
Write-Debug "Uninstalling extension: $extensionName from $resourceName"
if ($_.Resource_Type -eq "VMSS")
{
# if the extension is installed with a custom name, provide the name using the flag: --extension-instance-name <extension name>
az vmss extension delete --name $extensionName --vmss-name $resourceName --resource-group $_.Resource_Group --output none --no-wait
}
elseif($_.Resource_Type -eq "VM")
{
# if the extension is installed with a custom name, provide the name using the flag: --extension-instance-name <extension name>
az vm extension delete --name $extensionName --vm-name $resourceName --resource-group $_.Resource_Group --output none --no-wait
}
elseif($_.Resource_Type -eq "ArcServer")
{
az connectedmachine extension delete --name $extensionName --machine-name $resourceName --resource-group $_.Resource_Group --no-wait --output none --yes -y
}
# az cli sometime cannot handle many requests at same time, so delaying next delete request by 2 milliseconds
Start-Sleep -Milliseconds 2
}
}
Stop-Transcript
}
$logFileName = "LogAnalyticsAgentUninstallUtilityScriptLog.log"
switch ($args.Count)
{
0 {
Write-Host "The arguments provided are incorrect."
Write-Host "To get the Inventory: Run the script as: PS> .\LogAnalyticsAgentUninstallUtilityScript.ps1 GetInventory"
Write-Host "To uninstall Log Analytics Agent from Inventory: Run the script as: PS> .\LogAnalyticsAgentUninstallUtilityScript.ps1 UninstallExtension"
}
1 {
if (-Not (Test-Path $logFileName)) {
New-Item -Path $logFileName -ItemType File
}
$funcname = $args[0]
Invoke-Expression "& $funcname"
}
2 {
if (-Not (Test-Path $logFileName)) {
New-Item -Path $logFileName -ItemType File
}
$funcname = $args[0]
$funcargs = $args[1]
Invoke-Expression "& $funcname $funcargs"
}
}
步驟 3 取得清查
您會在訂用帳戶中的所有 VM、VMSS 和已啟用 Arc 的伺服器上收集所有舊版代理程式的清單。 您會執行您在訂用帳戶中下載舊版代理程式清查的指令碼。
.\LogAnalyticsAgentUninstallUtilityScript.ps1 GetInventory
此指令碼會報告訂用帳戶中看到的 VM、VMSS 或啟用 Arc 的伺服器總計。 它需要幾分鐘的時間來執行。 您會在主控台視窗中看到進度列。 完成後,您可以在本機目錄中看到名為 "LogAnalyticsAgentExtensionInventory.csv" 且格式如下的 CSV 檔案。
Resource_ID | 名稱 | Resource_Group | Resource_Type | Install_Type | Extension_Name | AMA_Extension_Installed |
---|---|---|---|---|---|---|
012cb5cf-e1a8-49ee-a484-d40673167c9c | Linux-ama-e2e-debian9 | Linux-AMA-E2E | VM | 副檔名 | OmsAgentForLinux | True |
8acae35a-454f-4869-bf4f-658189d98516 | test2012-r2-da | test2012-r2-daAMA-ADMIN | VM | 副檔名 | MicrosoftMonitorAgent | False |
步驟 4 解除安裝清查
此指令碼會逐一查看 VM、虛擬機器擴展集和已啟用 Arc 的伺服器清單,並將舊版代理程式解除安裝。 如果代理程式未執行,則無法移除代理程式。
.\LogAnalyticsAgentUninstallUtilityScript.ps1 UninstallExtension
指令碼完成後,您就能夠在 LogAnalyticsAgentExtensionInventory.csv 檔案中查看 VM、虛擬機器擴展集和已啟用 Arc 的伺服器的移除狀態。