Update SQLPackage via Powershell and MSI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wait before exiting script or force update? | |
# .\script.ps1 -force 0 -wait 1 | |
param ([bool] $wait = $false, [bool] $force = $false) | |
$global:ErrorActionPreference = 'SilentlyContinue' | |
$global:ProgressPreference = 'SilentlyContinue' | |
Clear-Host | |
Write-Host "**************************************************************" | |
Write-Host "****** Dumb SQLPackage PowerShell Updater by @CodeAngry ******" | |
Write-Host "**************************************************************" | |
Write-Host "https://docs.microsoft.com/en-us/sql/tools/sqlpackage-download" | |
Write-Host | |
Push-Location $PSScriptRoot | |
$oldVersion = $null | |
if ($force -eq $false) { | |
$dac = "C:\Program Files\Microsoft SQL Server" | |
if (Test-Path $dac -PathType Container) { | |
# $exes = $(Get-ChildItem -File -Path $dac -Filter "sqlpackage.exe" -Recurse) | |
$dirs = $(Get-ChildItem -Directory -Path $dac) | Where-Object { $_.Name -match '^\d+$' } | |
$exes = @($dirs | ForEach-Object { Get-Item "$($_.FullName)\DAC\bin\sqlpackage.exe" }) | |
$exes = @($exes | Where-Object { Test-Path $_ -PathType leaf }) | |
$exes = @($exes | Sort-Object { $_.VersionInfo.FileVersion.Trim() } -Descending) | |
if ($exes.Length -ge 1) { | |
$exe = $exes[0] | |
$oldVersion = $exe.VersionInfo.FileVersion.Trim() | |
Write-Host "Current Runtime:" $exe.FullName | |
Write-Host "Current Version:" $oldVersion | |
Write-Host | |
} | |
} | |
} | |
Write-Host "Locating DacFramework.msi Link." | |
$html = Invoke-WebRequest -Uri "https://docs.microsoft.com/en-us/sql/tools/sqlpackage-download" | |
$update = $false | |
if ($null -ne $oldVersion) { | |
$newVersion = @($html.ParsedHtml.body.getElementsByTagName('TD') | Where-Object { $_.innerText -match '\d+\.\d+\.\d+\.\d+' })[0].innerText.Trim() | |
Write-Host 'Old Version:' $oldVersion | |
Write-Host 'New Version:' $newVersion | |
if ([version]$newVersion -gt [version]$oldVersion) { | |
$update = $true | |
} | |
} | |
else { | |
$update = $true | |
} | |
if ($update -eq $false) { | |
Write-Host "DacFramework.msi update not required." | |
} | |
else { | |
$links = @($html.ParsedHtml.body.getElementsByTagName('A') | Where-Object { $_.innerText.Trim() -eq 'DacFramework.msi Installer for Windows' }) | |
if ($links.length -ne 1) { | |
Write-Host "DacFramework.msi not found." | |
} | |
else { | |
$linkMsi = $links[0] | |
$msi = $linkMsi.href | |
Write-Host $msi | |
Remove-Item -LiteralPath "sqlpackage.msi" | |
Write-Host "Downloading the DacFramework.msi." | |
Invoke-WebRequest $msi -OutFile "sqlpackage.msi" | |
Write-Host "Executing the DacFramework.msi." | |
# Start-Process -FilePath "sqlpackage.msi" -ArgumentList "/quiet /qn /passive /norestart" -Wait | |
Start-Process -FilePath "sqlpackage.msi" -ArgumentList "/quiet /qn /norestart" -Wait | |
Remove-Item -LiteralPath "sqlpackage.msi" | |
Write-Host "DacFramework.msi Installer completed." | |
} | |
} | |
Pop-Location | |
if ($wait -eq $true) { | |
Write-Host | |
Write-Host | |
Write-Host "(press any key to exit)" | |
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment