Skip to content

Commit fc239ab

Browse files
authored
Ensure backwards-compatible parameters (AutomatedLab#1782)
1 parent 49deb91 commit fc239ab

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- The module has not been updated since 2019
2525
- Fixed issue with case-sensitive Linux OS name matching (#1766)
2626
- Added filtering to Install-LabSshKnownHost in an attempt to speed up the function
27+
- Fix issue with incompatible parameters that were introduced to CreateDscSqlDatabase.ps1 in 5.59
2728

2829
## 5.59.0 (2025-08-18)
2930

LabSources/PostInstallationActivities/SetupDscPullServer/CreateDscSqlDatabase.ps1

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
param (
2-
[Parameter(Mandatory)]
3-
[string]
4-
$DomainAndComputerName,
2+
[Parameter(Mandatory)]
3+
[string]
4+
$DomainAndComputerName,
55

6-
[Parameter(Mandatory)]
7-
[ValidateSet('Mandatory', 'Optional', 'Strict')]
8-
[string]
9-
$Encrypt,
6+
[bool]
7+
$UseNewFeature = $false,
108

11-
[string]
12-
$ServerInstance,
9+
[ValidateSet('Mandatory', 'Optional', 'Strict')]
10+
[string]
11+
$Encrypt,
1312

14-
[bool]
15-
$UseNewFeature = $false
13+
[string]
14+
$ServerInstance = 'localhost'
1615
)
1716

17+
$sqlCmdParam = @{
18+
ServerInstance = $ServerInstance
19+
}
20+
21+
if ((Get-Command Invoke-SqlCmd).Parameters.Keys -contains 'Encrypt' -and -not [string]::IsNullOrEmpty($Encrypt)) {
22+
$sqlCmdParam.Encrypt = $Encrypt
23+
}
24+
1825
[string]$createDbQuery = @'
1926
USE [master]
2027
GO
@@ -904,10 +911,9 @@ ALTER DATABASE [DSC] SET READ_WRITE
904911
GO
905912
'@
906913

907-
if (-not $UseNewFeature)
908-
{
909-
$createDbQuery = $createDbQuery.Replace('WITH CATALOG_COLLATION = DATABASE_DEFAULT','')
910-
$createDbQuery = $createDbQuery.Replace(', OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF','')
914+
if (-not $UseNewFeature) {
915+
$createDbQuery = $createDbQuery.Replace('WITH CATALOG_COLLATION = DATABASE_DEFAULT', '')
916+
$createDbQuery = $createDbQuery.Replace(', OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF', '')
911917
}
912918

913919
$addPermissionsQuery = @'
@@ -930,47 +936,42 @@ ALTER ROLE [db_datawriter] ADD MEMBER [{1}]
930936
GO
931937
'@
932938

933-
if (-not (Test-Path -Path C:\DSCDB))
934-
{
935-
New-Item -ItemType Directory -Path C:\DSCDB | Out-Null
939+
if (-not (Test-Path -Path C:\DSCDB)) {
940+
New-Item -ItemType Directory -Path C:\DSCDB | Out-Null
936941
}
937942

938-
$dbCreated = Invoke-Sqlcmd -Query "SELECT name FROM master.sys.databases WHERE name='DSC'" -ServerInstance $ServerInstance -Encrypt $Encrypt
939-
if (-not $dbCreated)
940-
{
941-
Write-Verbose "Creating the DSC database on the local default SQL instance..."
943+
$dbCreated = Invoke-Sqlcmd -Query "SELECT name FROM master.sys.databases WHERE name='DSC'" @sqlCmdParam
944+
if (-not $dbCreated) {
945+
Write-Verbose "Creating the DSC database on the local default SQL instance..."
942946

943-
Invoke-Sqlcmd -Query $createDbQuery -ServerInstance $ServerInstance -Encrypt $Encrypt
947+
Invoke-Sqlcmd -Query $createDbQuery @sqlCmdParam
944948

945-
Write-Verbose 'finished.'
946-
Write-Verbose 'Database is stored on C:\DSCDB'
949+
Write-Verbose 'finished.'
950+
Write-Verbose 'Database is stored on C:\DSCDB'
947951
}
948952

949953
Write-Verbose "Adding permissions to DSC database for $DomainAndComputerName..."
950954

951955
$domain = ($DomainAndComputerName -split '\\')[0]
952956
$name = ($DomainAndComputerName -split '\\')[1]
953957

954-
if ($ComputerName -eq $env:COMPUTERNAME -and $DomainName -eq $env:USERDOMAIN)
955-
{
956-
$domain = 'NT AUTHORITY'
957-
$name = 'SYSTEM'
958+
if ($ComputerName -eq $env:COMPUTERNAME -and $DomainName -eq $env:USERDOMAIN) {
959+
$domain = 'NT AUTHORITY'
960+
$name = 'SYSTEM'
958961
}
959962
$name = $name + '$'
960963

961964
$account = New-Object System.Security.Principal.NTAccount($domain, $name)
962-
try
963-
{
964-
$account.Translate([System.Security.Principal.SecurityIdentifier]) | Out-Null
965+
try {
966+
$account.Translate([System.Security.Principal.SecurityIdentifier]) | Out-Null
965967
}
966-
catch
967-
{
968-
Write-Error "The account '$domain\$name' could not be found"
969-
continue
968+
catch {
969+
Write-Error "The account '$domain\$name' could not be found"
970+
continue
970971
}
971972

972973
$query = $addPermissionsQuery -f $domain, $name
973974

974-
Invoke-Sqlcmd -Query $query -ServerInstance $ServerInstance -Encrypt $Encrypt
975+
Invoke-Sqlcmd -Query $query @sqlCmdParam
975976

976977
Write-Verbose 'finished'

0 commit comments

Comments
 (0)