Skip to content

Instantly share code, notes, and snippets.

@emmaly
Last active January 26, 2023 09:30
Show Gist options
  • Save emmaly/aa6d6bb3cf82ec4e98dbf73f63a24742 to your computer and use it in GitHub Desktop.
Save emmaly/aa6d6bb3cf82ec4e98dbf73f63a24742 to your computer and use it in GitHub Desktop.
Install WinGet-CLI on Windows Server
function Get-FilesFromGithubLatestRelease {
# original function from https://github.com/microsoft/winget-cli/issues/1861#issuecomment-1193136622
param (
[parameter(Mandatory)][string]$Project, # e.g. paintdotnet/release
[parameter(Mandatory)][string[]]$Patterns, # regex
[switch]$Prerelease
)
$releases = Invoke-RestMethod -Method Get -Uri "https://api.github.com/repos/$Project/releases"
$release = $releases | Where-Object { $_.prerelease -eq $Prerelease } | Select-Object -First 1
return $Patterns | ForEach-Object {
$pattern = $_
$release | ForEach-Object { $_.assets } | Where-Object { $_.name -match $pattern } | Select-Object -First 1 -ExpandProperty "browser_download_url"
}
}
function Install-PackageMsVcLibsDesktop {
$PackageURL = "https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx"
$WorkingDir = New-Item -Path $(Join-Path $env:TEMP $((New-Guid).Guid)) -ItemType Directory
$PackagePath = Join-Path $WorkingDir $(Split-Path $PackageURL -Leaf)
Invoke-WebRequest -Uri $PackageURL -OutFile $PackagePath
Add-AppxPackage -Path $PackagePath -Verbose
Remove-Item -Path $PackagePath
Remove-Item -Path $WorkingDir
}
function Install-PackageMsUiXaml {
$ArchiveURL = "https://www.nuget.org/api/v2/package/Microsoft.UI.Xaml/2.7.3"
$WorkingDir = New-Item -Path $(Join-Path $env:TEMP $((New-Guid).Guid)) -ItemType Directory
$ArchivePath = Join-Path $WorkingDir "Microsoft.UI.Xaml.$(Split-Path $ArchiveURL -Leaf).zip"
$ExtractPath = Join-Path $WorkingDir "Microsoft.UI.Xaml.$(Split-Path $ArchiveURL -Leaf).extracted"
$PackagePath = Join-Path $ExtractPath "tools/AppX/x64/Release/Microsoft.UI.Xaml.2.7.appx"
Invoke-WebRequest -Uri $ArchiveURL -OutFile $ArchivePath
Expand-Archive -Path $ArchivePath -Destination $ExtractPath
Add-AppxPackage -Path $PackagePath -Verbose
Remove-Item -Path $ExtractPath -Recurse
Remove-Item -Path $ArchivePath
Remove-Item -Path $WorkingDir
}
function Install-PackageMsDesktopAppInstaller {
$FileURLs = Get-FilesFromGithubLatestRelease -Project microsoft/winget-cli -Patterns '_License1\.xml$','^Microsoft\.DesktopAppInstaller.+\.msixbundle$'
if ($null -eq $FileURLs -or $FileURLs.Count -ne 2) {
Write-Error "Unable to find expected files from GitHub release" -ErrorAction Stop
}
$LicenseURL = $FileURLs[0]
$PackageURL = $FileURLs[1]
$WorkingDir = New-Item -Path $(Join-Path $env:TEMP $((New-Guid).Guid)) -ItemType Directory
$LicensePath = Join-Path $WorkingDir $(Split-Path $LicenseURL -Leaf)
$PackagePath = Join-Path $WorkingDir $(Split-Path $PackageURL -Leaf)
Invoke-WebRequest -Uri $LicenseURL -OutFile $LicensePath
Invoke-WebRequest -Uri $PackageURL -OutFile $PackagePath
Add-AppxProvisionedPackage -Online -PackagePath $PackagePath -LicensePath $LicensePath -Verbose
Remove-Item -Path $LicensePath
Remove-Item -Path $PackagePath
Remove-Item -Path $WorkingDir
}
function Install-WinGetCli {
Install-PackageMsVcLibsDesktop
Install-PackageMsUiXaml
Install-PackageMsDesktopAppInstaller
if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
Write-Host "winget doesn't appear to have been successfully installed, or it wasn't found yet; perhaps a session restart?"
return 1
}
Write-Host "Now try something like this, with admin privileges:"
Write-Host "winget install vscode --scope machine --accept-package-agreements --accept-source-agreements --silent"
return 0
}
$ExitCode = Install-WinGetCli
exit $ExitCode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment