From 673feb11478ded8f3333b00a8b187109c5d798ea Mon Sep 17 00:00:00 2001 From: "Alexander (Sasha) Nosov" Date: Wed, 29 May 2024 12:20:15 -0700 Subject: [PATCH] progress --- .../install-payg-sql-server.ps1 | 121 +++++++++++++++--- 1 file changed, 104 insertions(+), 17 deletions(-) diff --git a/samples/manage/azure-arc-enabled-sql-server/install-payg-sql-server/install-payg-sql-server.ps1 b/samples/manage/azure-arc-enabled-sql-server/install-payg-sql-server/install-payg-sql-server.ps1 index 51a6298c..77d4501e 100644 --- a/samples/manage/azure-arc-enabled-sql-server/install-payg-sql-server/install-payg-sql-server.ps1 +++ b/samples/manage/azure-arc-enabled-sql-server/install-payg-sql-server/install-payg-sql-server.ps1 @@ -1,25 +1,114 @@ param ( + [Parameter (Mandatory=$true)] [string]$AzureSubscriptionId, + [Parameter (Mandatory=$true)] [string]$AzureResourceGroupUri, + [Parameter (Mandatory=$true)] [string]$location, + [Parameter (Mandatory=$false)] [string]$SqlServerInstanceName, + [Parameter (Mandatory=$true)] [string]$SqlServerAdminAccount, + [Parameter (Mandatory=$true)] [string]$SqlServerAdminPassword, + [Parameter (Mandatory=$true)] [string]$SqlServerVersion, + [Parameter (Mandatory=$true)] [string]$SqlServerEdition, + [Parameter (Mandatory=$true)] [string]$SqlServerProductKey, + [Parameter (Mandatory=$false)] [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 { - # 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) { Stop-Process -Name setup -Force Write-Host "Existing setup.exe process terminated." } - # Step 1: Log in to Azure + # Step 2: Log in to Azure az login $subscription = Get-AzSubscription -SubscriptionId $AzureSubscriptionId -ErrorAction SilentlyContinue if (-not $subscription) { @@ -38,8 +127,6 @@ try { } az group update --name $AzureResourceGroupUri --tags "ArcOnboarding=Blocked" - - # Step 3: Onboard the VM to Azure Arc $hostName = (Get-WmiObject Win32_ComputerSystem).Name @@ -50,29 +137,29 @@ try { # Step 5: Automatically download installable media - $localPath = "C:\path\to\download\SQLServer.iso" - $freeSpace = (Get-PSDrive -Name C).Free - $isoSize = (Invoke-WebRequest -Uri $isoURL -Method Head).Headers.'Content-Length' - - if ($freeSpace -gt $isoSize) { - Start-BitsTransfer -Source $isoURL -Destination $localPath - } else { - throw "Not enough free space to download the ISO." + + if (!(Test-Path -Path $isoLocation)) { + $freeSpace = (Get-PSDrive -Name C).Free + $isoSize = (Invoke-WebRequest -Uri $isoURL[$SqlServerVersion] -Method Head).Headers.'Content-Length' + if ($freeSpace -gt $isoSize) { + Start-BitsTransfer -Source $isoURL[$SqlServerVersion] -Destination $isoLocation + } else { + throw "Not enough free space to download the ISO." + } } - # 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 $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" # 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 - Remove-Item -Path $localPath + Remove-Item -Path $isoLocation # Step 10: Display the status of the Azure resource az resource show --ids $AzureResourceGroupUri