This commit is contained in:
Alexander (Sasha) Nosov
2024-05-29 12:20:15 -07:00
committed by GitHub
parent d181e237b8
commit 673feb1147
@@ -1,25 +1,114 @@
param ( param (
[Parameter (Mandatory=$true)]
[string]$AzureSubscriptionId, [string]$AzureSubscriptionId,
[Parameter (Mandatory=$true)]
[string]$AzureResourceGroupUri, [string]$AzureResourceGroupUri,
[Parameter (Mandatory=$true)]
[string]$location, [string]$location,
[Parameter (Mandatory=$false)]
[string]$SqlServerInstanceName, [string]$SqlServerInstanceName,
[Parameter (Mandatory=$true)]
[string]$SqlServerAdminAccount, [string]$SqlServerAdminAccount,
[Parameter (Mandatory=$true)]
[string]$SqlServerAdminPassword, [string]$SqlServerAdminPassword,
[Parameter (Mandatory=$true)]
[string]$SqlServerVersion, [string]$SqlServerVersion,
[Parameter (Mandatory=$true)]
[string]$SqlServerEdition, [string]$SqlServerEdition,
[Parameter (Mandatory=$true)]
[string]$SqlServerProductKey, [string]$SqlServerProductKey,
[Parameter (Mandatory=$false)]
[string]$SqlServerCU = "latest", [string]$SqlServerCU = "latest",
[string]$isoURL # URL to the ISO file [Parameter (Mandatory=$false)]
[string]$isoLocation = "C:\download\SQLServer.iso"
) )
# This function checks if the specified module is imported into the session and if not installes and/or imports it
function LoadModule
{
param (
[parameter(Mandatory = $true)][string] $name
)
$retVal = $true
if (!(Get-Module -Name $name))
{
$retVal = Get-Module -ListAvailable | Where-Object {$_.Name -eq $name}
if ($retVal)
{
try
{
Import-Module $name -ErrorAction SilentlyContinue
}
catch
{
write-host "The request to lload module $($name) failed with the following error:"
write-host $_.Exception.Message
$retVal = $false
}
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $name) {
Install-Module -Name $name -Force -Verbose -Scope CurrentUser
try
{
Import-Module $name -ErrorAction SilentlyContinue
}
catch
{
write-host "The request to lload module $($name) failed with the following error:"
write-host $_.Exception.Message
$retVal = $false
}
}
else {
# If module is not imported, not available and not in online gallery then abort
write-host "Module $($name) not imported, not available and not in online gallery, exiting."
EXIT 1
}
}
}
return $retVal
}
try { try {
# Step 0: Check if setup.exe is already running and kill it if so # ISO URL for each version
$isoURL @{
2012 = "";
2014 = "";
2016 = "";
2019 = "";
2022 = "https://download.microsoft.com/download/7/c/1/7c14e92e-bdcb-4f89-b7cf-93543e7112d1/SQLServer2019-x64-ENU-Dev.iso"
}
#Step 0: Ensure PS version and load missing Azure modules
#
# Suppress warnings
#
Update-AzConfig -DisplayBreakingChangeWarning $false
# Load required modules
$requiredModules = @(
"AzureAD",
"Az.Accounts",
"Az.ConnectedMachine",
"Az.ResourceGraph"
)
$requiredModules | Foreach-Object {LoadModule $_}
# Step 1: Check if setup.exe is already running and kill it if so
if (Get-Process setup -ErrorAction SilentlyContinue) { if (Get-Process setup -ErrorAction SilentlyContinue) {
Stop-Process -Name setup -Force Stop-Process -Name setup -Force
Write-Host "Existing setup.exe process terminated." Write-Host "Existing setup.exe process terminated."
} }
# Step 1: Log in to Azure # Step 2: Log in to Azure
az login az login
$subscription = Get-AzSubscription -SubscriptionId $AzureSubscriptionId -ErrorAction SilentlyContinue $subscription = Get-AzSubscription -SubscriptionId $AzureSubscriptionId -ErrorAction SilentlyContinue
if (-not $subscription) { if (-not $subscription) {
@@ -38,8 +127,6 @@ try {
} }
az group update --name $AzureResourceGroupUri --tags "ArcOnboarding=Blocked" az group update --name $AzureResourceGroupUri --tags "ArcOnboarding=Blocked"
# Step 3: Onboard the VM to Azure Arc # Step 3: Onboard the VM to Azure Arc
$hostName = (Get-WmiObject Win32_ComputerSystem).Name $hostName = (Get-WmiObject Win32_ComputerSystem).Name
@@ -50,29 +137,29 @@ try {
# Step 5: Automatically download installable media # Step 5: Automatically download installable media
$localPath = "C:\path\to\download\SQLServer.iso"
$freeSpace = (Get-PSDrive -Name C).Free if (!(Test-Path -Path $isoLocation)) {
$isoSize = (Invoke-WebRequest -Uri $isoURL -Method Head).Headers.'Content-Length' $freeSpace = (Get-PSDrive -Name C).Free
$isoSize = (Invoke-WebRequest -Uri $isoURL[$SqlServerVersion] -Method Head).Headers.'Content-Length'
if ($freeSpace -gt $isoSize) { if ($freeSpace -gt $isoSize) {
Start-BitsTransfer -Source $isoURL -Destination $localPath Start-BitsTransfer -Source $isoURL[$SqlServerVersion] -Destination $isoLocation
} else { } else {
throw "Not enough free space to download the ISO." throw "Not enough free space to download the ISO."
}
} }
# Step 6: Mount the ISO file as a volume # Step 6: Mount the ISO file as a volume
$volumeInfo = Mount-DiskImage -ImagePath $localPath -PassThru | Get-Volume $volumeInfo = Mount-DiskImage -ImagePath $isoLocation -PassThru | Get-Volume
# Step 7: Run unattended SQL Server setup from the mounted volume # Step 7: Run unattended SQL Server setup from the mounted volume
$setupPath = ($volumeInfo.DriveLetter + ":\setup.exe") $setupPath = ($volumeInfo.DriveLetter + ":\setup.exe")
Start-Process -FilePath $setupPath -ArgumentList "/q /ACTION=Install /INSTANCENAME=$SqlServerInstanceName /FEATURES=SQL /INSTANCEDIR=C:\SQL /SQLSYSADMINACCOUNTS=$SqlServerAdminAccount /SQLSVCACCOUNT=$SqlServerAdminAccount /SQLSVCPASSWORD=$SqlServerAdminPassword /AGTSVCACCOUNT=$SqlServerAdminAccount /AGTSVCPASSWORD=$SqlServerAdminPassword /IACCEPTSQLSERVERLICENSETERMS /PID=$SqlServerProductKey /SQLSERVERUPDATE=$SqlServerCU" Start-Process -FilePath $setupPath -ArgumentList "/q /ACTION=Install /INSTANCENAME=$SqlServerInstanceName /FEATURES=SQL /INSTANCEDIR=C:\SQL /SQLSYSADMINACCOUNTS=$SqlServerAdminAccount /SQLSVCACCOUNT=$SqlServerAdminAccount /SQLSVCPASSWORD=$SqlServerAdminPassword /AGTSVCACCOUNT=$SqlServerAdminAccount /AGTSVCPASSWORD=$SqlServerAdminPassword /IACCEPTSQLSERVERLICENSETERMS /PID=$SqlServerProductKey /SQLSERVERUPDATE=$SqlServerCU"
# Step 8: Dismount the ISO file after installation # Step 8: Dismount the ISO file after installation
Dismount-DiskImage -ImagePath $localPath Dismount-DiskImage -ImagePath $isoLocation
# Step 9: Remove the media from the local file system # Step 9: Remove the media from the local file system
Remove-Item -Path $localPath Remove-Item -Path $isoLocation
# Step 10: Display the status of the Azure resource # Step 10: Display the status of the Azure resource
az resource show --ids $AzureResourceGroupUri az resource show --ids $AzureResourceGroupUri