Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ $vmPostfix = $vmPostfix -replace "_", "-"
$vmName = "VM-$vmPostfix"
$TeamName = $me.ShortName
$TeamName = $TeamName.ToUpper()
$legacySQLName = "legacySQL2016"

# feed the effective resource group back to the console
@{"HackboxCredential" = @{ name = "ResourceGroupName" ; value = $effectiveResourceGroup; note = "The name of the resource group where lab resources are deployed" }}
@{"HackboxCredential" = @{ name = "Team VM Name" ; value = $vmName; note = "The name of the Team VM" }}
@{"HackboxCredential" = @{ name = "Team / Link Name" ; value = $TeamName; note = "Name of the Team (Prefix for DBs and to use as Link Name for SQLMI)" }}

$legacySQLName = "legacySQL2016"

$storageAccountName = Get-AzStorageAccount -ResourceGroupName $sharedResourceGroupName -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty StorageAccountName
$managedInstanceFQDN = Get-AzSqlInstance -ResourceGroupName $sharedResourceGroupName -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullyQualifiedDomainName
Expand Down Expand Up @@ -121,8 +126,3 @@ Write-Host "[$SubscriptionId] Lab deployment completed successfully"
Write-Host "[$SubscriptionId] Lab Resource Group: $effectiveResourceGroup"
Write-Host "[$SubscriptionId] Shared Resource Group: $sharedResourceGroupName"
Write-Host "[$SubscriptionId] Result: $($result)"

# feed the effective resource group back to the console
@{"HackboxCredential" = @{ name = "ResourceGroupName" ; value = $effectiveResourceGroup; note = "The name of the resource group where lab resources are deployed" }}
@{"HackboxCredential" = @{ name = "Team VM Name" ; value = $vmName; note = "The name of the Team VM" }}
@{"HackboxCredential" = @{ name = "Team / Link Name" ; value = $TeamName; note = "Name of the Team (Prefix for DBs and to use as Link Name for SQLMI)" }}
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ try {
#Write-Host "Skript erfolgreich auf SQL MI ausgeführt." -ForegroundColor Green
}
catch {
Write-Error "Fehler bei der Ausführung: $_"
$ErrorString = $_ | format-list -force | Out-String
Write-Error "ERR: $ErrorString"
}
finally {
if ($null -ne $serverConnection -and $serverConnection.IsOpen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ try {
}
catch {
Write-Host "An error occurred while configuring the team wallpaper: $_"
$ErrorString = $_ | format-list -force | Out-String
Write-Error "ERR: $ErrorString"
}

Stop-Transcript
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,20 @@ WHERE

DECLARE @DBName VARCHAR(255)
DECLARE @SQLCmd NVARCHAR(MAX)
DECLARE DB_Crs CURSOR READ_ONLY FORWARD_ONLY FOR SELECT name FROM sys.databases WHERE name = '$($TeamName)_TenantDataDB' AND state_desc = 'ONLINE'
OPEN DB_Crs
FETCH NEXT FROM DB_Crs INTO @DBName
SET @DBName = '$($TeamName)_TenantDataDB'

WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQLCmd = 'USE [' + @DBName + ']
ALTER ASSEMBLY CLRUFDS WITH PERMISSION_SET = UNSAFE;
ALTER ASSEMBLY Database1 WITH PERMISSION_SET = UNSAFE;
'
--PRINT @SQLCmd
EXEC (@SQLCmd)
SET @SQLCmd = 'USE [' + @DBName + '];
ALTER ASSEMBLY CLRUFDS WITH PERMISSION_SET = UNSAFE;
ALTER ASSEMBLY Database1 WITH PERMISSION_SET = UNSAFE;
'
--PRINT @SQLCmd
EXEC (@SQLCmd)

SET @SQLCmd = 'ALTER DATABASE [' + @DBName + ']
ADD LOG FILE (NAME = N''' + @DBName + '_Log2'', FILENAME = N''' + @DefaultLogPath + @DBName + '_Log2.ldf'');
'
--PRINT @SQLCmd
EXEC (@SQLCmd)

FETCH NEXT FROM DB_Crs INTO @DBName
END
CLOSE DB_Crs
DEALLOCATE DB_Crs
SET @SQLCmd = 'ALTER DATABASE [' + @DBName + ']
ADD LOG FILE (NAME = N''' + @DBName + '_Log2'', FILENAME = N''' + @DefaultLogPath + @DBName + '_Log2.ldf'');
'
--PRINT @SQLCmd
EXEC (@SQLCmd)
"@

$connectionString = "Data Source=$ServerInstance;Initial Catalog=master;TrustServerCertificate=True;"
Expand All @@ -66,12 +57,15 @@ try {
$command.CommandTimeout = $QueryTimeout

$command.CommandText = $ConfigureSql
$result = $command.ExecuteReader()
$table = New-Object System.Data.DataTable
$table.Load($result)
$result = $command.ExecuteNonQuery()
#$result = $command.ExecuteReader()
#$table = New-Object System.Data.DataTable
#$table.Load($result)
}
catch {
Write-Error "Fehler bei der Ausführung: $_"
Write-Host $result
$ErrorString = $_ | format-list -force | Out-String
Write-Error "ERR: $ErrorString"
}
finally {
if ($null -ne $serverConnection -and $serverConnection.IsOpen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ $Connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$SQLPwd.MakeReadOnly()
$cred = New-Object System.Data.SqlClient.SqlCredential($sqlusername,$SQLPwd)
$Connection.credential = $cred
#$Connection.open()

#$command = New-Object system.Data.SqlClient.SqlCommand($Connection)
#$command.Connection = $Connection
#$command.CommandTimeout = $QueryTimeout

#$command.CommandText = $ConfigureSql
#$result = $command.ExecuteReader()
#$table = New-Object System.Data.DataTable
#$table.Load($result)

# 5. SMO-Serververbindung initialisieren
$serverConnection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($Connection)
$server = New-Object Microsoft.SqlServer.Management.Smo.Server($serverConnection)

Expand All @@ -55,7 +44,8 @@ try {
#Write-Host "Skript erfolgreich auf SQL MI ausgeführt." -ForegroundColor Green
}
catch {
Write-Error "Fehler bei der Ausführung: $_"
$ErrorString = $_ | format-list -force | Out-String
Write-Error "ERR: $ErrorString"
}
finally {
if ($null -ne $serverConnection -and $serverConnection.IsOpen) {
Expand All @@ -72,11 +62,13 @@ Try {
Write-Host "Installing and configuring Windows Failover Cluster..." -ForegroundColor Green
Install-WindowsFeature -Name Failover-Clustering -IncludeManagementTools -IncludeAllSubFeature
$clus = New-Cluster -Name "CLU01" -AdministrativeAccessPoint None -Verbose -Force
Enable-SqlAlwaysOn -Path SQLSERVER:\SQL\legacysql2016\default -Force
Enable-SqlAlwaysOn -Path "SQLSERVER:\SQL\$($ServerInstance)\default" -Force
Restart-Service -Name "MSSQLSERVER" -Force
}
Catch {
Write-Host "Error configuring SQL legacy Instance: $_"
Write-Host "Error configuring SQL legacy Instance."
$ErrorString = $_ | format-list -force | Out-String
Write-Error "ERR: $ErrorString"
}

Stop-Transcript
Original file line number Diff line number Diff line change
Expand Up @@ -585,15 +585,6 @@ END;
# Main
# ---------------------------------------------------------------------------

if (-not (Test-Path -LiteralPath $DownloadDirectory))
{
New-Item `
-ItemType Directory `
-Path $DownloadDirectory `
-Force |
Out-Null
}

Write-Host "Testing SQL Server connection to [$ServerInstance]..."

$serverInformation = Invoke-DatabaseQuery -Query @"
Expand Down Expand Up @@ -621,75 +612,6 @@ foreach ($backupName in $backupNames)
{
$fileName = "$backupName.bak"
$destination = Join-Path $DownloadDirectory $fileName
$downloadUri = Get-BackupDownloadUri `
-BaseUri $BackupBaseUri `
-FileName $fileName

if ((Test-Path -LiteralPath $destination) -and
-not $ForceDownload.IsPresent)
{
Write-Host "Using existing backup: $destination"
}
else
{
Write-Host "Downloading [$fileName] from $downloadUri ..."

$temporaryFile = "$destination.download"

Remove-Item `
-LiteralPath $temporaryFile `
-Force `
-ErrorAction SilentlyContinue

try
{
$invokeWebRequestParameters = @{
Uri = $downloadUri
OutFile = $temporaryFile
ErrorAction = "Stop"
}

# UseBasicParsing exists in Windows PowerShell 5.1 but not in
# newer editions in the same form.
if ($PSVersionTable.PSEdition -eq "Desktop")
{
$invokeWebRequestParameters.UseBasicParsing = $true
}

$lastProgressPreference = $ProgressPreference
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest @invokeWebRequestParameters
$ProgressPreference = $lastProgressPreference

if (-not (Test-Path -LiteralPath $temporaryFile))
{
throw "The downloaded file was not created."
}

if ((Get-Item -LiteralPath $temporaryFile).Length -eq 0)
{
throw "The downloaded file is empty."
}

Move-Item `
-LiteralPath $temporaryFile `
-Destination $destination `
-Force
}
catch
{
Remove-Item `
-LiteralPath $temporaryFile `
-Force `
-ErrorAction SilentlyContinue

throw "Download of [$fileName] failed: $($_.Exception.Message)"
}

Write-Host "Downloaded [$fileName] to [$destination]." `
-ForegroundColor Green
}

$downloadedBackups[$backupName] = $destination
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ Write-Host "[$SubscriptionId] Result: $($result)"
$managedInstance = Get-AzSqlInstance -ResourceGroupName $sharedResourceGroup -ErrorAction SilentlyContinue | Select-Object -First 1
$managedInstanceName = $managedInstance.ManagedInstanceName
$managedInstanceFQDN = $managedInstance.FullyQualifiedDomainName

@{"HackboxCredential" = @{name = 'Legacy SQL Server Name'; value = $legacySQLName; note = 'Name of legacy SQL Server'}}
@{"HackboxCredential" = @{name = 'SQLMI FQDN'; value = $managedInstanceFQDN; note = 'FQDN of SQLMI'}}
@{"HackboxCredential" = @{name = 'Arc SQL Server Name'; value = $arcSQLName; note = 'Name of new SQL Server'}}

@{"HackboxCredential" = @{name = 'VM User Name'; value = $adminUsername; note = 'Username to connect for every VM'}}
@{"HackboxCredential" = @{name = 'VM User Password'; value = $adminPassword; note = 'Password to connect to every VM'}}
@{"HackboxCredential" = @{name = 'SQLMI User Name'; value = $sqlMiAdminUsername; note = 'Username to connect to SQLMI'}}
@{"HackboxCredential" = @{name = 'SQLMI User Password'; value = $sqlMiAdminPassword; note = 'Password to connect to SQLMI'}}

[string]$managedInstanceResourceId = $managedInstance.Id
#$managedInstancePrincipalId = $managedInstance.Identity.PrincipalId

Expand All @@ -123,9 +133,9 @@ try {
}
catch {
Write-Host "Failed to grant 'Directory Readers' to Managed Identity of $managedInstanceFQDN." -ForegroundColor Red
$ErrorString = $_ | format-list -force | Out-String
Write-Host "ERR: $ErrorString" -ForegroundColor Red
Write-Host $return
$ErrorString = $_ | format-list -force | Out-String
Write-Error "ERR: $ErrorString"
}

Start-Sleep -Seconds 30
Expand Down Expand Up @@ -160,7 +170,7 @@ try {
catch {
Write-Host "Failed to set Entra ID Admin on $managedInstanceFQDN." -ForegroundColor Red
$ErrorString = $_ | format-list -force | Out-String
Write-Host "ERR: $ErrorString" -ForegroundColor Red
Write-Error "ERR: $ErrorString"
}

$timeoutSeconds = 120
Expand Down Expand Up @@ -194,12 +204,3 @@ if (-not $found)
{
throw "Expected Entra ID Admin was not configured within 2 minutes."
}

@{"HackboxCredential" = @{name = 'Legacy SQL Server Name'; value = $legacySQLName; note = 'Name of legacy SQL Server'}}
@{"HackboxCredential" = @{name = 'SQLMI FQDN'; value = $managedInstanceFQDN; note = 'FQDN of SQLMI'}}
@{"HackboxCredential" = @{name = 'Arc SQL Server Name'; value = $arcSQLName; note = 'Name of new SQL Server'}}

@{"HackboxCredential" = @{name = 'VM User Name'; value = $adminUsername; note = 'Username to connect for every VM'}}
@{"HackboxCredential" = @{name = 'VM User Password'; value = $adminPassword; note = 'Password to connect to every VM'}}
@{"HackboxCredential" = @{name = 'SQLMI User Name'; value = $sqlMiAdminUsername; note = 'Username to connect to SQLMI'}}
@{"HackboxCredential" = @{name = 'SQLMI User Password'; value = $sqlMiAdminPassword; note = 'Password to connect to SQLMI'}}