-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_SQLServerLab.ps1
More file actions
204 lines (139 loc) · 7.07 KB
/
Copy pathinit_SQLServerLab.ps1
File metadata and controls
204 lines (139 loc) · 7.07 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
Param (
[string[]]$StartComputerName,
[string[]]$ConnectComputerName
)
<# Sample code to run this init script:
. .\init_SQLServerLab.ps1
. .\init_SQLServerLab.ps1 -Start DC, CLIENT -Connect CLIENT
. .\init_SQLServerLab.ps1 -Start All -Connect CLIENT
#>
$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 = 'SQLServerLab'
$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
# Read the configuration
. .\SQLServerLab\set_vm_config.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 $credentials.Admin
}
}
# 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 CLIENT -Credential $credentials.Admin
Start-MyAzureLabRDP -ComputerName CLIENT -Credential $initCredential
$psSession = New-MyAzureLabSession -ComputerName CLIENT -Credential $initCredential
$psSession | Remove-PSSession
# Tasks to create and remove virtual maschines:
###############################################
# Read the configuration
. .\SQLServerLab\set_vm_config.ps1
# Create the VMs
. .\SQLServerLab\create_VMs.ps1
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
##########
Write-PSFMessage -Level Host -Message 'Part 5: Connecting to client'
##########
# Just once:
# reg add "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client" /v "AuthenticationLevelOverride" /t "REG_DWORD" /d 0 /f
Start-MyAzureLabRDP -ComputerName CLIENT -Credential $credentials.SQLAdmin
Start-MyAzureLabRDP -ComputerName DC -Credential $credentials.Admin
# Testing dbatools
##################
Start-MyAzureLabRDP -ComputerName SQL01 -Credential $credentials.Admin
# powershell as administrator on CLIENT:
Install-Module -Name Pester -Force -SkipPublisherCheck
Install-Module -Name PSScriptAnalyzer -Force -SkipPublisherCheck -MaximumVersion 1.18.2
# powershell on CLIENT:
$null = New-Item -Path C:\GitHub -ItemType Directory
Push-Location -Path C:\GitHub
git clone --quiet https://github.com/dataplat/dbatools.git
git clone --quiet https://github.com/dataplat/appveyor-lab.git
git clone --quiet https://github.com/andreasjordan/testing-dbatools.git
Pop-Location
$ErrorActionPreference = 'Stop'
Import-Module -Name C:\GitHub\dataplat\dbatools
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues['*-Dba*:Confirm'] = $false
Set-DbatoolsConfig -FullName sql.connection.trustcert -Value $true
$config = Get-Content -Path C:\Deployment\config.txt | ConvertFrom-Json
$version = 2022
# Get-DbaInstalledPatch.Tests.ps1 braucht einen installiertes CU
Get-ChildItem -Path \\fs\Software\SQLServer\CU\SQLServer$version* | Sort-Object Name | Select-Object -Last 1 | Remove-Item
$instanceParams = @{
Feature = 'Engine'
Version = $version
Configuration = @{
SqlMaxMemory = '2048'
}
AuthenticationMode = 'Mixed'
IFI = $true
Path = '\\fs\Software\SQLServer\ISO'
UpdateSourcePath = '\\fs\Software\SQLServer\CU'
}
$null = Install-DbaInstance @instanceParams -SqlInstance localhost -ProductID '11111-00000-00000-00000-00000'
$null = Install-DbaInstance @instanceParams -SqlInstance localhost\dbatools2
$null = Install-DbaInstance @instanceParams -SqlInstance localhost\dbatools3
# $updateResult = Update-DbaInstance -ComputerName localhost -Path \\fs\Software\SQLServer\CU
# Restart
$sqlInstance = 'localhost', 'localhost\dbatools2', 'localhost\dbatools3'
$null = Set-DbaLogin -SqlInstance $sqlInstance -Login sa -SecurePassword (ConvertTo-SecureString -String 'Passw0rd!' -AsPlainText -Force)
$null = Set-DbaSpConfigure -SqlInstance $sqlInstance -Name IsSqlClrEnabled -Value 1
$null = Set-DbaSpConfigure -SqlInstance $sqlInstance -Name ClrStrictSecurity -Value 0
$null = Set-DbaNetworkConfiguration -SqlInstance $sqlInstance -EnableProtocol NamedPipes -RestartService
$null = Set-DbaNetworkConfiguration -SqlInstance $sqlInstance[1] -StaticPortForIPAll 14333 -RestartService
$null = Enable-DbaAgHadr -SqlInstance $sqlInstance[2] -Force
$server = Connect-DbaInstance -SqlInstance $sqlInstance[2]
$server.Query("IF NOT EXISTS (select * from sys.symmetric_keys where name like '%DatabaseMasterKey%') CREATE MASTER KEY ENCRYPTION BY PASSWORD = '<StrongPassword>'")
$server.Query("IF EXISTS ( SELECT * FROM sys.tcp_endpoints WHERE name = 'End_Mirroring') DROP ENDPOINT endpoint_mirroring")
$server.Query("CREATE CERTIFICATE dbatoolsci_AGCert WITH SUBJECT = 'AG Certificate'")
# 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