This repository was archived by the owner on May 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2_GoldenImage_creation_steps.ps1
More file actions
84 lines (75 loc) · 4.3 KB
/
Copy path2_GoldenImage_creation_steps.ps1
File metadata and controls
84 lines (75 loc) · 4.3 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
Throw "This is not a robust file"
$oldVerbose = $VerbosePreference
$VerbosePreference = "continue"
#Region Values declaration
Import-Module Az.DesktopVirtualization
$resourceLocation = "NorthEurope"
$vmResourceGroupName = 'rg-vms'
$giResourceGroupName = 'rg-GoldenImage'
$vmName = Read-host -Prompt "Please Provide VM Name"
$date = Get-date -UFormat "%d_%m_%Y"
$snapshotName = "snap-" + $vmName + "_" + $date
$imageName = "img-" + $vmName + "_" + $date
#endregion
#Region Create Snapshot
Write-Verbose "Looking for VM:'$vmName'"
$vm = Get-AzVM -ResourceGroupName $vmResourceGroupName -Name $vmName
$diskID = $vm.StorageProfile.OsDisk.ManagedDisk.Id
Write-Verbose "Creating Snapshoot config for '$vmName' OSDisk '$diskID'"
$snapshot = New-AzSnapshotConfig -SourceUri $diskID -Location $resourceLocation -CreateOption copy
$snapshotTest = Get-AzSnapshot -SnapshotName $snapshotName -ResourceGroupName $vmResourceGroupName -ErrorAction SilentlyContinue
if ($null -eq $snapshotTest) {
Write-Verbose "Creating new snapshot for VM: '$vmName', SnapshootName: '$snapshotName'"
New-AzSnapshot -Snapshot $snapshot -SnapshotName $snapshotName -ResourceGroupName $vmResourceGroupName
}
else {
Write-Verbose "Snapshot for VM: '$vmName', SnapshootName: '$snapshotName' already created."
}
#endregion
#region Create Image from VM
Write-Verbose "Looking for VM:'$vmName'"
$vm = Get-AzVM -ResourceGroupName $vmResourceGroupName -Name $vmName
$vmStatus = (Get-AZVM -ResourceGroupName $vmResourceGroupName -Name $vmName -status).statuses[1].DisplayStatus
if ($vmStatus -ne 'VM Deallocated') {
Write-Verbose "Stopping vm:'$vmName'"
Stop-AzVM -ResourceGroupName $vmResourceGroupName -Name $vmName -Force
}
else {
Write-Verbose "VM:'$vmName' deallocated."
}
$diskID = $vm.StorageProfile.OsDisk.ManagedDisk.Id
$imageTest = Get-AZimage -ResourceGroupName $giResourceGroupName -ImageName $imageName -ErrorAction SilentlyContinue
if ($null -eq $imageTest) {
Write-Verbose "Creating image config for vm: '$vmName', OSDisk '$diskID'"
$imageConfig = New-AzImageConfig -Location $resourceLocation
$imageConfig = Set-AzImageOsDisk -Image $imageConfig -OsState Generalized -OsType Windows -ManagedDiskId $diskID
Write-Verbose "Creating new image for VM: '$vmName', imageName: '$imageName'"
New-AzImage -ImageName $imageName -ResourceGroupName $giResourceGroupName -Image $imageConfig
}
else {
Write-Verbose "Image from VM: '$vmName', ImageName: ''$imageName' already created."
}
#endregion
#region Create Shared Image Gallery
$galleryName = Read-host -Prompt "Please provide SharedImageGallery name"
$galleryTest = Get-AZGallery -ResourceGroupName $giResourceGroupName -GalleryName $galleryName -ErrorAction SilentlyContinue
if ($null -eq $galleryTest) {
Write-Verbose "Creating image gallery: '$galleryName'"
New-AzGallery -GalleryName $galleryName -ResourceGroupName $giResourceGroupName -Location $resourceLocation
}
else {
Write-Verbose "Gallery '$galleryName' already created."
}
#endregion
#region Upload Image to SIG
Write-Verbose "Looking for SharedImageGallery: '$galleryName'"
$gallery = Get-AzGallery -Name $galleryName -ResourceGroupName $giResourceGroupName
$imageID = (Get-AzImage -ResourceGroupName $giResourceGroupName -ImageName $imageName).id
Write-Verbose "Creating ImageDefinition"
$imageDefinition = New-AzGalleryImageDefinition -GalleryName $gallery.Name -ResourceGroupName $giResourceGroupName -Location $resourceLocation -Name 'Windows10-MU-AcrobatDC' -OsState Generalized -OsType Windows -Publisher 'Azureblog' -Offer 'Azureblog' -Sku '1'
$region1 = @{Name='northeurope';ReplicaCount=1}
$targetRegions = @($region1)
Write-Verbose "Creating ImageVersion"
New-AzGalleryImageVersion -GalleryImageDefinitionName $imageDefinition.Name -GalleryImageVersionName '1.0.1' -GalleryName $gallery.Name -ResourceGroupName $giResourceGroupName -Location $resourceLocation -TargetRegion $targetRegions -SourceImageId $imageID -PublishingProfileEndOfLifeDate '2021-10-06' -asJob
#endregion
$VerbosePreference = $oldVerbose