-
Notifications
You must be signed in to change notification settings - Fork 0
/
takeBaseSnapshots.ps1
86 lines (68 loc) · 3.27 KB
/
takeBaseSnapshots.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
<#
.Description
Take a base snapshot of a set of virtual machine's OS and data disks
Then copies them to another subscription/region/resource group
.Example
./takeBaseSnapshots.ps1 -CsvRelativePath relative/path/to/you/CSV
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)][string]$CsvRelativePath
)
## Internal Functions
. (Join-Path $PSScriptRoot functions.ps1)
$csvFilePath = Join-Path -Path $PSScriptRoot -ChildPath $CsvRelativePath
$csvFileResult = Test-Path $csvFilePath
if(!$csvFileResult)
{
Write-Host "Path ${csvFilePath} not found!"
exit
}
$sourceSnapshotsBicepPath = Join-Path -Path $PSScriptRoot -ChildPath "/bicep/sourceSnapshot.bicep"
$sourceSnapshotsBicepResult = Test-Path $sourceSnapshotsBicepPath
if(!$sourceSnapshotsBicepResult)
{
Write-Host "Path ${sourceSnapshotsBicepPath} not found!"
exit
}
$targetSnapshotsBicepPath = Join-Path -Path $PSScriptRoot -ChildPath "/bicep/targetSnapshot.bicep"
$targetSnapshotsBicepResult = Test-Path $targetSnapshotsBicepPath
if(!$targetSnapshotsBicepResult)
{
Write-Host "Path ${targetSnapshotsBicepPath} not found!"
exit
}
#Get the function definition as a string
$converToBicepArray = ${function:ConvertTo-BicepArray}.ToString()
$csv = Import-Csv -Path $csvFilePath
$csv | ForEach-Object -ThrottleLimit $csv.Count -Parallel {
$SourceResourceGroupName = $_.src_rg
$SourceVMName = $_.src_vm_name
$SourceSubscriptionName = $_.src_subscription
$SourceSnapshotResourceGroupName = $_.src_snapshot_rg
$SourceLocation = $_.src_region
$TargetResourceGroupName = $_.tgt_vm_rg
$TargetSubscriptionName = $_.tgt_subscription
$TargetLocation = $_.tgt_region
$ResourceBaseName = "${SourceVMName}-base"
$sourceSnapshotDeploymentName = "source-${SourceVMName}-snapshots-base"
$targetSnapshotDeploymentName = "target-${SourceVMName}-snapshots-base"
# define the functions inside of the thread
${function:ConvertTo-BicepArray} = $using:converToBicepArray
az account set --subscription $SourceSubscriptionName
az deployment group create --resource-group $SourceResourceGroupName --name $sourceSnapshotDeploymentName `
--template-file $using:sourceSnapshotsBicepPath --parameters location=$SourceLocation sourceVMName=$SourceVMName `
resourceGroupName=$SourceSnapshotResourceGroupName baseSnapshotName=$ResourceBaseName
$sourceOSSnapshotID = az deployment group show --resource-group $SourceResourceGroupName --name $sourceSnapshotDeploymentName `
--query properties.outputs.sourceOSSnapshotID.value
$sourceDataSnapshotIDs = "["
$dataSnapshotIds = az deployment group show --resource-group $SourceResourceGroupName --name $sourceSnapshotDeploymentName `
--query properties.outputs.sourceDataSnapshotIDs.value
$sourceDataSnapshotIDs = ConvertTo-BicepArray -ArrayToConvert $dataSnapshotIds
Write-Output "Sleeping for 120 seconds"
Start-Sleep 120
az account set --subscription $TargetSubscriptionName
az deployment group create --resource-group $TargetResourceGroupName --name $targetSnapshotDeploymentName `
--template-file $using:targetSnapshotsBicepPath --parameters location=$TargetLocation osSnapshotID=$sourceOSSnapshotID `
dataSnapshotIDs=$sourceDataSnapshotIDs baseSnapshotName=$ResourceBaseName
}