-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_HyperVLab.ps1
More file actions
156 lines (110 loc) · 4.96 KB
/
init_HyperVLab.ps1
File metadata and controls
156 lines (110 loc) · 4.96 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
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
#>
$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
}
}
# Start 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
# 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
# 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