Skip to content

Instantly share code, notes, and snippets.

@CodeAngry
Created May 29, 2020 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CodeAngry/63ce4a9769d465b9d54277f8f9d66cc6 to your computer and use it in GitHub Desktop.
Save CodeAngry/63ce4a9769d465b9d54277f8f9d66cc6 to your computer and use it in GitHub Desktop.
Update SQLPackage via Powershell and MSI
# 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