forked from Azure/azure-vm-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Register-Images.ps1
60 lines (47 loc) · 2.53 KB
/
Register-Images.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
Param([string]$sourceURI = $(Read-Host -prompt "Specify the full URL to the source blob"),
[string]$storageId = $(Read-Host -prompt "Specify the full storage account ARM-ID"),
[string]$location = $(Read-Host -prompt "Specify the source location"),
[string]$imageName = $(Read-Host -prompt "Specify the image name"))
$RGName="Images"
$location = $location.Replace(' ','')
$snapName=$imageName+$location+"-snap"
$imageNameRegional=$imageName+$location
$subscriptions=Get-AzureRmSubscription
foreach ($sub in $subscriptions){
Set-AzureRmContext -Subscription $sub.Name
$ctx = Get-AzureRmContext
Start-Job {param ($ctx,$sub,$location,$sourceURI,$storageId,$RGName,$snapName,$imageNameRegional)
#
# Create a snapshot from the remote blob
#
write-host "Creating Snapshot"
$snapshotConfig = New-AzureRmSnapshotConfig -AccountType StandardLRS `
-OsType Windows `
-Location $location `
-CreateOption Import `
-SourceUri $sourceURI `
-StorageAccountId $storageId
$snap = New-AzureRmSnapshot -AzureRmContext $ctx `
-ResourceGroupName $RGName `
-SnapshotName $snapName `
-Snapshot $snapshotConfig
#
# Create an image from the snapshot
#
write-host "Creating Image from Snapshot"
$imageConfig = New-AzureRmImageConfig -Location $location
Set-AzureRmImageOsDisk -Image $imageConfig `
-OsType Windows `
-OsState Generalized `
-SnapshotId $snap.Id
New-AzureRmImage -AzureRmContext $ctx `
-ResourceGroupName $RGName `
-ImageName $imageNameRegional `
-Image $imageConfig
#
# Cleanup snapshot
#
write-host "Deleting Snapshot"
Remove-AzureRmSnapshot -AzureRmContext $ctx -ResourceGroupName $RGName -SnapshotName $snapName -force
} -ArgumentList $ctx,$sub,$location,$sourceURI,$storageId,$RGName,$snapName,$imageNameRegional
}