-
Notifications
You must be signed in to change notification settings - Fork 119
/
Manage-VmExtensions.ps1
66 lines (63 loc) · 2.44 KB
/
Manage-VmExtensions.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Param
(
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName = "ienopcore-rg",
[switch]$RemoveFailed,
[switch]$RemoveAll
)
$vmList = Get-AzureRmVM -ResourceGroupName $ResourceGroupName
foreach ($vm in $vmList)
{
if ($vm.Extensions -eq $null -or $vm.Extensions.Count -eq 0)
{
Write-Host -ForegroundColor Yellow "NONE: $($vm.Name) extension: <absent>."
}
else
{
foreach ($ext in $vm.Extensions)
{
$extIdParts = $ext.Id -split '/'
$extIdName = $extIdParts[$extIdParts.Length - 1]
$extDetails = Get-AzureRmVMExtension -ResourceGroupName $ResourceGroupName -VMName $vm.Name -Name $extIdName
if ($extDetails.ProvisioningState -eq "Failed")
{
if ($RemoveFailed -or $RemoveAll)
{
Write-Host -NoNewLine -ForegroundColor Red "FAIL: $($vm.Name) extension: $extIdName. Removing..."
Remove-AzureRmVMExtension -ResourceGroupName $ResourceGroupName -VMName $vm.Name -Name $extIdName -Force | Out-Null
Write-Host "[Done]"
}
else
{
Write-Host -ForegroundColor Red "FAIL: $($vm.Name) extension: $extIdName."
}
}
elseif ($extDetails.ProvisioningState -eq "Succeeded")
{
if ($RemoveAll)
{
Write-Host -NoNewLine "OK: $($vm.Name) extension: $extIdName. Removing..."
Remove-AzureRmVMExtension -ResourceGroupName $ResourceGroupName -VMName $vm.Name -Name $extIdName -Force | Out-Null
Write-Host "[Done]"
}
else
{
Write-Host "OK: $($vm.Name) extension: $extIdName."
}
}
else
{
if ($RemoveAll)
{
Write-Host -NoNewLine -ForegroundColor Yellow "$($extDetails.ProvisioningState): $($vm.Name) extension: $extIdName. Removing..."
Remove-AzureRmVMExtension -ResourceGroupName $ResourceGroupName -VMName $vm.Name -Name $extIdName -Force | Out-Null
Write-Host "[Done]"
}
else
{
Write-Host -ForegroundColor Yellow "$($extDetails.ProvisioningState): $($vm.Name) extension: $extIdName."
}
}
}
}
}