-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDirectRemove-SCVMHost.sql
More file actions
285 lines (190 loc) · 7.99 KB
/
DirectRemove-SCVMHost.sql
File metadata and controls
285 lines (190 loc) · 7.99 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
USE VirtualManagerDB;
DECLARE @DeleteHostId GUID;
SET @DeleteHostId = 'YourHostGUID'
PRINT N'Deleting host with GUID ' + RTRIM(CAST(@DeleteHostID AS nvarchar(50)))
PRINT N'Getting host cluster GUID'
DECLARE @HostClusterID GUID;
SET @HostClusterID =
(
SELECT HostClusterID FROM [dbo].[tbl_ADHC_Host]
WHERE HostID = @DeleteHostId
)
IF (@HostClusterID IS NOT NULL)
PRINT N'Retreived host cluster GUID ' + RTRIM(CAST(@HostClusterID AS nvarchar(50)))
ELSE
PRINT N'This host does not belong to a cluster'
PRINT N'Deleteing physical objects'
DELETE FROM [dbo].[tbl_WLC_PhysicalObject]
WHERE HostId = @DeleteHostId
PRINT N'Deleteing virtual objects'
DELETE FROM [dbo].[tbl_WLC_VObject]
WHERE HostId = @DeleteHostId
PRINT N'Prepairing to delete host network adapters'
DECLARE @HostNetworkAdapterCursor CURSOR;
DECLARE @HostNetworkAdapterID GUID;
SET @HostNetworkAdapterCursor = CURSOR FOR
(SELECT NetworkAdapterID FROM [dbo].[tbl_ADHC_HostNetworkAdapter]
WHERE HostID = @DeleteHostId
)
OPEN @HostNetworkAdapterCursor
FETCH NEXT FROM @HostNetworkAdapterCursor INTO @HostNetworkAdapterID
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'Prepairing to delete host network adapter with GUID ' + RTRIM(CAST(@HostNetworkAdapterID AS nvarchar(50)))
PRINT N'Deleting logical network mapping for host network adapter with GUID ' + RTRIM(CAST(@HostNetworkAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_NetMan_HostNetworkAdapterToLogicalNetwork]
WHERE HostNetworkAdapterID = @HostNetworkAdapterID
PRINT N'Deleting IP subnet VLAN mapping for host network adapter with GUID ' + RTRIM(CAST(@HostNetworkAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_NetMan_HostNetworkAdapterToIPSubnetVLan]
WHERE HostNetworkAdapterID = @HostNetworkAdapterID
FETCH NEXT FROM @HostNetworkAdapterCursor INTO @HostNetworkAdapterID
END
CLOSE @HostNetworkAdapterCursor
DEALLOCATE @HostNetworkAdapterCursor
PRINT N'Completing host network adapters deletion'
DELETE FROM [dbo].[tbl_ADHC_HostNetworkAdapter]
WHERE HostID = @DeleteHostId
PRINT N'Deleting virtual networks'
DELETE FROM [dbo].[tbl_ADHC_VirtualNetwork]
WHERE HostID = @DeleteHostId
PRINT N'Deleting virtual switch extensions'
DELETE FROM [dbo].[tbl_NetMan_InstalledVirtualSwitchExtension]
WHERE HostID = @DeleteHostId
PRINT N'Deleting host volumes'
DELETE FROM [dbo].[tbl_ADHC_HostVolume]
WHERE HostID = @DeleteHostId
PRINT N'Deleting pass through disks'
DELETE FROM [dbo].[tbl_WLC_VDrive]
WHERE HostDiskId IN (SELECT DiskID FROM [dbo].[tbl_ADHC_HostDisk] WHERE HostID IN (SELECT HostID FROM [dbo].[tbl_ADHC_Host] WHERE HostID = @DeleteHostId))
PRINT N'Deleting host disks'
DELETE FROM [dbo].[tbl_ADHC_HostDisk]
WHERE HostID = @DeleteHostId
PRINT N'Prepairing to delete host bus adapters'
DECLARE @HostBusAdapterCursor CURSOR;
DECLARE @HostBusAdapterID GUID;
SET @HostBusAdapterCursor = CURSOR FOR
(SELECT HbaID FROM [dbo].[tbl_ADHC_HostBusAdapter]
WHERE HostID = @DeleteHostId
)
OPEN @HostBusAdapterCursor
FETCH NEXT FROM @HostBusAdapterCursor INTO @HostBusAdapterID
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'Prepairing to delete host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
PRINT N'Deleting fiber port mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DECLARE @FiberPortID GUID;
SET @FiberPortID =
(
SELECT PortID FROM [dbo].[tbl_ADHC_FCHbaToFibrePortMapping]
WHERE FCHbaID = @HostBusAdapterID
)
DELETE FROM [dbo].[tbl_ADHC_FCHbaToFibrePortMapping]
WHERE FCHbaID = @HostBusAdapterID
PRINT N'Deleting fiber port with GUID ' + RTRIM(CAST(@FiberPortID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_FibrePort]
WHERE PortID = @FiberPortID
PRINT N'Deleting fiber channel mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_HostFibreChannelHba]
WHERE FCHbaID = @HostBusAdapterID
PRINT N'Deleting SAS host bus adapter for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_HostSASHba]
WHERE SASHbaID = @HostBusAdapterID
PRINT N'Deleting any iSCSI entries for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DECLARE @iSCSITargets TABLE
(
TargetID GUID
)
INSERT INTO @iSCSITargets (TargetID)
SELECT TargetID FROM [dbo].[tbl_ADHC_ISCSIHbaToTargetMapping]
WHERE ISCSIHbaID = @HostBusAdapterID
PRINT N'Deleting iSCSI host bus adapter to target mapping for mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_ISCSIHbaToTargetMapping]
WHERE ISCSIHbaID = @HostBusAdapterID
PRINT N'Deleting iSCSI host bus adapter to portal mapping for mapping for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_ISCSIHbaToPortalMapping]
WHERE ISCSIHbaID = @HostBusAdapterID
PRINT N'Deleting iSCSI host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_HostInternetSCSIHba]
WHERE ISCSIHbaID = @HostBusAdapterID
PRINT N'Deleting iSCSI targets for host bus adapter with GUID ' + RTRIM(CAST(@HostBusAdapterID AS nvarchar(50)))
DECLARE @iSCSITargetIDCursor CURSOR;
DECLARE @iSCSITargetID GUID;
SET @iSCSITargetIDCursor = CURSOR FOR
(SELECT TargetID FROM @iSCSITargets)
OPEN @iSCSITargetIDCursor
FETCH NEXT FROM @iSCSITargetIDCursor INTO @iSCSITargetID
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'Deleting iSCSI targets with GUID ' + RTRIM(CAST(@iSCSITargetID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_ISCSITarget]
WHERE TargetID = @iSCSITargetID
FETCH NEXT FROM @iSCSITargetIDCursor INTO @iSCSITargetID
END
CLOSE @iSCSITargetIDCursor
DEALLOCATE @iSCSITargetIDCursor
FETCH NEXT FROM @HostBusAdapterCursor INTO @HostBusAdapterID
END
CLOSE @HostBusAdapterCursor
DEALLOCATE @HostBusAdapterCursor
PRINT N'Completing host bus adapters deletion'
DELETE FROM [dbo].[tbl_ADHC_HostBusAdapter]
WHERE HostID = @DeleteHostId
PRINT N'Prepairing to delete agent servers'
DECLARE @AgentServerID GUID;
SET @AgentServerID =
(
SELECT AgentServerID FROM [dbo].[tbl_ADHC_AgentServerRelation]
WHERE HostLibraryServerID = @DeleteHostID
)
PRINT N'Deleting agent server relations'
DELETE FROM [dbo].[tbl_ADHC_AgentServerRelation]
WHERE HostLibraryServerID = @DeleteHostID
PRINT N'Deleting health monitor data for agent server with GUID ' + RTRIM(CAST(@AgentServerID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_HealthMonitor]
WHERE AgentServerID = @AgentServerID
PRINT N'Deleting agent server with GUID ' + RTRIM(CAST(@AgentServerID AS nvarchar(50)))
DELETE FROM [dbo].[tbl_ADHC_AgentServer]
WHERE AgentServerID = @AgentServerID
PRINT N'Deleting host GPUs'
DELETE FROM [dbo].[tbl_ADHC_HostGPU]
WHERE HostID = @DeleteHostId
PRINT N'Deleting host Processor Compatibility Vector Mapping'
DELETE FROM [dbo].[tbl_ADHC_HostToProcessorCompatibilityVectorMapping]
WHERE HostId = @DeleteHostId
PRINT N'Deleting physical machine record'
DECLARE @PhysicalMachineId GUID;
SET @PhysicalMachineId =
(
SELECT PhysicalMachineId FROM [dbo].[tbl_ADHC_Host]
WHERE HostID = @DeleteHostID
)
DELETE FROM [dbo].[tbl_PMM_PhysicalMachine]
WHERE PhysicalMachineId = @PhysicalMachineId
PRINT N'Deleting host'
DELETE FROM [dbo].[tbl_ADHC_Host]
WHERE HostID = @DeleteHostId
IF (@HostClusterID IS NOT NULL)
BEGIN
PRINT N'Checking to see if any other hosts are joined to the same cluster'
DECLARE @HostCount INT;
SET @HostCount =
(
SELECT COUNT(*) FROM [dbo].[tbl_ADHC_Host]
WHERE HostClusterID = @HostClusterID
)
PRINT N'There are ' + RTRIM(CAST(@HostCount AS nvarchar(50))) + N' currently joined to the same cluster'
IF (@HostCount = 0)
BEGIN
PRINT N'Deleting cluster disks'
DELETE FROM [dbo].[tbl_ADHC_ClusterDisk]
WHERE ClusterID = @HostClusterID
PRINT N'Deleting cluster'
DELETE FROM [dbo].[tbl_ADHC_HostCluster]
WHERE ClusterID = @HostClusterID
END
ELSE
PRINT N'This host is not the last host in the cluster, the cluster will be deleted upon the deletion of the last host.'
END
ELSE
PRINT N'This host does not belong to a cluster, no clusters will be deleted'
GO