Skip to content

Instantly share code, notes, and snippets.

@scriptingstudio
Last active May 27, 2023 12:52
Show Gist options
  • Save scriptingstudio/ae5dba993f194728ed818df569031272 to your computer and use it in GitHub Desktop.
Save scriptingstudio/ae5dba993f194728ed818df569031272 to your computer and use it in GitHub Desktop.
Windows Terminal installation script for Windows Server 2022
param (
[alias('update','install','apply')]
[switch]$run, # action mode
[switch]$force # skips edition limitation
)
If ($PSVersionTable.PSVersion.Major -ge 7) {
Write-Error 'This script needs be run by version of PowerShell prior to 7.0'
}
# check Windows edition
$wininfo = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
if ($wininfo.ProductName -notmatch 'Server 2022') {
Write-Warning 'This script is primarily intendent for Windows Server 2022.'
if (-not $force) {return}
}
$downloadDir = $env:temp
$framworkPkgUrl = 'https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx'
$framworkPkg = "$downloadDir\Microsoft.VCLibs.x64.14.00.Desktop.appx"
$xamlPkg = "$downloadDir\microsoft.ui.xaml.2.7.3.zip"
$xamlUrl = 'https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.3'
$msixbundle = "$downloadDir\Microsoft.WindowsTerminal.msixbundle"
$releasesUri = 'https://api.github.com/repos/microsoft/terminal/releases/latest'
$progressPreference = 'silentlyContinue'
$downloadUri = ((Invoke-RestMethod -Method GET -Uri $releasesUri).assets | Where-Object name -like "*.msixbundle").browser_download_url #| Select-Object -First 1
if (-not $downloadUri) {
Write-Warning "Retrieve WT download url error occurred."
return
}
# Get prerequisites status
$packages = Get-AppxPackage
$uixaml = $packages.Where{$_.name -match 'Microsoft\.UI\.Xaml\.2\.7'}
$vclibstatus = $packages.Where{$_.name -match 'Microsoft\.VCLibs\.140\.00\.UWPDesktop'}
$wtver = $packages.Where{$_.name -match 'Windowsterminal'}.version
$storever = if ($downloadUri -match '/v([\d.]+)/') {$matches[1]}
if ($run) {
# Start installation/update
if ($wtver) {
if ([version]$storever -le [version]$wtver) {
Write-Host 'No update is required.'
return
}
}
Write-Host 'Downloading packages...'
#Start-BitsTransfer -Source $framworkPkgUrl -Destination $framworkPkg
#Start-BitsTransfer -Source $downloadUri -Destination $msixbundle
if (-not $vclibstatus) {
Write-Host ' VCLibs package'
Invoke-WebRequest -Uri $framworkPkgUrl -OutFile $framworkPkg
}
if (-not $uixaml) {
Write-Host ' Xaml package'
Invoke-WebRequest -Uri $xamlUrl -OutFile $xamlPkg
}
Write-Host ' WT package'
Invoke-WebRequest -Uri $downloadUri -OutFile $msixbundle
Write-Host 'Installing packages...'
if (-not $uixaml) {
Write-Host ' Xaml package'
Expand-Archive $xamlpkg -DestinationPath "$downloadDir\xamlpkg" -Force #-ErrorAction Stop
Add-AppxPackage -Path "$downloadDir\xamlpkg\tools\AppX\x64\Release\Microsoft.UI.Xaml.2.7.appx"
}
if (-not $vclibstatus) {
Write-Host ' VCLibs package'
Add-AppxPackage -Path $framworkPkg
}
$vclibstatus = (Get-AppxPackage).Where{$_.name -match 'Microsoft\.VCLibs\.140\.00\.UWPDesktop'}
if ($vclibstatus) {
Write-Host ' WT package'
Add-AppxPackage -Path $msixbundle
} else {
Write-Warning 'Could not install Microsoft.VCLibs.x64.14.00.'
}
# Cleanup
Remove-Item $framworkPkg -Force -ErrorAction 0
Remove-Item $msixbundle -Force -ErrorAction 0
Remove-Item "$downloadDir\xamlpkg" -Force -Recurse -ErrorAction 0
Write-Host 'Done.'
}
else { # view mode
$installed = 'Installed (required)'
$notinstalled = 'Not installed (required)'
[pscustomobject]@{
InstalledVersion = if ($wtver) {$wtver} else {'Not installed'}
LatestVersion = $storever
WTDownloadUri = $downloadUri
VCLibs = if ($vclibstatus) {$installed} else {$notinstalled}
UIXamlPackage = if ($uixaml) {$installed} else {$notinstalled}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment