From b8108b1e6e68ebe479ae3f1c0f86a098daf6768b Mon Sep 17 00:00:00 2001 From: Lukas Volf Date: Mon, 25 Mar 2024 03:22:22 +0100 Subject: [PATCH] F Obsoleted scripts --- .../install2019Developer.bat | 2 +- .../install2019Developer.ps1 | 326 +++++++++--------- .../install2019Express.bat | 2 +- .../install2019Express.ps1 | 326 +++++++++--------- .../install2022Developer.bat | 2 +- .../install2022Developer.ps1 | 326 +++++++++--------- .../install2022Express.bat | 2 +- .../install2022Express.ps1 | 326 +++++++++--------- 8 files changed, 656 insertions(+), 656 deletions(-) rename src/{ => LegacyScripts}/install2019Developer.bat (99%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2019Developer.ps1 (97%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2019Express.bat (99%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2019Express.ps1 (97%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2022Developer.bat (99%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2022Developer.ps1 (97%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2022Express.bat (99%) mode change 100755 => 100644 rename src/{ => LegacyScripts}/install2022Express.ps1 (97%) mode change 100755 => 100644 diff --git a/src/install2019Developer.bat b/src/LegacyScripts/install2019Developer.bat old mode 100755 new mode 100644 similarity index 99% rename from src/install2019Developer.bat rename to src/LegacyScripts/install2019Developer.bat index db61fbf..f31f99b --- a/src/install2019Developer.bat +++ b/src/LegacyScripts/install2019Developer.bat @@ -1 +1 @@ -Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2019Developer.ps1' -Verb RunAs}" +Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2019Developer.ps1' -Verb RunAs}" diff --git a/src/install2019Developer.ps1 b/src/LegacyScripts/install2019Developer.ps1 old mode 100755 new mode 100644 similarity index 97% rename from src/install2019Developer.ps1 rename to src/LegacyScripts/install2019Developer.ps1 index 74cec10..8ea259b --- a/src/install2019Developer.ps1 +++ b/src/LegacyScripts/install2019Developer.ps1 @@ -1,163 +1,163 @@ -# set the current path to the same directory as the script -Set-Location -LiteralPath $PSScriptRoot - -$sqlExpressDownloadUrl = "https://go.microsoft.com/fwlink/?linkid=866662" -$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" -$sqlYear = 2019 -$sqlVersion = 15 -$instanceName = "MSSQLSERVER" -$user = whoami - -$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Dev.exe" -$fullInstallerPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU.exe" -$setupFolderPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU" -$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" -$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQL /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /SQLSYSADMINACCOUNTS=$user /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" - -$maxDownloadRepeatCount = 5 -$actualInstanceName = $instanceName -If ($instanceName -eq "MSSQLSERVER") { - $serviceName = $instanceName -} else { - $serviceName = "MSSQL`$$instanceName" -} -$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" -$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" - -Function Send-ToRecycleBin -{ - Param( - [Parameter(Mandatory = $true, - ValueFromPipeline = $true)] - [alias('FullName')] - [string]$FilePath - ) - Begin{$shell = New-Object -ComObject 'Shell.Application'} - Process{ - $Item = Get-Item $FilePath - $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') - } -} - -# create temporary work folder -If(-not(Test-Path "./Temp")) { - New-Item -Path "Temp" -ItemType Directory -} - -# download and install SQL Server -If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Installer..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { - Write-Host "Downloading Full SQL Developer Installer..." - $absoluteInstallerPath = (Resolve-Path "./Temp").Path - - # use the "express installer" to download the full installer - Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait -} - -If(-not(Test-Path $setupFolderPath)) { - # extract the full installer - Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" -} - -# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. -# The installation will fail on M1 in the final steps of the installation. - -# run the setup - the first time it fails to install SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# running the setup for the second time installs the SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database - -# fix the SQL installation: -$logDir = "$installedPath\Log" -New-Item -Path $logDir -ItemType Directory - -$dataDir = "$installedPath\DATA" -New-Item -Path $dataDir -ItemType Directory - -# set ACL for the SQL Server account -$user = "NT SERVICE\$serviceName" - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# set the ACL for the current user -$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work -Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose -Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose - -# create missing registry entries to point the SQL Server to the right master database -$name = "SQLArg0" -$value = "-d$installedPath\DATA\master.mdf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg1" -$value = "-e$installedPath\Log\ERRORLOG" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg2" -$value = "-l$installedPath\DATA\mastlog.ldf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force - -# rebuild databases -$user = whoami -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait - -# run the service - it should start now -net start $serviceName - -# download and install SSMS -If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Server Management Studio..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -# install SSMS -Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait - -# delete temp files -"./Temp" | Send-ToRecycleBin - -echo "SQL Installation completed, press any key to exit" -[Console]::ReadKey() +# set the current path to the same directory as the script +Set-Location -LiteralPath $PSScriptRoot + +$sqlExpressDownloadUrl = "https://go.microsoft.com/fwlink/?linkid=866662" +$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" +$sqlYear = 2019 +$sqlVersion = 15 +$instanceName = "MSSQLSERVER" +$user = whoami + +$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Dev.exe" +$fullInstallerPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU.exe" +$setupFolderPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU" +$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" +$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQL /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /SQLSYSADMINACCOUNTS=$user /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" + +$maxDownloadRepeatCount = 5 +$actualInstanceName = $instanceName +If ($instanceName -eq "MSSQLSERVER") { + $serviceName = $instanceName +} else { + $serviceName = "MSSQL`$$instanceName" +} +$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" +$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" + +Function Send-ToRecycleBin +{ + Param( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true)] + [alias('FullName')] + [string]$FilePath + ) + Begin{$shell = New-Object -ComObject 'Shell.Application'} + Process{ + $Item = Get-Item $FilePath + $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') + } +} + +# create temporary work folder +If(-not(Test-Path "./Temp")) { + New-Item -Path "Temp" -ItemType Directory +} + +# download and install SQL Server +If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Installer..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { + Write-Host "Downloading Full SQL Developer Installer..." + $absoluteInstallerPath = (Resolve-Path "./Temp").Path + + # use the "express installer" to download the full installer + Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait +} + +If(-not(Test-Path $setupFolderPath)) { + # extract the full installer + Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" +} + +# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. +# The installation will fail on M1 in the final steps of the installation. + +# run the setup - the first time it fails to install SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# running the setup for the second time installs the SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database + +# fix the SQL installation: +$logDir = "$installedPath\Log" +New-Item -Path $logDir -ItemType Directory + +$dataDir = "$installedPath\DATA" +New-Item -Path $dataDir -ItemType Directory + +# set ACL for the SQL Server account +$user = "NT SERVICE\$serviceName" + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# set the ACL for the current user +$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work +Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose +Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose + +# create missing registry entries to point the SQL Server to the right master database +$name = "SQLArg0" +$value = "-d$installedPath\DATA\master.mdf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg1" +$value = "-e$installedPath\Log\ERRORLOG" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg2" +$value = "-l$installedPath\DATA\mastlog.ldf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force + +# rebuild databases +$user = whoami +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait + +# run the service - it should start now +net start $serviceName + +# download and install SSMS +If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Server Management Studio..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +# install SSMS +Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait + +# delete temp files +"./Temp" | Send-ToRecycleBin + +echo "SQL Installation completed, press any key to exit" +[Console]::ReadKey() diff --git a/src/install2019Express.bat b/src/LegacyScripts/install2019Express.bat old mode 100755 new mode 100644 similarity index 99% rename from src/install2019Express.bat rename to src/LegacyScripts/install2019Express.bat index 7caf7ee..83b49eb --- a/src/install2019Express.bat +++ b/src/LegacyScripts/install2019Express.bat @@ -1 +1 @@ -Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2019Express.ps1' -Verb RunAs}" +Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2019Express.ps1' -Verb RunAs}" diff --git a/src/install2019Express.ps1 b/src/LegacyScripts/install2019Express.ps1 old mode 100755 new mode 100644 similarity index 97% rename from src/install2019Express.ps1 rename to src/LegacyScripts/install2019Express.ps1 index e3ae44d..b04299d --- a/src/install2019Express.ps1 +++ b/src/LegacyScripts/install2019Express.ps1 @@ -1,163 +1,163 @@ -# set the current path to the same directory as the script -Set-Location -LiteralPath $PSScriptRoot - -$sqlExpressDownloadUrl = "https://download.microsoft.com/download/7/f/8/7f8a9c43-8c8a-4f7c-9f92-83c18d96b681/SQL2019-SSEI-Expr.exe" -$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" -$sqlYear = 2019 -$sqlVersion = 15 -$instanceName = "SQLEXPRESS" -$user = whoami - -$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Expr.exe" -$fullInstallerPath = "./Temp/SQLEXPR_x64_ENU.exe" -$setupFolderPath = "./Temp/SQLEXPR_x64_ENU" -$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" -$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQLEngine /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" - -$maxDownloadRepeatCount = 5 -$actualInstanceName = $instanceName -If ($instanceName -eq "MSSQLSERVER") { - $serviceName = $instanceName -} else { - $serviceName = "MSSQL`$$instanceName" -} -$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" -$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" - -Function Send-ToRecycleBin -{ - Param( - [Parameter(Mandatory = $true, - ValueFromPipeline = $true)] - [alias('FullName')] - [string]$FilePath - ) - Begin{$shell = New-Object -ComObject 'Shell.Application'} - Process{ - $Item = Get-Item $FilePath - $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') - } -} - -# create temporary work folder -If(-not(Test-Path "./Temp")) { - New-Item -Path "Temp" -ItemType Directory -} - -# download and install SQL Server -If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Installer..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { - Write-Host "Downloading Full SQL Installer..." - $absoluteInstallerPath = (Resolve-Path "./Temp").Path - - # use the "express installer" to download the full installer - Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait -} - -If(-not(Test-Path $setupFolderPath)) { - # extract the full installer - Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" -} - -# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. -# The installation will fail on M1 in the final steps of the installation. - -# run the setup - the first time it fails to install SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# running the setup for the second time installs the SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database - -# fix the SQL installation: -$logDir = "$installedPath\Log" -New-Item -Path $logDir -ItemType Directory - -$dataDir = "$installedPath\DATA" -New-Item -Path $dataDir -ItemType Directory - -# set ACL for the SQL Server account -$user = "NT SERVICE\$serviceName" - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# set the ACL for the current user -$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work -Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose -Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose - -# create missing registry entries to point the SQL Server to the right master database -$name = "SQLArg0" -$value = "-d$installedPath\DATA\master.mdf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg1" -$value = "-e$installedPath\Log\ERRORLOG" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg2" -$value = "-l$installedPath\DATA\mastlog.ldf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force - -# rebuild databases -$user = whoami -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait - -# run the service - it should start now -net start $serviceName - -# download and install SSMS -If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Server Management Studio..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -# install SSMS -Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait - -# delete temp files -"./Temp" | Send-ToRecycleBin - -echo "SQL Installation completed, press any key to exit" -[Console]::ReadKey() +# set the current path to the same directory as the script +Set-Location -LiteralPath $PSScriptRoot + +$sqlExpressDownloadUrl = "https://download.microsoft.com/download/7/f/8/7f8a9c43-8c8a-4f7c-9f92-83c18d96b681/SQL2019-SSEI-Expr.exe" +$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" +$sqlYear = 2019 +$sqlVersion = 15 +$instanceName = "SQLEXPRESS" +$user = whoami + +$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Expr.exe" +$fullInstallerPath = "./Temp/SQLEXPR_x64_ENU.exe" +$setupFolderPath = "./Temp/SQLEXPR_x64_ENU" +$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" +$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQLEngine /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" + +$maxDownloadRepeatCount = 5 +$actualInstanceName = $instanceName +If ($instanceName -eq "MSSQLSERVER") { + $serviceName = $instanceName +} else { + $serviceName = "MSSQL`$$instanceName" +} +$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" +$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" + +Function Send-ToRecycleBin +{ + Param( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true)] + [alias('FullName')] + [string]$FilePath + ) + Begin{$shell = New-Object -ComObject 'Shell.Application'} + Process{ + $Item = Get-Item $FilePath + $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') + } +} + +# create temporary work folder +If(-not(Test-Path "./Temp")) { + New-Item -Path "Temp" -ItemType Directory +} + +# download and install SQL Server +If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Installer..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { + Write-Host "Downloading Full SQL Installer..." + $absoluteInstallerPath = (Resolve-Path "./Temp").Path + + # use the "express installer" to download the full installer + Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait +} + +If(-not(Test-Path $setupFolderPath)) { + # extract the full installer + Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" +} + +# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. +# The installation will fail on M1 in the final steps of the installation. + +# run the setup - the first time it fails to install SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# running the setup for the second time installs the SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database + +# fix the SQL installation: +$logDir = "$installedPath\Log" +New-Item -Path $logDir -ItemType Directory + +$dataDir = "$installedPath\DATA" +New-Item -Path $dataDir -ItemType Directory + +# set ACL for the SQL Server account +$user = "NT SERVICE\$serviceName" + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# set the ACL for the current user +$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work +Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose +Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose + +# create missing registry entries to point the SQL Server to the right master database +$name = "SQLArg0" +$value = "-d$installedPath\DATA\master.mdf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg1" +$value = "-e$installedPath\Log\ERRORLOG" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg2" +$value = "-l$installedPath\DATA\mastlog.ldf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force + +# rebuild databases +$user = whoami +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait + +# run the service - it should start now +net start $serviceName + +# download and install SSMS +If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Server Management Studio..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +# install SSMS +Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait + +# delete temp files +"./Temp" | Send-ToRecycleBin + +echo "SQL Installation completed, press any key to exit" +[Console]::ReadKey() diff --git a/src/install2022Developer.bat b/src/LegacyScripts/install2022Developer.bat old mode 100755 new mode 100644 similarity index 99% rename from src/install2022Developer.bat rename to src/LegacyScripts/install2022Developer.bat index 27c2afe..253f4e8 --- a/src/install2022Developer.bat +++ b/src/LegacyScripts/install2022Developer.bat @@ -1 +1 @@ -Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2022Developer.ps1' -Verb RunAs}" +Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2022Developer.ps1' -Verb RunAs}" diff --git a/src/install2022Developer.ps1 b/src/LegacyScripts/install2022Developer.ps1 old mode 100755 new mode 100644 similarity index 97% rename from src/install2022Developer.ps1 rename to src/LegacyScripts/install2022Developer.ps1 index 4b72a1e..d68e281 --- a/src/install2022Developer.ps1 +++ b/src/LegacyScripts/install2022Developer.ps1 @@ -1,163 +1,163 @@ -# set the current path to the same directory as the script -Set-Location -LiteralPath $PSScriptRoot - -$sqlExpressDownloadUrl = "https://download.microsoft.com/download/c/c/9/cc9c6797-383c-4b24-8920-dc057c1de9d3/SQL2022-SSEI-Dev.exe" -$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" -$sqlYear = 2022 -$sqlVersion = 16 -$instanceName = "MSSQLSERVER" -$user = whoami - -$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Dev.exe" -$fullInstallerPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU.exe" -$setupFolderPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU" -$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" -$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQL /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /SQLSYSADMINACCOUNTS=$user /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" - -$maxDownloadRepeatCount = 5 -$actualInstanceName = $instanceName -If ($instanceName -eq "MSSQLSERVER") { - $serviceName = $instanceName -} else { - $serviceName = "MSSQL`$$instanceName" -} -$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" -$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" - -Function Send-ToRecycleBin -{ - Param( - [Parameter(Mandatory = $true, - ValueFromPipeline = $true)] - [alias('FullName')] - [string]$FilePath - ) - Begin{$shell = New-Object -ComObject 'Shell.Application'} - Process{ - $Item = Get-Item $FilePath - $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') - } -} - -# create temporary work folder -If(-not(Test-Path "./Temp")) { - New-Item -Path "Temp" -ItemType Directory -} - -# download and install SQL Server -If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Installer..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { - Write-Host "Downloading Full SQL Developer Installer..." - $absoluteInstallerPath = (Resolve-Path "./Temp").Path - - # use the "express installer" to download the full installer - Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait -} - -If(-not(Test-Path $setupFolderPath)) { - # extract the full installer - Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" -} - -# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. -# The installation will fail on M1 in the final steps of the installation. - -# run the setup - the first time it fails to install SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# running the setup for the second time installs the SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database - -# fix the SQL installation: -$logDir = "$installedPath\Log" -New-Item -Path $logDir -ItemType Directory - -$dataDir = "$installedPath\DATA" -New-Item -Path $dataDir -ItemType Directory - -# set ACL for the SQL Server account -$user = "NT SERVICE\$serviceName" - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# set the ACL for the current user -$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work -Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose -Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose - -# create missing registry entries to point the SQL Server to the right master database -$name = "SQLArg0" -$value = "-d$installedPath\DATA\master.mdf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg1" -$value = "-e$installedPath\Log\ERRORLOG" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg2" -$value = "-l$installedPath\DATA\mastlog.ldf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force - -# rebuild databases -$user = whoami -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait - -# run the service - it should start now -net start $serviceName - -# download and install SSMS -If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Server Management Studio..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -# install SSMS -Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait - -# delete temp files -"./Temp" | Send-ToRecycleBin - -echo "SQL Installation completed, press any key to exit" -[Console]::ReadKey() +# set the current path to the same directory as the script +Set-Location -LiteralPath $PSScriptRoot + +$sqlExpressDownloadUrl = "https://download.microsoft.com/download/c/c/9/cc9c6797-383c-4b24-8920-dc057c1de9d3/SQL2022-SSEI-Dev.exe" +$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" +$sqlYear = 2022 +$sqlVersion = 16 +$instanceName = "MSSQLSERVER" +$user = whoami + +$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Dev.exe" +$fullInstallerPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU.exe" +$setupFolderPath = "./Temp/SQLServer$sqlYear-DEV-x64-ENU" +$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" +$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQL /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /SQLSYSADMINACCOUNTS=$user /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" + +$maxDownloadRepeatCount = 5 +$actualInstanceName = $instanceName +If ($instanceName -eq "MSSQLSERVER") { + $serviceName = $instanceName +} else { + $serviceName = "MSSQL`$$instanceName" +} +$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" +$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" + +Function Send-ToRecycleBin +{ + Param( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true)] + [alias('FullName')] + [string]$FilePath + ) + Begin{$shell = New-Object -ComObject 'Shell.Application'} + Process{ + $Item = Get-Item $FilePath + $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') + } +} + +# create temporary work folder +If(-not(Test-Path "./Temp")) { + New-Item -Path "Temp" -ItemType Directory +} + +# download and install SQL Server +If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Installer..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { + Write-Host "Downloading Full SQL Developer Installer..." + $absoluteInstallerPath = (Resolve-Path "./Temp").Path + + # use the "express installer" to download the full installer + Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait +} + +If(-not(Test-Path $setupFolderPath)) { + # extract the full installer + Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" +} + +# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. +# The installation will fail on M1 in the final steps of the installation. + +# run the setup - the first time it fails to install SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# running the setup for the second time installs the SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database + +# fix the SQL installation: +$logDir = "$installedPath\Log" +New-Item -Path $logDir -ItemType Directory + +$dataDir = "$installedPath\DATA" +New-Item -Path $dataDir -ItemType Directory + +# set ACL for the SQL Server account +$user = "NT SERVICE\$serviceName" + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# set the ACL for the current user +$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work +Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose +Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose + +# create missing registry entries to point the SQL Server to the right master database +$name = "SQLArg0" +$value = "-d$installedPath\DATA\master.mdf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg1" +$value = "-e$installedPath\Log\ERRORLOG" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg2" +$value = "-l$installedPath\DATA\mastlog.ldf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force + +# rebuild databases +$user = whoami +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait + +# run the service - it should start now +net start $serviceName + +# download and install SSMS +If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Server Management Studio..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +# install SSMS +Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait + +# delete temp files +"./Temp" | Send-ToRecycleBin + +echo "SQL Installation completed, press any key to exit" +[Console]::ReadKey() diff --git a/src/install2022Express.bat b/src/LegacyScripts/install2022Express.bat old mode 100755 new mode 100644 similarity index 99% rename from src/install2022Express.bat rename to src/LegacyScripts/install2022Express.bat index 0db830b..813a340 --- a/src/install2022Express.bat +++ b/src/LegacyScripts/install2022Express.bat @@ -1 +1 @@ -Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2022Express.ps1' -Verb RunAs}" +Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0install2022Express.ps1' -Verb RunAs}" diff --git a/src/install2022Express.ps1 b/src/LegacyScripts/install2022Express.ps1 old mode 100755 new mode 100644 similarity index 97% rename from src/install2022Express.ps1 rename to src/LegacyScripts/install2022Express.ps1 index f9bfdf8..181d16a --- a/src/install2022Express.ps1 +++ b/src/LegacyScripts/install2022Express.ps1 @@ -1,163 +1,163 @@ -# set the current path to the same directory as the script -Set-Location -LiteralPath $PSScriptRoot - -$sqlExpressDownloadUrl = "https://download.microsoft.com/download/5/1/4/5145fe04-4d30-4b85-b0d1-39533663a2f1/SQL2022-SSEI-Expr.exe" -$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" -$sqlYear = 2022 -$sqlVersion = 16 -$instanceName = "SQLEXPRESS" -$user = whoami - -$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Expr.exe" -$fullInstallerPath = "./Temp/SQLEXPR_x64_ENU.exe" -$setupFolderPath = "./Temp/SQLEXPR_x64_ENU" -$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" -$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQLEngine /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" - -$maxDownloadRepeatCount = 5 -$actualInstanceName = $instanceName -If ($instanceName -eq "MSSQLSERVER") { - $serviceName = $instanceName -} else { - $serviceName = "MSSQL`$$instanceName" -} -$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" -$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" - -Function Send-ToRecycleBin -{ - Param( - [Parameter(Mandatory = $true, - ValueFromPipeline = $true)] - [alias('FullName')] - [string]$FilePath - ) - Begin{$shell = New-Object -ComObject 'Shell.Application'} - Process{ - $Item = Get-Item $FilePath - $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') - } -} - -# create temporary work folder -If(-not(Test-Path "./Temp")) { - New-Item -Path "Temp" -ItemType Directory -} - -# download and install SQL Server -If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Installer..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { - Write-Host "Downloading Full SQL Installer..." - $absoluteInstallerPath = (Resolve-Path "./Temp").Path - - # use the "express installer" to download the full installer - Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait -} - -If(-not(Test-Path $setupFolderPath)) { - # extract the full installer - Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" -} - -# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. -# The installation will fail on M1 in the final steps of the installation. - -# run the setup - the first time it fails to install SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# running the setup for the second time installs the SQL Server Windows service -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait - -# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database - -# fix the SQL installation: -$logDir = "$installedPath\Log" -New-Item -Path $logDir -ItemType Directory - -$dataDir = "$installedPath\DATA" -New-Item -Path $dataDir -ItemType Directory - -# set ACL for the SQL Server account -$user = "NT SERVICE\$serviceName" - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# set the ACL for the current user -$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name - -$acl = Get-Acl $logDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $logDir - -$acl = Get-Acl $dataDir -$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") -$acl.SetAccessRule($accessRule) -$acl | Set-Acl $dataDir - -# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work -Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose -Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose - -# create missing registry entries to point the SQL Server to the right master database -$name = "SQLArg0" -$value = "-d$installedPath\DATA\master.mdf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg1" -$value = "-e$installedPath\Log\ERRORLOG" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force -$name = "SQLArg2" -$value = "-l$installedPath\DATA\mastlog.ldf" -New-ItemProperty -Path $registryPath -Name $name -Value $value -Force - -# rebuild databases -$user = whoami -Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait - -# run the service - it should start now -net start $serviceName - -# download and install SSMS -If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { - Write-Host "Downloading SQL Server Management Studio..." - - $counter = $maxDownloadRepeatCount; - While ($counter -gt 0) { - try { - Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath - $counter = 0 - } catch { - $counter -= 1 - } - } -} - -# install SSMS -Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait - -# delete temp files -"./Temp" | Send-ToRecycleBin - -echo "SQL Installation completed, press any key to exit" -[Console]::ReadKey() +# set the current path to the same directory as the script +Set-Location -LiteralPath $PSScriptRoot + +$sqlExpressDownloadUrl = "https://download.microsoft.com/download/5/1/4/5145fe04-4d30-4b85-b0d1-39533663a2f1/SQL2022-SSEI-Expr.exe" +$sqlManagementStudioDownloadUrl = "https://aka.ms/ssmsfullsetup?clcid=0x409" +$sqlYear = 2022 +$sqlVersion = 16 +$instanceName = "SQLEXPRESS" +$user = whoami + +$expressInstallerPath = "./Temp/SQL$sqlYear-SSEI-Expr.exe" +$fullInstallerPath = "./Temp/SQLEXPR_x64_ENU.exe" +$setupFolderPath = "./Temp/SQLEXPR_x64_ENU" +$ssmsInstallerPath = "./Temp/SSMS-Setup-ENU.exe" +$sqlInstallArgs = "/qs /ACTION=Install /FEATURES=SQLEngine /INSTANCENAME=$instanceName /ENU /IACCEPTSQLSERVERLICENSETERMS /UPDATEENABLED=false /USEMICROSOFTUPDATE=false" + +$maxDownloadRepeatCount = 5 +$actualInstanceName = $instanceName +If ($instanceName -eq "MSSQLSERVER") { + $serviceName = $instanceName +} else { + $serviceName = "MSSQL`$$instanceName" +} +$installedPath = "$Env:Programfiles\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQL" +$registryPath = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL$sqlVersion.$actualInstanceName\MSSQLServer\Parameters" + +Function Send-ToRecycleBin +{ + Param( + [Parameter(Mandatory = $true, + ValueFromPipeline = $true)] + [alias('FullName')] + [string]$FilePath + ) + Begin{$shell = New-Object -ComObject 'Shell.Application'} + Process{ + $Item = Get-Item $FilePath + $shell.namespace(0).ParseName($item.FullName).InvokeVerb('delete') + } +} + +# create temporary work folder +If(-not(Test-Path "./Temp")) { + New-Item -Path "Temp" -ItemType Directory +} + +# download and install SQL Server +If(-not(Test-Path $expressInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Installer..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlExpressDownloadUrl -OutFile $expressInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +If(-not(Test-Path $fullInstallerPath -PathType Leaf)) { + Write-Host "Downloading Full SQL Installer..." + $absoluteInstallerPath = (Resolve-Path "./Temp").Path + + # use the "express installer" to download the full installer + Start-Process -FilePath $expressInstallerPath -ArgumentList "/QUIET /ACTION=Download /ENU /MEDIAPATH=$absoluteInstallerPath" -Wait +} + +If(-not(Test-Path $setupFolderPath)) { + # extract the full installer + Start-Process -FilePath $fullInstallerPath -ArgumentList "/q" -Wait -WorkingDirectory "./Temp" +} + +# Running the setup in GUI will fail before the installation even starts because of Windows Updates, so it has to be executed from the command line. +# The installation will fail on M1 in the final steps of the installation. + +# run the setup - the first time it fails to install SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# running the setup for the second time installs the SQL Server Windows service +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList $sqlInstallArgs -Wait + +# now we are in the state when SQL is installed, but SQL Service cannot be started because there are missing registry entries and there is no master database + +# fix the SQL installation: +$logDir = "$installedPath\Log" +New-Item -Path $logDir -ItemType Directory + +$dataDir = "$installedPath\DATA" +New-Item -Path $dataDir -ItemType Directory + +# set ACL for the SQL Server account +$user = "NT SERVICE\$serviceName" + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# set the ACL for the current user +$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name + +$acl = Get-Acl $logDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $logDir + +$acl = Get-Acl $dataDir +$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl","ContainerInherit,ObjectInherit","None","Allow") +$acl.SetAccessRule($accessRule) +$acl | Set-Acl $dataDir + +# copy missing databases from the Templates folder - without these in place the SQL Database Rebuild won't work +Copy-Item -Path "$installedPath\Binn\Templates\master.mdf" -Destination "$installedPath\DATA" -Force -Verbose +Copy-Item -Path "$installedPath\Binn\Templates\mastlog.ldf" -Destination "$installedPath\DATA" -Force -Verbose + +# create missing registry entries to point the SQL Server to the right master database +$name = "SQLArg0" +$value = "-d$installedPath\DATA\master.mdf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg1" +$value = "-e$installedPath\Log\ERRORLOG" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force +$name = "SQLArg2" +$value = "-l$installedPath\DATA\mastlog.ldf" +New-ItemProperty -Path $registryPath -Name $name -Value $value -Force + +# rebuild databases +$user = whoami +Start-Process -FilePath "$setupFolderPath/SETUP.EXE" -ArgumentList "/QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=$actualInstanceName /ENU /SQLSYSADMINACCOUNTS=$user" -Wait + +# run the service - it should start now +net start $serviceName + +# download and install SSMS +If(-not(Test-Path $ssmsInstallerPath -PathType Leaf)) { + Write-Host "Downloading SQL Server Management Studio..." + + $counter = $maxDownloadRepeatCount; + While ($counter -gt 0) { + try { + Invoke-WebRequest -Uri $sqlManagementStudioDownloadUrl -OutFile $ssmsInstallerPath + $counter = 0 + } catch { + $counter -= 1 + } + } +} + +# install SSMS +Start-Process -FilePath $ssmsInstallerPath -ArgumentList "/Install /Passive" -Wait + +# delete temp files +"./Temp" | Send-ToRecycleBin + +echo "SQL Installation completed, press any key to exit" +[Console]::ReadKey()