-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForceRemoveMMAAndOMSAgents.ps1
47 lines (38 loc) · 2.44 KB
/
ForceRemoveMMAAndOMSAgents.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Disclaimer
# This script is provided for testing purposes only. It should be tested out internally before being used in Production Environment.
# The author provides this script "as is" without warranty of any kind, either expressed or implied.
# The user assumes all risks associated with the use of this script. Before implementing it in a production environment,
# it is strongly recommended that the user thoroughly test the script and review it for any potential issues or risks.
# By using this script, you agree to these terms and acknowledge that any damages or issues arising from its use are the sole responsibility of the user.
# Read the CSV file with VM details
$vmList = Import-Csv "VMList.csv"
foreach ($vm in $vmList) {
$SubscriptionId = $vm.SubscriptionId
$ResourceGroupName = $vm.ResourceGroupName
$VMScaleSetName = $vm.VMSSName
$VMInstanceID = $vm.InstanceId
# Connect to the Azure account (make sure you are logged in)
Set-AzContext -Subscription $SubscriptionId
# Get the InstanceView of the VM
$VM = Get-AzVmssVM -ResourceGroupName $ResourceGroupName -VMScaleSetName $VMScaleSetName -InstanceId $VMInstanceID
# Check if MMA extension is installed
$MMAExtension = $VM.Resources | Where-Object { $_.Publisher -eq 'Microsoft.EnterpriseCloud.Monitoring' -and $_.VirtualMachineExtensionType -eq 'MicrosoftMonitoringAgent' }
if ($MMAExtension -ne $null) {
$VM.Resources.Remove($MMAExtension)
Update-AzVmssVM -VirtualMachineScaleSetVM $VM
Write-Output "MMA extension removed successfully from Instance $VMInstanceID in VMSS $VMScaleSetName in resource group $ResourceGroupName."
}
else {
Write-Output "MMA extension is not installed on Instance $VMInstanceID in VMSS $VMScaleSetName in resource group $ResourceGroupName."
}
# Check if OMS extension is installed
$OMSExtension = $VM.Extensions | Where-Object { $_.Publisher -eq 'Microsoft.EnterpriseCloud.Monitoring' -and $_.VirtualMachineExtensionType -eq 'OmsAgentForLinux' }
if ($OMSExtension -ne $null) {
$VM.Resources.Remove($OMSExtension)
Update-AzVmssVM -VirtualMachineScaleSetVM $VM
Write-Output "OMS extension removed successfully from Instance $VMInstanceID in VMSS $VMScaleSetName in resource group $ResourceGroupName."
}
else {
Write-Output "OMS extension is not installed on Instance $VMInstanceID in VMSS $VMScaleSetName in resource group $ResourceGroupName."
}
}