PowerShell Script: Install-SQLServer
PowerShell Script: Install-SQLServer.ps1
# PowerShell Script: Install-SQLServer.ps1
# Purpose: Fully automate SQL Server and SSMS installation
# --- Variables ---
$SQLDownloadURL = "https://go.microsoft.com/fwlink/?linkid=866658" # SQL 2022 Developer
$SSMSDownloadURL = "https://aka.ms/ssmsfullsetup"
$SQLInstaller = "C:\SQLServer2022.exe"
$SQLMediaPath = "C:\SQLSetup"
$ConfigFile = "C:\SQLSetup\ConfigurationFile.ini"
$SSMSInstaller = "C:\SSMSSetup.exe"
# --- Step 1: Download installers ---
Write-Host "Downloading SQL Server..."
Invoke-WebRequest -Uri $SQLDownloadURL -OutFile $SQLInstaller
Write-Host "Downloading SSMS..."
Invoke-WebRequest -Uri $SSMSDownloadURL -OutFile $SSMSInstaller
# --- Step 2: Extract SQL Server setup files ---
Write-Host "Extracting SQL Server installer..."
Start-Process -FilePath $SQLInstaller -ArgumentList "/ACTION=Download /MEDIAPATH=$SQLMediaPath /QUIET" -Wait
# --- Step 3: Create Configuration File ---
Write-Host "Creating configuration file..."
@"
[OPTIONS]
ACTION="Install"
FEATURES=SQLENGINE
INSTANCENAME="MSSQLSERVER"
SQLSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"
AGTSVCACCOUNT="NT AUTHORITY\NETWORK SERVICE"
SECURITYMODE="SQL"
SAPWD="P@ssw0rd123"
SQLSYSADMINACCOUNTS="$env:USERNAME"
TCPENABLED="1"
NPENABLED="0"
IACCEPTSQLSERVERLICENSETERMS="True"
"@ | Out-File -FilePath $ConfigFile -Encoding ASCII
# --- Step 4: Run silent SQL Server installation ---
Write-Host "Installing SQL Server..."
Start-Process -FilePath "$SQLMediaPath\setup.exe" -ArgumentList "/ConfigurationFile=$ConfigFile /QUIET /NORESTART" -Wait
# --- Step 5: Open firewall ports ---
Write-Host "Configuring firewall..."
netsh advfirewall firewall add rule name="SQL Server TCP 1433" dir=in action=allow protocol=TCP localport=1433
netsh advfirewall firewall add rule name="SQL Browser UDP 1434" dir=in action=allow protocol=UDP localport=1434
# --- Step 6: Install SSMS silently ---
Write-Host "Installing SQL Server Management Studio..."
Start-Process -FilePath $SSMSInstaller -ArgumentList "/install /quiet /norestart" -Wait
# --- Step 7: Verify installation ---
Write-Host "SQL Server and SSMS installation completed."
Write-Host "You can now connect using SSMS to 'localhost' with Windows Authentication."
---
How to Use
1. Save the above script as Install-SQLServer.ps1.
2. Open PowerShell as Administrator.
3. Run:
Set-ExecutionPolicy Bypass -Scope Process -Force
.\Install-SQLServer.ps1
---
Result
SQL Server 2022 Developer installed silently.
SSMS installed silently.
TCP port 1433 and UDP 1434 open for remote access.
Default instance: MSSQLSERVER
sa password: P@ssw0rd123 (change it later).
Comments
Post a Comment