-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_HyperVLab.ps1
More file actions
288 lines (219 loc) · 10.7 KB
/
Copy pathinit_HyperVLab.ps1
File metadata and controls
288 lines (219 loc) · 10.7 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
Param (
[string[]]$CreateComputerName,
[string[]]$StartComputerName,
[string[]]$ConnectComputerName
)
<# Sample code to run this init script:
. .\init_HyperVLab.ps1
. .\init_HyperVLab.ps1 -Create BASE -Connect BASE
. .\init_HyperVLab.ps1 -Start BASE -Connect BASE
Stop-MyAzureLabResourceGroup
#>
$ErrorActionPreference = 'Stop'
if ($PSVersionTable.PSVersion.Major -lt 7) {
throw "This script needs pwsh 7"
}
. .\MyAzureLab.ps1
# Name of resource group and location
# Will be used by MyAzureLab commands (so these are "global" variables)
$resourceGroupName = 'HyperVLab'
$location = 'North Europe'
# Name and password of the initial account
$initUser = 'initialAdmin' # Will be used when creating the virtual maschines
$initPassword = 'initialP#ssw0rd' # Will be used when creating the virtual maschines and for the certificate
$initCredential = [PSCredential]::new($initUser, (ConvertTo-SecureString -String $initPassword -AsPlainText -Force))
# Show state of the resource group
Show-MyAzureLabResourceGroupInfo
# Configuration for the lab
$labConfig = @{
LabScript = @{
Name = 'TestingDbatools.ps1'
Content = Get-Content -Path ".\HyperVLab\TestingDbatools.ps1" -Raw
}
ISODownloads = @(
@{ Name = 'Windows2025' ; URL = $Env:MyWIN2025URL ; FileName = 'WindowsServer2025_x64_EN_Eval.iso' }
@{ Name = 'SQLServer2025' ; URL = $Env:MySQL2025URL ; FileName = 'SQLServer2025-x64-ENU.iso' }
@{ Name = 'SQLServer2022' ; URL = $Env:MySQL2022URL ; FileName = 'enu_sql_server_2022_developer_edition_x64_dvd_7cacf733.iso' }
@{ Name = 'SQLServer2019' ; URL = $Env:MySQL2019URL ; FileName = 'en_sql_server_2019_developer_x64_dvd_e5ade34a.iso' }
)
EnvironmentVariables = @{
MyStatusURL = $env:MyStatusURL
MyLabName = 'TestingDbatools'
MyLabNetworkBase = '192.168.3'
MyLabAdminUser = 'Admin'
MyLabAdminPassword = 'P@ssw0rd'
MyLabDomainName = 'ordix.local'
}
}
# Create VMs
if ($CreateComputerName) {
foreach ($computerName in $CreateComputerName) {
. .\HyperVLab\create_$computerName.ps1
}
}
# Start VMs
if ($StartComputerName) {
if ($StartComputerName -eq 'All') {
Start-MyAzureLabResourceGroup
} else {
Start-MyAzureLabResourceGroup -OnlyComputerName $StartComputerName
}
}
# Connect to VMs (always use the admin credential in this case)
if ($ConnectComputerName) {
Start-Sleep -Seconds 30
foreach ($computerName in $ConnectComputerName) {
Start-MyAzureLabRDP -ComputerName $computerName -Credential $initCredential
}
}
# Try to set TLS 1.2 fix to avoid errors with Azure modules (The SSL connection could not be established / An error occurred while sending the request)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Don't do anything else
break
# To suppress the warnings about breaking changes:
# Update-AzConfig -DisplayBreakingChangeWarning $false
# To suppress information about cheaper regions:
# Update-AzConfig -DisplayRegionIdentified $false
# Daily tasks if the lab is fully set up:
#########################################
Start-MyAzureLabResourceGroup
Stop-MyAzureLabResourceGroup
Start-MyAzureLabRDP -ComputerName BASE -Credential $initCredential
$psSession = New-MyAzureLabSession -ComputerName BASE -Credential $initCredential
$psSession | Remove-PSSession
Invoke-MyAzureLabCommand -ComputerName BASE -Credential $initCredential -ArgumentList $labConfig -ScriptBlock {
param($config)
foreach ($envVar in $config.EnvironmentVariables.GetEnumerator()) {
[Environment]::SetEnvironmentVariable($envVar.Key, $envVar.Value, 'Machine')
}
}
init.ps1:
Import-Module -Name AutomatedLab
Import-Lab -Name $env:MyLabName -NoValidation
Start-LabVM -ComputerName DC -Wait
Start-Sleep -Seconds 30
Start-LabVM -All -Wait
Start-Sleep -Seconds 60
mstsc /v:$env:MyLabNetworkBase.20
$RunKeyPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Run'
$ValueName = 'InitLabScript'
$ScriptPath = 'C:\LabScripts\init.ps1'
$PowerShell = "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe"
$CommandLine = "`"$PowerShell`" -NoProfile -ExecutionPolicy Bypass -File `"$ScriptPath`""
$current = Get-ItemProperty -Path $RunKeyPath -Name $ValueName -ErrorAction SilentlyContinue
if ($null -eq $current) {
New-ItemProperty -Path $RunKeyPath -Name $ValueName -PropertyType String -Value $CommandLine -Force | Out-Null
Write-Host "Created HKCU Run entry '$ValueName' -> $CommandLine"
} elseif ($current.$ValueName -ne $CommandLine) {
Set-ItemProperty -Path $RunKeyPath -Name $ValueName -Value $CommandLine -Force
Write-Host "Updated HKCU Run entry '$ValueName' -> $CommandLine"
} else {
Write-Host "HKCU Run entry '$ValueName' already set. No changes made."
}
Invoke-Command -ComputerName DC -ScriptBlock {
$dnsRoot = (Get-ADDomain).DNSRoot
Add-DnsServerResourceRecordCName -ComputerName dc -ZoneName $dnsRoot -HostNameAlias fci01.$dnsRoot -Name app01
Add-DnsServerResourceRecordCName -ComputerName dc -ZoneName $dnsRoot -HostNameAlias sql03.$dnsRoot -Name app02
}
# Next steps for improving the lab:
# * Start VMs when BASE starts
# * RDP into ADMIN01 on login to BASE
# * Include "reg add "HKLM\SOFTWARE\Microsoft\Terminal Server Client" /v DisableHardwareAcceleration /t REG_DWORD /d 1 /f" as it helps
# * Chang default keyboard layout to DE in ADMIN01
# Tasks to create and remove virtual maschine:
##############################################
. .\HyperVLab\create_BASE.ps1
Start-MyAzureLabRDP -ComputerName BASE -Credential $initCredential
# Tasks to resize the virtual maschine:
#######################################
$vm = Get-AzVM -ResourceGroupName $resourceGroupName -Name BASE_VM
$result = $vm | Stop-AzVM -Force
$result.Status # Should be 'Succeeded'
$vm.HardwareProfile.VmSize = 'Standard_E4s_v6'
$result = Update-AzVM -ResourceGroupName $resourceGroupName -VM $vm
$result.IsSuccessStatusCode # Should be True
$result = $vm | Start-AzVM
$result.Status # Should be 'Succeeded'
# To remove all virtual maschines:
##################################
Remove-MyAzureLabVM -All -Verbose
# The path where the logging is saved:
######################################
Get-PSFConfigvalue -FullName PSFramework.Logging.FileSystem.LogPath
# Adding Azure SQL Database to the lab:
######################################
# ------------------------------
# Inputs you set
# ------------------------------
$LogicalServerName = "sqllab$(Get-Random)" # must be globally unique
$DatabaseName = "sqllabdb"
$ApplyFreeOffer = $true # try to use free tier
$AutoPauseMinutes = 15 # minimum supported, keeps cost down
$MinVCore = 0.5 # serverless lower bound for GP
$MaxVCore = 1 # tiny top end for tests
# ------------------------------
# Create (or reuse) logical server
# ------------------------------
$sqlServer = Get-AzSqlServer -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -ErrorAction SilentlyContinue
if (-not $sqlServer) {
$sqlServer = New-AzSqlServer -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -Location $location -SqlAdministratorCredentials $initCredential
}
# ------------------------------
# Lock down firewall to current public IP only
# (change to your office IP if needed)
# ------------------------------
$fwRule = Get-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -FirewallRuleName AllowHome -ErrorAction SilentlyContinue
if ($fwRule) {
$null = Set-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -FirewallRuleName AllowHome -StartIpAddress $homeIP -EndIpAddress $homeIP
} else {
$null = New-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -FirewallRuleName AllowHome -StartIpAddress $homeIP -EndIpAddress $homeIP
}
# ------------------------------
# Create the database (serverless GP)
# Try free offer first; fall back to paid serverless if not applicable.
# ------------------------------
$commonDbParams = @{
ResourceGroupName = $resourceGroupName
ServerName = $LogicalServerName
DatabaseName = $DatabaseName
Edition = "GeneralPurpose"
ComputeModel = "Serverless" # vCore-based serverless
ComputeGeneration = "Gen5"
VCore = [int][math]::Ceiling($MaxVCore) # required by cmdlet; cap at max
AutoPauseDelayInMinutes = $AutoPauseMinutes
MinimumCapacity = $MinVCore
MaxSizeBytes = 32GB # stays within free limit / small storage
BackupStorageRedundancy = "Local" # cheapest for a test lab
}
$db = Get-AzSqlDatabase -ResourceGroupName $ResourceGroupName -ServerName $LogicalServerName -DatabaseName $DatabaseName -ErrorAction SilentlyContinue
if (-not $db) {
if ($ApplyFreeOffer) {
try {
$db = New-AzSqlDatabase @commonDbParams -UseFreeLimit -FreeLimitExhaustionBehavior "Pause" -ErrorAction Stop
} catch {
Write-Warning "Free offer not applied (maybe quota/region/subscription). Falling back to paid serverless."
$db = New-AzSqlDatabase @commonDbParams
}
} else {
$db = New-AzSqlDatabase @commonDbParams
}
}
# Output essentials
$sqlServer | Select-Object ServerName, FullyQualifiedDomainName, Location
$db | Select-Object DatabaseName, Edition, Status, CurrentServiceObjectiveName
Remove-AzSqlDatabase -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -DatabaseName $DatabaseName
Remove-AzSqlServerFirewallRule -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName -FirewallRuleName AllowHome
Remove-AzSqlServer -ResourceGroupName $resourceGroupName -ServerName $LogicalServerName
# The following commands are only used for initial setup or final destruction:
##############################################################################
# Creating resource group
$null = New-AzResourceGroup -Name $resourceGroupName -Location $location
# $null = Remove-AzResourceGroup -Name $resourceGroupName -Force
# Get-AzKeyVault -InRemovedState -WarningAction SilentlyContinue | ForEach-Object -Process { Remove-AzKeyVault -VaultName $_.VaultName -Location $_.Location -InRemovedState -Force }
# Creating key vault and certificate
New-MyAzureLabKeyVault
# Get-AzKeyVault -ResourceGroupName $resourceGroupName | Remove-AzKeyVault -Force
# Creating network and security group
New-MyAzureLabNetwork -HomeIP $homeIP
# Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName | Remove-AzVirtualNetwork -Force
# Get-AzNetworkSecurityGroup -ResourceGroupName $resourceGroupName | Remove-AzNetworkSecurityGroup -Force