forked from SquirrelAssassin/Azure-cloneVM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloneVM.ps1
275 lines (238 loc) · 15.3 KB
/
cloneVM.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
##### Read Me #####
#
# - Tested as of 2/21/2017
# - Authored by [email protected]
#
# Basic Info
# - This script is written to clone everything that goes along with a vm to somewhere new
# - Resouces that require a unique name will have a random 3 char number added to the end of the name
# - Future releass will include a move feature
# - Right now a new public nic will be created
# - No Source content will be removed
# - All interfaces will be replicated
# - If you specifiy a storage account type this will be used for all storage accounts associated with the VM
#
# Assumptions
# - All storage accounts go into the same Resource Group
# - Single Machines not in Availablity Sets
#
# .Parameter [String] $sourceVmName = Source VM Name
#
# .Parameter [String] $destinationVmName = Destination VM Name
#
# .Parameter [string] $destinationResourceGroup = If you want the Destination Name to be different from the destionationVmName
#
# .Parameter [string] $subscriptionID = Subscription ID
#
# .Parameter [string] $destinationResourceGroupLocation = Destination Location IE eastus,centralus
#
# .Parameter [string] $destinationStorageType = Destination Storage Type IE Premium_LRS, Standard_GRS, Standard_LRS, Standard_RAGRS
#
# .parameter [switch] $powerDownSource = If you want to power down the source VM
#
# .parameter [switch] $newVmSize = If you want to pick some other size then what the source is
#
# .Parameter [switch] $skipAuth = If you want to skip logging into azure rm because you already are logged in
#
# Example : .\cloneVM.ps1 -sourceVmName desktop-123 -destinationVmName waka-44rfv -skipauth -powerdownsource -subscriptionID 0c644443-3333-4444-4444-3444de4444ef -destinationResourceGroupLocation eastus -destinationStorageType Standard_LRS
#
###################
[CmdletBinding()]Param (
[Parameter(Mandatory = $True)]
[String] $sourceVmName,
[Parameter(Mandatory = $True)]
[String] $destinationVmName,
[Parameter(Mandatory = $False)]
[string] $destinationResourceGroup,
[Parameter(Mandatory = $False)]
[string] $subscriptionID,
[Parameter(Mandatory = $False)]
[string] $destinationResourceGroupLocation,
[Parameter(Mandatory = $False)]
[string] $destinationStorageType,
[Parameter(Mandatory = $False)]
[switch]$powerDownSource,
[Parameter(Mandatory = $False)]
[switch]$newVmSize,
[Parameter(Mandatory = $False)]
[switch]$skipAuth
)
do {
if ($destinationVmName.Length -gt 15) {
Write-host "The destination VM name has be be 15 charecters or less"; $destinationVmName = Read-Host "Please enter a new name Under 15 charecters"}
}
until ($destinationVmName.Length -le 15)
# If you testing and already authed you can skip it
if ($skipauth -ne $true) {
Login-AzureRmAccount
}
# Select the subscription
if (!$subscriptionID) {
$sub = Set-AzureRmContext -SubscriptionId (Get-AzureRmSubscription | Out-GridView -Title "Pick A Subscription" -PassThru).subscriptionid
$subscription = $sub.Subscription.SubscriptionId
}
Else {
$subscription = $subscriptionID
}
# Destination VM Size
if ($newVmSize -eq $true) {
$vmSize = (Get-AzureRoleSize | where SupportedByVirtualMachines -eq $true | select InstanceSize | Out-GridView -PassThru).instancesize
}
Else {$vmSize = (Get-AzureRmVM -wa Ignore -InformationAction Ignore | where {$_.name -eq $sourceVmName}).HardwareProfile.VmSize
}
#
# Destionation Resource Group
if (!$destinationResourceGroup) {
$destinationResourceGroup = $destinationVmName.ToLower()
}
# Gather Source Info
$sourceResourceGroup = (Get-AzureRmVM -ea SilentlyContinue -wa Ignore -InformationAction Ignore | where {$_.name -eq $sourceVmName}).ResourceGroupName
$sourcevhdOSName = (Get-AzureRmVM -InformationAction Ignore -wa Ignore | where {$_.name -eq $sourceVmName} -ea SilentlyContinue -wa Ignore -InformationAction Ignore).StorageProfile.OsDisk
$sourceDataDisks = (Get-AzureRmVM -name $sourceVmName -ResourceGroupName $sourceResourceGroup -ea SilentlyContinue -wa Ignore -InformationAction Ignore).StorageProfile.DataDisks.vhd.uri
$sourceDataDisksProperties = (Get-AzureRmVM -name $sourceVmName -ResourceGroupName $sourceResourceGroup -ea SilentlyContinue -wa Ignore -InformationAction Ignore).StorageProfile.DataDisks
$sourceOSDisks = (Get-AzureRmVM -name $sourceVmName -ResourceGroupName $sourceResourceGroup -ea SilentlyContinue -wa Ignore -InformationAction Ignore).StorageProfile.OSDisk.vhd.uri
# Deallocat Old VM
if ($powerdownsource -eq $true) {
if ($(Get-AzureRmVM -ResourceGroupName $sourceResourceGroup -Name $sourceVmName -Status -wa Ignore -InformationAction Ignore | select -ExpandProperty Statuses | ?{ $_.Code -match "PowerState" } | select `
-ExpandProperty DisplayStatus) -ne 'vm deallocated'){
$continue = 'yes'
}}
Else {
if ($(Get-AzureRmVM -ResourceGroupName $sourceResourceGroup -Name $sourceVmName -Status -wa Ignore -InformationAction Ignore | select -ExpandProperty Statuses | ?{ $_.Code -match "PowerState" } | select `
-ExpandProperty DisplayStatus) -ne 'vm deallocated'){
write-host "In order to copy the VM $sourceVmName it must be Deallocated, Enter yes to Deallocate the $sourceVmName or enter no to exit"
$continue = $($yn = 'yes','no' ; $yn | Out-GridView -PassThru -Title "Shutdown VM $sourceVmName ????")
}
}
if ($continue -eq "yes") {
Write-host "Please wait while the VM is deallocated"; Stop-AzureRmVM -Name $sourceVmName -ResourceGroupName $sourceResourceGroup -force -wa Ignore -InformationAction Ignore
}
elseif ($continue -eq "no") {exit}
# Create Destination Resource Group
$dstResourceGroup = Get-AzureRmResourceGroup -Name $destinationResourceGroup -ea SilentlyContinue
if(!$dstResourceGroup){
if (!$destinationResourceGroupLocation) {
Write-Host "Resource group '$destinationResourceGroup' does not exist. To create a new resource group, please enter a location.";
$resourceGroupLocation = ((Get-AzureRmLocation | select location) | Out-GridView -Title "Pick The Destination Location" -PassThru).location
}
else {
$resourceGroupLocation = $destinationResourceGroupLocation
}
Write-Host "Creating resource group '$destinationResourceGroup' in location '$resourceGroupLocation'";
New-AzureRmResourceGroup -Name $destinationResourceGroup -Location $resourceGroupLocation -wa SilentlyContinue -InformationAction Ignore
}
Else{
Write-Host "Using existing resource group $destinationResourceGroup"
# ISSUE
# Need to capture the location of the named (passed by parameter) destination resource group if it already exists
# Otherwise, create the new storage account will fail
$resourceGroupLocation = $dstResourceGroup.Location
}
# Create Storage Account if it doesnt exist
$randomNumber = 1..9
# You can change randomNumber to whatever value you want to add to the end of all resources
$randomNumber = (-join (Get-Random $randomNumber -count 3))
$saArray = @(); $upArray = @()
foreach ($sa in ($sourceDataDisks + $sourceOSDisks)) {$saArray +=@(($sa.split('//')[2]).split('.')[0])}
$saArray = $saArray | select -Unique
$saArray | % {$upArray +=, @($_,($_ + $randomNumber ))}
foreach ($Storage in $upArray) {
$storageAct = $storage[1]
if (!(Get-AzureRmStorageAccount -ResourceGroupName $destinationResourceGroup -Name $storageAct.ToLower() -ea SilentlyContinue -wa Ignore -InformationAction Ignore)) {
do {
if ((Get-AzureRmStorageAccountNameAvailability -Name $storageAct.ToLower() -wa Ignore -InformationAction Ignore).NameAvailable -eq $True) {
if (!$destinationStorageType) {
write-host "Pick The Storage Type For $storageAct"
$storageType = $($sTypes = 'Standard_LRS','Premium_LRS', 'Standard_GRS', 'Standard_RAGRS'; $sTypes | Out-GridView -Title "Pick The Storage Type For $storageAct" -PassThru)
}
else {$storageType = $destinationStorageType}
$strQuit = 'Yes'; New-AzureRmStorageAccount -ResourceGroupName $destinationResourceGroup -Name $storageAct.ToLower() -Location $resourceGroupLocation -SkuName $storageType -wa SilentlyContinue -InformationAction Ignore
}
else {
write-host "That name is already taken please try running the script again"
exit
}
}
until ($strQuit -eq 'Yes')
}
}
# Create Container if it doesnt exist
foreach ($vhdFolder in ($sourceDataDisks + $sourceOSDisks)) {
$vhdContainer = $vhdFolder.split('//')[3]
$vhdContainerSa = ($VhdFolder.split('//')[2]).split('.')[0]
$azureRmStorageAccount = (Get-AzureRmStorageAccount -ResourceGroupName $destinationResourceGroup -Name $($vhdContainerSa + $randomNumber) -wa Ignore -InformationAction Ignore | Get-AzureStorageContainer -ea SilentlyContinue -wa Ignore -InformationAction Ignore)
if (($azureRmStorageAccount | where {$_.name -eq $vhdContainer}).count -eq 1) {}
else {
Get-AzureRmStorageAccount -resourcegroup $destinationResourceGroup -name $($vhdContainerSa + $randomNumber) -wa Ignore -InformationAction Ignore | New-AzureStorageContainer -Name $vhdContainer -wa SilentlyContinue -InformationAction Ignore
}
}
# COPY BLOBS
$diskArray = @()
# Loop Through Disks
foreach ($disk in ($sourceDataDisks + $sourceOSDisks)) {
# Strip apart the blob URL
$storageAccount = ($disk.split('//')[2]).split('.')[0];
$containerName = $disk.split('//')[3];
$vhdName = $disk.split('//')[4]
# Get Keys and Contect
$srcKey = ((Get-AzureRmStorageAccountKey -ResourceGroupName $((Get-AzureRmStorageAccount | ? {$_.StorageAccountName -eq $storageAccount}).ResourceGroupName) -Name $storageAccount -wa SilentlyContinue).Value)[0]
$srcContext = New-AzureStorageContext -StorageAccountName $storageAccount -StorageAccountKey $srckey -wa SilentlyContinue
$destKey = ((Get-AzureRmStorageAccountKey -ResourceGroupName $destinationResourceGroup -Name $($storageAccount + $randomNumber) -wa SilentlyContinue).Value)[0]
$destContext = New-AzureStorageContext -StorageAccountName $($storageAccount + $randomNumber) -StorageAccountKey $destKey -wa SilentlyContinue
$vhds = Get-AzureStorageBlob -Container $containerName -Context $srcContext -wa SilentlyContinue| ? {$_.name -like "$($vhdName)"}
# Start the Copy
$copy = Start-AzureStorageBlobCopy -DestContainer $containerName -DestContext $destContext -SrcBlob $vhdName -DestBlob $vhdName -Context $srcContext -SrcContainer $containerName -Force -wa SilentlyContinue -InformationAction Ignore
$diskArray +=,@($destContext, $vhdname, $containerName)
}
# Create a Network Subnet
# make subnet
$subIntId = (Get-AzureRmVM -ResourceGroupName $sourceResourceGroup -Name $sourceVmName -ea SilentlyContinue -wa ignore).NetworkProfile.NetworkInterfaces
$subIntId | % {
$subNicInfo1 = (Get-AzureRmNetworkInterface -ResourceGroupName $sourceResourceGroup -Name $($_.id.Split("/")| select -Last 1) -ea SilentlyContinue -wa ignore)
$subVnetName1 = (($subnicinfo1.IpConfigurations.subnet.id).Split("/") | select -Last 3)[0]
$subNicInfo = (Get-AzureRmNetworkInterface -ResourceGroupName $sourceResourceGroup -Name $($_.id.Split("/")| select -Last 1) -ea SilentlyContinue -wa ignore)
$subSubNetId = $subnicinfo.IpConfigurations.subnet.id
$subSubNetConfig = (Get-AzureRmVirtualNetworkSubnetConfig -VirtualNetwork (Get-AzureRmVirtualNetwork -ResourceGroupName $sourceResourceGroup -name $subVnetName1))
$subSubNetConfigFinal = New-AzureRmVirtualNetworkSubnetConfig -Name $subSubNetConfig.name -AddressPrefix $subSubNetConfig.AddressPrefix
}
# Create a Network
$netIntId = (Get-AzureRmVM -ResourceGroupName $sourceResourceGroup -Name $sourceVmName -ea SilentlyContinue -wa SilentlyContinue).NetworkProfile.NetworkInterfaces
$netIntId | % {
$netNicInfo = (Get-AzureRmNetworkInterface -ResourceGroupName $sourceResourceGroup -Name $($_.id.Split("/")| select -Last 1) -ea SilentlyContinue -wa SilentlyContinue)
$netVnetName = (($netnicinfo.IpConfigurations.subnet.id).Split("/") | select -Last 3)[0]
$netAddressPrefix = $((Get-AzureRmVirtualNetwork -ResourceGroupName $sourceResourceGroup -Name $netVnetName -wa SilentlyContinue).Subnets.addressprefix)
$netVnetFinal = New-AzureRmVirtualNetwork -ResourceGroupName $destinationResourceGroup -Name $netVnetName -Location $resourceGroupLocation -Subnet $subSubNetConfigFinal -AddressPrefix $netAddressPrefix -wa SilentlyContinue -InformationAction Ignore
}
# Create NSG
$intId = (Get-AzureRmVM -ResourceGroupName $sourceResourceGroup -Name $sourceVmName -ea SilentlyContinue -wa SilentlyContinue).NetworkProfile.NetworkInterfaces
$intId | % {
$nicInfo = (Get-AzureRmNetworkInterface -ResourceGroupName $sourceResourceGroup -Name $($_.id.Split("/") | select -Last 1) -ea SilentlyContinue -wa SilentlyContinue)
$nsgId = $nicInfo.NetworkSecurityGroup.Id
$nsg = Get-AzureRmNetworkSecurityGroup -ResourceGroupName $sourceresourcegroup -Name $($nsgId.split("/") | select -last 1) -ea SilentlyContinue -wa SilentlyContinue
$newNsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $destinationResourceGroup -Name $nsg.Name -SecurityRules $nsg.SecurityRules -location $resourceGroupLocation -ea SilentlyContinue -wa SilentlyContinue -InformationAction Ignore
}
# Creates vmconfig and sets size
$VirtualMachine = New-AzureRmVMConfig -VMName $destinationVmName -VMSize $VMSize
# Create a public ip and Network Interface and Attaches the Public IP
$pubIp = (Get-AzureRmVM -ResourceGroupName $sourceResourceGroup -Name $sourceVmName -ea SilentlyContinue -wa Ignore -InformationAction Ignore).NetworkProfile.NetworkInterfaces
$pubIp | % {
$pubName = $($destinationVmName + $randomNumber)
$pip=New-AzureRmPublicIpAddress -Name $pubName -ResourceGroupName $destinationResourceGroup -Location $resourceGroupLocation -AllocationMethod Dynamic -wa Ignore -InformationAction Ignore
$NIC=New-AzureRmNetworkInterface -Name $pubName -ResourceGroupName $destinationResourceGroup -Location $resourceGroupLocation -NetworkSecurityGroupId $newNsg.Id `
-SubnetId $netvnetfinal.Subnets.id -PublicIpAddressId $pip.Id -wa Ignore -InformationAction Ignore
$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id -InformationAction Ignore -wa Ignore
}
# Add OS Disk to VM
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -Name $destinationVmName -VhdUri $($sourceOSDisks.replace($($sourceOSDisks.split("/").split(".")[2]), $($sourceOSDisks.split("/").split(".")[2] + $randomNumber))) -Caching "ReadWrite" -CreateOption Attach -Windows
# Add Data Disk to VM
$sourceDataDisksProperties | % {
$virtualMachineVhdUri = $($_.vhd.uri.replace($(($_.Vhd.Uri.split("/").split(".")[2])), $(($_.Vhd.Uri.split("/").split(".")[2]) + $randomNumber)))
$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name $_.name -VhdUri $virtualMachineVhdUri -CreateOption Attach -Lun $_.Lun -DiskSizeInGB $_.DiskSizeGB
}
# Wait For Drives to Finish Copying
$diskArray | % {
Write-Host "Please wait for drives to finish copying."
Get-AzureStorageBlobCopyState -Context $_.context -Blob $_[1] -Container $_[2] -wa SilentlyContinue -InformationAction Ignore -WaitForComplete
}
#Create VM
New-AzureRmVM -ResourceGroupName $destinationResourceGroup -Location $resourceGroupLocation -VM $VirtualMachine