forked from robotechredmond/Azure-PowerShell-Snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureRM - Move existing VM to new VNET.ps1
331 lines (228 loc) · 9.12 KB
/
AzureRM - Move existing VM to new VNET.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#-------------------------------------------------------------------------
# Copyright (c) Microsoft. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#--------------------------------------------------------------------------
# TECHNICAL NOTES:
#
# - This script is intended to move an Azure VM with ONLY a single attached
# NIC to a new VNET
#
# - VMs attached to an Azure Load Balancer or Application Gateway will need
# to be manually re-attached to a new Azure Load Balancer resource after
# all VMs in the Availability Set are moved.
#
# - This script snippet is provided as a sample demo, and as such, robust
# error handling that is common to a production script is not inclued.
#
# - This script snippet was tested using Azure PowerShell 2.x
# STEP 1 - Sign-in with Azure account
$Error.Clear()
Login-AzureRmAccount
# STEP 2 - Select Azure Subscription
$subscriptionId =
( Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription ..." `
-PassThru
).SubscriptionId
Select-AzureRmSubscription `
-SubscriptionId $subscriptionId
# STEP 3 - If needed, register ARM core resource providers
Register-AzureRmResourceProvider `
-ProviderNamespace Microsoft.Compute
Register-AzureRmResourceProvider `
-ProviderNamespace Microsoft.Storage
Register-AzureRmResourceProvider `
-ProviderNamespace Microsoft.Network
Get-AzureRmResourceProvider |
Select-Object `
-Property ProviderNamespace `
-ExpandProperty ResourceTypes
# STEP 4 - Select Azure Resource Group in which existing VM is provisioned
$rgName =
( Get-AzureRmResourceGroup |
Out-GridView `
-Title "Select Azure Resource Group in which VM is provisioned ..." `
-PassThru
).ResourceGroupName
# STEP 5 - Select VM to re-provision
$vmName =
( Get-AzureRmVm `
-ResourceGroupName $rgName
).Name |
Out-GridView `
-Title "Select a VM ..." `
-PassThru
$vm =
Get-AzureRmVm `
-ResourceGroupName $rgName `
-Name $vmName
$location =
$vm.Location
# STEP 6 - Select Resource Group in which target VNET is provisioned - VM will be moved to this Resource Group as well
$rgName2 =
( Get-AzureRmResourceGroup |
Out-GridView `
-Title "Select an Azure Resource Group in which target VNET is provisioned ..." `
-PassThru
).ResourceGroupName
# STEP 7 - Select VNET to which VM should be moved
$vnetName =
( Get-AzureRmVirtualNetwork `
-ResourceGroupName $rgName2
).Name |
Out-GridView `
-Title "Select a VNET to which VM should be moved ..." `
-PassThru
$vnet =
Get-AzureRmVirtualNetwork `
-ResourceGroupName $rgName2 `
-Name $vnetName
# STEP 8 - Select Subnet to which VM should be moved
$subnetName =
( Get-AzureRmVirtualNetworkSubnetConfig `
-VirtualNetwork $vnet
).Name |
Out-GridView `
-Title "Select a Subnet to which VM should be moved ..." `
-PassThru
$subnet =
Get-AzureRmVirtualNetworkSubnetConfig `
-VirtualNetwork $vnet `
-Name $subnetName
# STEP 9 - Get VM NIC properties for primary NIC
$nicId =
$vm.NetworkInterfaceIDs[0]
$nicName =
$nicId.Split("/")[-1]
$nic =
Get-AzureRmNetworkInterface `
-ResourceGroupName $rgName `
-Name $nicName
# STEP 10 - Detach VM NIC from Azure Load Balancer, if currently assigned.
# After all VMs in Availability Set are moved, Azure Load Balancer
# will need to be reconfigured for moved VMs.
if ( $nic.IpConfigurations[0].LoadBalancerBackendAddressPools ) {
$nic.IpConfigurations[0].LoadBalancerBackendAddressPools = $null
}
if ( $nic.IpConfigurations[0].LoadBalancerInboundNatRules ) {
$nic.IpConfigurations[0].LoadBalancerInboundNatRules = $null
}
if ( $nic.IpConfigurations[0].ApplicationGatewayBackendAddressPools ) {
$nic.IpConfigurations[0].ApplicationGatewayBackendAddressPools = $null
}
# STEP 11 - Set new properties for VM's primary NIC
# VM NIC's private IP address will be set to Dynamic,
# because VNET IP address space could be different.
# If using Static private IP address, will need to configure
# manually after script completes.
$nicIpConfigName =
$nic.IpConfigurations[0].Name
$publicIpId =
$nic.IpConfigurations[0].PublicIpAddress.Id
if ( !$publicIpId ) {
$nicNew =
Set-AzureRmNetworkInterfaceIpConfig `
-NetworkInterface $nic `
-Name $nicIpConfigName `
-Subnet $subnet
} else {
$publicIpName = $publicIpId.Split("/")[-1]
$publicIp = Get-AzureRmPublicIpAddress -ResourceGroupName $rgName -Name $publicIpName
$nicNew =
Set-AzureRmNetworkInterfaceIpConfig `
-NetworkInterface $nic `
-Name $nicIpConfigName `
-Subnet $subnet `
-PublicIpAddress $publicIp
}
# STEP 12 - Clean-up VM config to reflect deployment from attached disks
$vm.StorageProfile.OSDisk.Name = $vmName
$vm.StorageProfile.OSDisk.CreateOption = "Attach"
$vm.StorageProfile.DataDisks |
ForEach-Object { $_.CreateOption = "Attach" }
$vm.StorageProfile.ImageReference = $null
$vm.OSProfile = $null
# STEP 13 - If VM is in an availability set, move to new availabity set
if ( $vm.AvailabilitySetReference ) {
$asName = $vm.AvailabilitySetReference.Id.Split("/")[-1]
# Define new Availability Set name for VMs in new VNET
$asNewName = "${asName}-${vnetName}"
New-AzureRmAvailabilitySet `
-ResourceGroupName $rgName2 `
-Name $asNewName `
-Location $location `
-ErrorAction SilentlyContinue
$as =
Get-AzureRmAvailabilitySet `
-ResourceGroupName $rgName2 `
-Name $asNewName
$asRef = New-Object Microsoft.Azure.Management.Compute.Models.SubResource
$asRef.id = $as.id
$vm.AvailabilitySetReference = $asRef
}
# STEP 14 - Re-provision VM with new configuration settings
If ( !$Error ) {
$yn = @("Yes","No") |
Out-GridView `
-Title "OK to reconfigure existing VM ${vmName}?" `
-PassThru
if ( $yn -eq "Yes" ) {
# Stop and de-allocate existing VM
Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName
# Remove existing VM, but preserve virtual hard disks
Remove-AzureRmVM -ResourceGroupName $rgName -Name $vmName
# Reconfigure existing Network Interface for new Subnet
Set-AzureRmNetworkInterface -NetworkInterface $nicNew
# Re-provision VM from attached disks
$vm |
New-AzureRmVm `
-ResourceGroupName $rgName2 `
-Location $location
}
}
# STEP 15 - Move NIC, NSG and PublicIp to Resource Group with VM (if different)
# Storage Accounts could be a shared resource across several VMs, so this
# script doesn't attempt to move them.
$nic =
Get-AzureRmNetworkInterface `
-ResourceGroupName $rgName `
-Name $nicName
# Move NIC resource to new Resource Group, if needed
$nicId = $nic.Id
$nicRgName = $nic.ResourceGroupName
if ( $nicRgName -ne $rgName2 ) {
Move-AzureRmResource `
-ResourceId $nicId `
-DestinationResourceGroupName $rgName2
}
# Move NSG resource to new Resource Group, if needed
$nsgId = $nic.NetworkSecurityGroup.Id
if ( $nsgId ) {
$nsgRgName = $nsgId.Split("/")[4]
if ( $nsgRgName -ne $rgName2 ) {
Move-AzureRmResource `
-ResourceId $nsgId `
-DestinationResourceGroupName $rgName2
}
}
# Move Public IP resource to new Resource Group, if needed
$publicIpId = $nic.IpConfigurations[0].PublicIpAddress.Id
if ( $publicIpId ) {
$publicIpRgName = $publicIpId.Split("/")[4]
if ( $publicIpRgName -ne $rgName2 ) {
Move-AzureRmResource `
-ResourceId $publicIpId `
-DestinationResourceGroupName $rgName2
}
}