-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate.ps1
More file actions
197 lines (168 loc) · 6.98 KB
/
Create.ps1
File metadata and controls
197 lines (168 loc) · 6.98 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
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
param(
[Parameter(Mandatory=$false)]
[switch]$Force
)
# Load JSON config
$config = Get-Content -Path "C:\Users\wimwauters\OneDrive - Microsoft\CUSTOMERS\EUROFINS\AzureBackupScript\config.json" | ConvertFrom-Json
# Generate VM list based on config parameters
$vmsToProcess = @()
for ($i = 0; $i -lt $config.vmCount; $i++) {
$vmNumber = $config.vmStartNumber + $i
$vmName = "$($config.vmNamePrefix)-$vmNumber"
$vmsToProcess += @{
vmName = $vmName
image = $config.vmConfig.image
adminUsername = $config.vmConfig.adminUsername
size = $config.vmConfig.size
}
}
Write-Host "=== VM Configuration ===" -ForegroundColor Cyan
Write-Host "Will process $($config.vmCount) VMs:" -ForegroundColor Yellow
foreach ($vm in $vmsToProcess) {
Write-Host " - $($vm.vmName)" -ForegroundColor Gray
}
Write-Host ""
# Login with Azure CLI
az login
# Set subscription context
az account set --subscription $config.subscriptionId
# CREATION LOGIC ONLY
Write-Host "=== CREATION MODE ===" -ForegroundColor Green
# Create Resource Group
$rgExists = az group show --name $config.resourceGroup 2>$null
if (-not $rgExists) {
Write-Host "Creating Resource Group..."
az group create --name $config.resourceGroup --location $config.location
} else {
Write-Host "Resource Group already exists, skipping creation..."
}
# Create VMs
$createdVMs = @()
$existingVMs = @()
foreach ($vm in $vmsToProcess) {
$vmExists = az vm show --name $vm.vmName --resource-group $config.resourceGroup 2>$null
if (-not $vmExists) {
Write-Host "Creating VM '$($vm.vmName)'..."
az vm create `
--resource-group $config.resourceGroup `
--name $vm.vmName `
--image $vm.image `
--admin-username $vm.adminUsername `
--size $vm.size `
--generate-ssh-keys
$createdVMs += $vm.vmName
Write-Host "VM '$($vm.vmName)' created successfully"
} else {
Write-Host "VM '$($vm.vmName)' already exists, skipping creation..."
$existingVMs += $vm.vmName
}
}
# Create Recovery Services Vault
$vaultExists = az backup vault show --name $config.vaultName --resource-group $config.resourceGroup 2>$null
if (-not $vaultExists) {
Write-Host "Creating Recovery Services Vault..."
az backup vault create `
--resource-group $config.resourceGroup `
--name $config.vaultName `
--location $config.location
} else {
Write-Host "Recovery Services Vault already exists, skipping creation..."
}
# Enable Backup Protection
Write-Host ""
Write-Host "=== CONFIGURING BACKUP PROTECTION ===" -ForegroundColor Cyan
$protectedItems = az backup item list --resource-group $config.resourceGroup --vault-name $config.vaultName --output json 2>$null
$protectedVMs = @()
$unprotectedVMs = @()
foreach ($vm in $vmsToProcess) {
Write-Host "Checking protection status for VM '$($vm.vmName)'..."
$vmAlreadyProtected = $false
if ($protectedItems -and $protectedItems -ne "[]") {
$items = $protectedItems | ConvertFrom-Json
$vmProtected = $items | Where-Object {
$_.properties.friendlyName -eq $vm.vmName -or
$_.properties.virtualMachineId -like "*/$($vm.vmName)"
}
if ($vmProtected) {
$vmAlreadyProtected = $true
$protectedVMs += $vm.vmName
Write-Host "VM '$($vm.vmName)' is already protected"
}
}
if (-not $vmAlreadyProtected) {
$unprotectedVMs += $vm.vmName
Write-Host "Enabling backup protection for VM '$($vm.vmName)'..."
az backup protection enable-for-vm `
--resource-group $config.resourceGroup `
--vault-name $config.vaultName `
--vm $vm.vmName `
--policy-name DefaultPolicy
if ($LASTEXITCODE -eq 0) {
Write-Host "Backup protection enabled for VM '$($vm.vmName)'"
} else {
Write-Host "Protection enable had warnings for VM '$($vm.vmName)'"
}
}
}
# Trigger Initial Backups
Write-Host ""
Write-Host "=== TRIGGERING INITIAL BACKUPS ===" -ForegroundColor Cyan
if ($unprotectedVMs.Count -gt 0) {
Write-Host "Waiting 30 seconds for backup protection to be configured..."
Start-Sleep -Seconds 30
}
$allBackupResults = @{}
foreach ($vm in $vmsToProcess) {
Write-Host ""
Write-Host "Processing backup for VM '$($vm.vmName)'..." -ForegroundColor Yellow
$backupSucceeded = $false
$maxAttempts = 3
for ($attempt = 1; $attempt -le $maxAttempts; $attempt++) {
Write-Host " Backup attempt $attempt of $maxAttempts for VM '$($vm.vmName)'..."
az backup protection backup-now `
--resource-group $config.resourceGroup `
--vault-name $config.vaultName `
--container-name "iaasvmcontainerv2;$($config.resourceGroup);$($vm.vmName)" `
--item-name $vm.vmName `
--retain-until $config.backupRetainUntil `
--output json
if ($LASTEXITCODE -eq 0) {
Write-Host " Backup job initiated successfully for VM '$($vm.vmName)'!" -ForegroundColor Green
$backupSucceeded = $true
$allBackupResults[$vm.vmName] = "Success"
break
} else {
Write-Host " Attempt $attempt failed for VM '$($vm.vmName)'" -ForegroundColor Yellow
if ($attempt -lt $maxAttempts) {
Write-Host " Waiting 15 seconds before retry..."
Start-Sleep -Seconds 15
}
}
}
if (-not $backupSucceeded) {
Write-Host " Could not trigger backup for VM '$($vm.vmName)' after $maxAttempts attempts" -ForegroundColor Red
$allBackupResults[$vm.vmName] = "Failed"
}
}
# Final Summary
Write-Host ""
Write-Host "=== BACKUP RESULTS SUMMARY ===" -ForegroundColor Cyan
$successfulBackups = ($allBackupResults.GetEnumerator() | Where-Object { $_.Value -eq "Success" }).Count
$failedBackups = ($allBackupResults.GetEnumerator() | Where-Object { $_.Value -eq "Failed" }).Count
Write-Host "Successful backups: $successfulBackups/$($vmsToProcess.Count)" -ForegroundColor Green
Write-Host "Failed backups: $failedBackups/$($vmsToProcess.Count)" -ForegroundColor $(if ($failedBackups -gt 0) { "Red" } else { "Green" })
Write-Host ""
Write-Host "=== SCRIPT COMPLETION SUMMARY ===" -ForegroundColor Green
Write-Host "Resource Group: $($config.resourceGroup) - $(if($rgExists) { 'Already existed' } else { 'Created' })"
Write-Host "Recovery Vault: $($config.vaultName) - $(if($vaultExists) { 'Already existed' } else { 'Created' })"
Write-Host ""
Write-Host "VMs Summary:"
foreach ($vm in $vmsToProcess) {
$status = if ($vm.vmName -in $createdVMs) { "Created" } else { "Already existed" }
$backupStatus = $allBackupResults[$vm.vmName]
$backupIcon = if ($backupStatus -eq "Success") { "Success" } else { "Warning" }
Write-Host " - $($vm.vmName): $status | Backup: $backupIcon $backupStatus"
}
Write-Host ""
Write-Host "Script Usage Examples:"
Write-Host " .\create_infra_v3.ps1 # Create resources"