-
Notifications
You must be signed in to change notification settings - Fork 38
/
Remove-Hostpool.ps1
146 lines (137 loc) · 5.4 KB
/
Remove-Hostpool.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<#
.DESCRIPTION
Used to remove a Host Pool from WVD.
This script will not remove the Session Host VM's from Azure.
Script check that the user is logged in, runs the log in command if not.
This script is offered as is with no warranty.
Test it before you trust it.
Requires the RDS PowerShell Modules
Download here https://learn.microsoft.com/en-us/powershell/windows-virtual-desktop/overview
.NOTES
Author : Travis Roberts
Website : www.ciraltos.com
Version : 1.0.0.0 Initial Build
#>
# Test if user is logged in to WVD, log them in if not
if ((Get-RdsContext -ErrorAction SilentlyContinue) -eq $null) {
Write-Output "Use the login window to connect to WVD" -ForegroundColor Red
Add-RdsAccount -DeploymentUrl "https://rdbroker.wvd.microsoft.com"
}
# Get the tenant name
try {
$tenantName = (Get-RdsTenant -ErrorAction stop).TenantName
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error getting the Tenant name ' + $ErrorMessage)
Exit
}
$message = 'Confirm Tenant Name'
$question = "Please confirm the Tenant name is $tenantName to continue"
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host "Tenant Name $tenantName confirmed"
}
else {
Write-Host 'Verify that you logged into the correct tenant and try again.'
exit
}
# Build a list of HostPools
try {
$hostPools = @(Get-RdsHostPool -ErrorAction stop -TenantName $tenantName)
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error getting the list of host pools ' + $ErrorMessage)
Exit
}
if ($hostPools -ne $null) {
$message = 'Select the Host Pool'
$question = "Please type the name of the Host Pool to remove"
$choices = @()
For ($index = 0; $index -lt $hostPools.Count; $index++) {
$choices += New-Object System.Management.Automation.Host.ChoiceDescription ($hostPools[$index]).HostPoolName
}
$options = [System.Management.Automation.Host.ChoiceDescription[]]$choices
$result = $host.ui.PromptForChoice($message, $question, $options, 0)
$hostPoolName = ($hostPools[$result]).HostPoolName
}
else {
write-host 'No Host Pools found, exiting script.'
exit
}
# Confirm the Host Pool Name
write-host "##### You are about to permanently delete the Host Pool $hostPoolName from the WVD Tenant $tenantName #####" -ForegroundColor red
$message = 'Confirm Host Removal'
$question = "Please confirm that you want to permanently delete Host Pool $hostPoolName from the Tenant $TenantName"
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host "One more chance to change your mind" -ForegroundColor red -BackgroundColor white
$message = 'Confirm Host Removal'
$question = "Please confirm that you want to permanently delete Host Pool $hostPoolName from the Tenant $TenantName"
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($message, $question, $choices, 1)
if ($decision -eq 0) {
# Remove App Group Users
$appGroups = Get-RdsAppGroup -TenantName $tenantName -HostPoolName $hostPoolName
foreach ($appGroup in $appGroups) {
$appGroupName = $appGroup.AppGroupName
write-host "Removing Users from App Group $appGroupName"
try {
Get-RdsAppGroupUser -ErrorAction Stop -TenantName $tenantName -HostPoolName $hostPoolName -AppGroupName $appGroupName | Remove-RdsAppGroupUser
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error removing App Group User ' + $ErrorMessage)
}
# Code to remove remote apps
try {
if ($appGroup.ResourceType -eq "RemoteApp") {
write-host "Removing published apps from $appGroupName"
get-RdsRemoteApp -TenantName $tenantName -HostPoolName $hostpoolname -AppGroupName $appGroupName | Remove-RdsRemoteApp
}
}
catch {
$ErrorMessage = $_.Exception.message
write-error ("Error removing Remote App $appGroupName " + $ErrorMessage)
}
#####
write-host "Removing $appGroupName"
try {
Remove-RdsAppGroup -ErrorAction Stop -TenantName $tenantName -HostPoolName $hostPoolName -Name $appGroupName
}
catch {
$ErrorMessage = $_.Exception.message
write-error ("Error removing app group $appGroupName " + $ErrorMessage)
}
}
Write-Host "Removing Session Hosts from Host Pool $hostPoolName"
try {
Get-RdsSessionHost -ErrorAction Stop -TenantName $tenantName -HostPoolName $hostPoolName | Remove-RdsSessionHost
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error removing Session Hosts ' + $ErrorMessage)
Break
}
Write-Host "Removing Host Pool $hostPoolName"
try {
Remove-RdsHostPool -ErrorAction Stop -TenantName $tenantName -HostPoolName $hostPoolName
}
catch {
$ErrorMessage = $_.Exception.message
write-error ('Error removing host pool ' + $ErrorMessage)
Break
}
}
else {
Write-Host 'Removal canceled'
exit
}
}
else {
Write-Host 'Removal canceled'
exit
}