Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active October 23, 2023 13:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/40678a76079a8bb775d9db5f1128f388 to your computer and use it in GitHub Desktop.
Save stknohg/40678a76079a8bb775d9db5f1128f388 to your computer and use it in GitHub Desktop.
Windows Server 2022にWindows Terminal, WinGetをインストールする関数
function Install-LatestWindowsTerminal ([switch]$SkipInstallVCLibs) {
# Check OS version
$buildVersion = [Version](Get-CimInstance -ClassName 'Win32_OperatingSystem' -Property Version | Select-Object -ExpandProperty Version)
$isWin11 = if ($buildVersion -ge ([Version]"10.0.22000.0") ) { $true } else { $false }
# Get the latest release information
$latest = Invoke-RestMethod -Uri 'https://api.github.com/repos/microsoft/terminal/releases/latest'
$installerUrl = $latest.assets | Where-Object { $_.browser_download_url -like '*.msixbundle_Windows10_PreinstallKit.zip' } | Select-Object -ExpandProperty browser_download_url -First 1
if ([string]::IsNullOrEmpty($installerUrl)) {
Write-Error 'Failed to get the installer url'
return
}
# For Windows 10, Need to install VCLibs
if ((-not $isWin11) -and (-not $SkipInstallVCLibs)) {
$vcLibsUrl = 'https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx'
Write-Host -ForegroundColor Green "Download $vcLibsUrl"
Add-AppxPackage -Path $vcLibsUrl
}
# Download zip and expand items
Write-Host -ForegroundColor Green "Download $installerUrl"
$zipPath = Join-Path -Path ([IO.Path]::GetTempPath()) ($installerUrl -split '/')[-1]
try {
$client = [System.Net.WebClient]::new()
$client.DownloadFile($installerUrl, $zipPath)
} finally {
$client.Dispose()
}
Add-Type -AssemblyName System.IO.Compression.FileSystem
$xamlUIPath = ''
$bundlePath = ''
try {
$archive = [System.IO.Compression.ZipFile]::OpenRead($zipPath)
# Extract Microsoft.UI.Xaml appx
$archName = switch ($env:PROCESSOR_ARCHITECTURE) {
'x86' { 'x86' }
'AMD64' { 'x64' }
'ARM64' { 'arm64' }
Default { 'x64' }
}
$item = $archive.Entries | Where-Object { $_.FullName -like "Microsoft.UI.Xaml*_${archName}*.appx" } | Select-Object -First 1
if ($item) {
$xamlUIPath = Join-Path -Path ([IO.Path]::GetTempPath()) ($item.Name)
Write-Host -ForegroundColor Green "Extract $($item.Name) to $xamlUIPath"
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($item, $xamlUIPath)
}
# Extract msixbundle
$item = $archive.Entries | Where-Object { $_.FullName -like '*.msixbundle' } | Select-Object -First 1
if ($item) {
$bundlePath = Join-Path -Path ([IO.Path]::GetTempPath()) ($item.Name)
Write-Host -ForegroundColor Green "Extract $($item.Name) to $bundlePath"
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($item, $bundlePath)
}
} finally {
if ($archive) {
$archive.Dispose()
}
}
# Install Microsoft.UI.Xaml appx
if (-not $isWin11) {
Write-Host -ForegroundColor Green "Install $xamlUIPath"
Add-AppxPackage -Path $xamlUIPath
}
# Install MSIX bundle
Write-Host -ForegroundColor Green "Install $bundlePath"
Add-AppxPackage -Path $bundlePath
# Remove files
($bundlePath, $xamlUIPath, $zipPath) | ForEach-Object {
if (Test-Path -LiteralPath $_) {
Write-Host -ForegroundColor Green "Remove $_"
Remove-Item -LiteralPath $_
}
}
Write-Host -ForegroundColor Green "Installation complete!"
}
function Install-LatestWinGet ([switch]$SkipInstallVCLibs) {
# Install prerequisites
if (-not $SkipInstallVCLibs) {
$vcLibsUrl = 'https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx'
Write-Host -ForegroundColor Green "Download $vcLibsUrl"
Add-AppxPackage -Path $vcLibsUrl
}
# Find the latest assets url
$latest = Invoke-RestMethod -Uri 'https://api.github.com/repos/microsoft/winget-cli/releases/latest'
$msixUrl = $latest.assets | Where-Object { $_.browser_download_url -like '*.msixbundle' } | Select-Object -ExpandProperty browser_download_url
$msixPath = Join-Path -Path ([IO.Path]::GetTempPath()) ($msixUrl -split '/')[-1]
$licenseUrl = $latest.assets | Where-Object { $_.browser_download_url -like '*License1.xml' } | Select-Object -ExpandProperty browser_download_url
$licensePath = Join-Path -Path ([IO.Path]::GetTempPath()) ($licenseUrl -split '/')[-1]
# Download assets
( @{Url = $msixUrl ; LocalPath = $msixPath }, @{Url = $licenseUrl ; LocalPath = $licensePath } ) | ForEach-Object {
Write-Host -ForegroundColor Green "Download $($_.Url)"
try {
$client = [System.Net.WebClient]::new()
$client.DownloadFile($_.Url, $_.LocalPath)
} finally {
$client.Dispose()
}
}
# Install MSIX to provisioned package
Write-Host -ForegroundColor Green "Install $msixPath"
Write-Host -ForegroundColor Green " : License $licensePath"
Add-AppxProvisionedPackage -Online -PackagePath $msixPath -LicensePath $licensePath
# Install Microsoft.UI.Xaml 2.7.3 (need fixed version)
Write-Host -ForegroundColor Green "Install Microsoft.UI.Xaml 2.7.3"
$archName = switch ($env:PROCESSOR_ARCHITECTURE) {
'x86' { 'x86' }
'AMD64' { 'x64' }
'ARM64' { 'arm64' }
Default { 'x64' }
}
Add-AppxPackage -Path "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.7.3/Microsoft.UI.Xaml.2.7.${archName}.appx"
# Register MSIX
Write-Host -ForegroundColor Green "Registter Microsoft.DesktopAppInstaller"
Add-AppxPackage -RegisterByFamilyName -MainPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe
# Remove assets
($msixPath, $licensePath) | ForEach-Object {
if (Test-Path -LiteralPath $_) {
Write-Host -ForegroundColor Green "Remove $_"
Remove-Item -LiteralPath $_
}
}
Write-Host -ForegroundColor Green "Installation complete!"
}
@stknohg
Copy link
Author

stknohg commented Sep 26, 2021

実行例

Install-LatestWindowsTerminal
Install-LatestWinGet

@stknohg
Copy link
Author

stknohg commented Jun 24, 2022

Windows Terminalに関して、2022年6月現在の最新バージョンだと

  • Windows 11とWindows 10でmsixbundleが分かれた
  • Windows 10にはVC Runtimeの事前インストールが必要になった
    ため処理を更新しています。

@stknohg
Copy link
Author

stknohg commented Oct 23, 2023

Windows Terminalに関して、2023年10月現在の状況として

  • MSIX bundleは再度統合された
  • Windows 10ではVC Runtimeに加え、Microsoft.Xaml.UIへの依存が増えた

という状況になり関数を見直しています。

また、WinGetに関して Microsoft.Xaml.UI 2.7 へのバージョン固定の依存関係が増えているためこちらも処理を見直しています。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment