-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyFromAzure.ps1
192 lines (98 loc) · 5.97 KB
/
CopyFromAzure.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
#Please enter the name of the VM. The VHD file associated with the VM will be copied to Lab
$vmName = 'TestVM'
#Please enter the name of the Lab where you want to copy the VHD file.
$labName = 'ContosoLab86'
#Please enter the name of the VHD file with extension as .vhd. You will identify the file with this name while creating template.
$vhdFileName = 'TestVM.vhd'
#If you have created the VM from management portal or created the VM using Service Management Stack from preview portal then enter $true otherwise enter $false.
#If you have created the VM inside a Lab then please set the variable as $false
$isVMClassic = $false
#Enter the Subscription Id
$subscriptionId = '00c7b539-06b3-4dbc-8b04-2963d83c5c79'
#If you dont want to enter credential again and again, please set this variable to false
$skipCredential = $true
####### Main ###############################################################
If($skipCredential -eq $false){
$accounts = Get-AzureAccount
foreach($acc in $accounts){
Remove-AzureAccount -Name $acc.Id -Force -WarningAction SilentlyContinue
}
Add-AzureAccount | Out-Null
}
Select-AzureSubscription -SubscriptionId $subscriptionId -WarningAction SilentlyContinue
Select-AzureRMSubscription -SubscriptionId $subscriptionId -WarningAction SilentlyContinue
#Switch-AzureMode -Name AzureResourceManager -WarningAction SilentlyContinue
Write-Host 'Searching for the Lab..'
$labs = Find-AzureRmResource -ResourceType 'Microsoft.DevTestLab/labs'
$isLabPresent = $false
foreach($lab in $labs){
if($lab.ResourceName -eq $labName){
$isLabPresent = $true
Write-Host 'Lab search successful.'
$properties = (Get-AzureRMResource -ResourceType 'Microsoft.DevTestLab/labs' -ResourceName $lab.ResourceName -ResourceGroupName $lab.ResourceGroupName -WarningAction SilentlyContinue).Properties
Write-Host 'Fetching the storage account and storage account key for the Lab..'
$labStorageAccountId = $properties.DefaultStorageAccount.Split('/')
$labStorageAccountName = $labStorageAccountId[$labStorageAccountId.Length-1]
$labStorageAccountKey = (Get-AzureRMStorageAccountKey -StorageAccountName $labStorageAccountName -ResourceGroupName $lab.ResourceGroupName).key1
Write-Host 'Successfully fetched the storage account and storage account key for the Lab..'
}
}
if($isLabPresent -eq $false){
THROW 'Lab $labName does not exist in the subscription provided'
}
$resourceType = 'Microsoft.Compute/virtualMachines'
if($isVMClassic -eq $true){
$resourceType = 'Microsoft.ClassicCompute/virtualMachines'
}
$vms = Find-AzureRmResource -ResourceType $resourceType
Write-Host 'Searching VM in the subscription..'
foreach($vm in $vms){
if($vm.ResourceName -eq $vmName){
Write-Host 'VM Search successful.'
if($isVMClassic -eq $true){
#Switch-AzureMode -Name AzureServiceManagement -WarningAction SilentlyContinue
$disk = Get-AzureDisk | Where-Object { $_.AttachedTo.RoleName -eq "$vmName" }
$sourceUri = $disk.MediaLink.AbsoluteUri
$vmStorageAccountName = $disk.MediaLink.Host.Split('.')[0]
$vmStorageAccountKey = (Get-AzureStorageKey -StorageAccountName $vmStorageAccountName).Primary
$classicVm = Get-AzureVM | Where-Object{$_.Name -eq $vmName}
Write-Host 'Successfully fetched the storage account and storage account key for the VM.'
Write-Host 'Stopping VM..'
Stop-AzureVM -Name $classicVm.Name -ServiceName $classicVm.ServiceName -Force | Out-Null
Write-Host 'Successfully stopped VM'
#Switch-AzureMode -Name AzureResourceManager
}else{
$properties = (Get-AzureRMResource -ResourceType $resourceType -ResourceName $vm.ResourceName -ResourceGroupName $vm.ResourceGroupName).Properties
$sourceUri = $properties.storageProfile.osDisk.vhd.uri
$uri = New-Object System.Uri($sourceUri)
$vmStorageAccountName = $uri.Host.Split('.')[0]
$storageAccounts = Find-AzureRmResource -ResourceType 'Microsoft.Storage/storageAccounts'
foreach($storageAccount in $storageAccounts){
if($storageAccount.ResourceName -eq $vmStorageAccountName){
$vmStorageAccountRG = $storageAccount.ResourceGroupName
}
}
$vmStorageAccountKey = (Get-AzureRMStorageAccountKey -StorageAccountName $vmStorageAccountName -ResourceGroupName $vmStorageAccountRG).key1
Write-Host 'Successfully fetched the storage account and storage account key for the VM.'
Write-Host 'Stopping VM..'
Stop-AzureRMVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Force | Out-Null
Write-Host 'Successfully stopped VM'
}
break;
}
}
$srcContext = New-AzureStorageContext –StorageAccountName $vmStorageAccountName -StorageAccountKey $vmStorageAccountKey
$destContext = New-AzureStorageContext –StorageAccountName $labStorageAccountName -StorageAccountKey $labStorageAccountKey
$copyHandle = Start-AzureStorageBlobCopy -srcUri $sourceUri -SrcContext $srcContext -DestContainer 'uploads' -DestBlob $vhdFileName -DestContext $destContext -Force
Write-Host "Copy started..."
$copyStatus = $copyHandle | Get-AzureStorageBlobCopyState
While($copyStatus.Status -eq "Pending"){
$copyStatus = $copyHandle | Get-AzureStorageBlobCopyState
$perComplete = ($copyStatus.BytesCopied/$copyStatus.TotalBytes)*100
Write-Progress -Activity "Copying blob..." -status "Percentage Complete" -percentComplete "$perComplete"
Start-Sleep 10
}
if($copyStatus.Status -eq "Success")
{
Write-Host "$vhdFileName successfully copied to Lab $labName "
}