{"id":7829,"date":"2024-04-12T03:13:51","date_gmt":"2024-04-12T03:13:51","guid":{"rendered":"https:\/\/pariswells.com\/blog\/?p=7829"},"modified":"2024-10-06T11:15:36","modified_gmt":"2024-10-06T11:15:36","slug":"server-2019-2022-winget","status":"publish","type":"post","link":"https:\/\/pariswells.com\/blog\/research\/server-2019-2022-winget","title":{"rendered":"Server 2019 \\ 2022 Winget"},"content":{"rendered":"\n<p><a href=\"https:\/\/github.com\/asheroto\/winget-install\">https:\/\/github.com\/asheroto\/winget-install<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Install-PackageProvider -Name \"NuGet\" -Force\nSet-PSRepository -Name \"PSGallery\" -InstallationPolicy Trusted\nInstall-Script winget-install -Force\nwinget-install\n<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\"># Create WinGet Folder<br>New-Item -Path C:\\WinGet -ItemType directory -ErrorAction SilentlyContinue<br><br># Install VCLibs<br>Invoke-WebRequest -Uri https:\/\/aka.ms\/Microsoft.VCLibs.x64.14.00.Desktop.appx -OutFile \"C:\\WinGet\\Microsoft.VCLibs.x64.14.00.Desktop.appx\"<br>Add-AppxPackage \"C:\\WinGet\\Microsoft.VCLibs.x64.14.00.Desktop.appx\"<br><br># Install Microsoft.UI.Xaml from NuGet<br>Invoke-WebRequest -Uri https:\/\/www.nuget.org\/api\/v2\/package\/Microsoft.UI.Xaml\/2.8.8 -OutFile \"C:\\WinGet\\Microsoft.UI.Xaml.2.8.8.zip\"<br>Expand-Archive \"C:\\WinGet\\Microsoft.UI.Xaml.2.8.8.zip\" -DestinationPath \"C:\\WinGet\\Microsoft.UI.Xaml.2.8.8\"<br>Add-AppxPackage \"C:\\WinGet\\Microsoft.UI.Xaml.2.8.6\\tools\\AppX\\x64\\Release\\Microsoft.UI.Xaml.2.8.appx\"<br><br># Install latest WinGet from GitHub<br>Invoke-WebRequest -Uri https:\/\/github.com\/microsoft\/winget-cli\/releases\/latest\/download\/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle -OutFile \"C:\\WinGet\\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle\"<br>Add-AppxPackage \"C:\\WinGet\\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle\"<br><br># Fix Permissions<br>TAKEOWN \/F \"C:\\Program Files\\WindowsApps\" \/R \/A \/D Y<br>ICACLS \"C:\\Program Files\\WindowsApps\" \/grant Administrators:F \/T<br><br># Add Environment Path<br>$ResolveWingetPath = Resolve-Path \"C:\\Program Files\\WindowsApps\\Microsoft.DesktopAppInstaller_*_x64__8wekyb3d8bbwe\"<br>if ($ResolveWingetPath) {<br>\t$WingetPath = $ResolveWingetPath[-1].Path<br>}<br>$ENV:PATH += \";$WingetPath\"<br>$SystemEnvPath = [System.Environment]::GetEnvironmentVariable('PATH', [System.EnvironmentVariableTarget]::Machine)<br>$SystemEnvPath += \";$WingetPath;\"<br>setx \/M PATH \"$SystemEnvPath\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Write-Information \"This script needs be run on Windows Server 2019 or 2022\"<br><br>If ($PSVersionTable.PSVersion.Major -ge 7){ Write-Error \"This script needs be run by version of PowerShell prior to 7.0\" }<br><br># Define environment variables<br>$downloadDir = \"C:\\WinGet\"<br>$gitRepo = \"microsoft\/winget-cli\"<br>$msiFilenamePattern = \"*.msixbundle\"<br>$licenseFilenamePattern = \"*.xml\"<br>$releasesUri = \"https:\/\/api.github.com\/repos\/$gitRepo\/releases\/latest\"<br><br># Preparing working directory<br>New-Item -Path $downloadDir -ItemType Directory<br>Push-Location $downloadDir<br><br># Downloaing artifacts<br>function Install-Package {<br>    param (<br>        [string]$PackageFamilyName<br>    )<br><br>    Write-Host \"Querying latest $PackageFamilyName version and its dependencies...\"<br>    $response = Invoke-WebRequest `<br>        -Uri \"https:\/\/store.rg-adguard.net\/api\/GetFiles\" `<br>        -Method \"POST\" `<br>        -ContentType \"application\/x-www-form-urlencoded\" `<br>        -Body \"type=PackageFamilyName&amp;url=$PackageFamilyName&amp;ring=RP&amp;lang=en-US\" -UseBasicParsing<br><br>    Write-Host \"Parsing response...\"<br>    $regex = '&lt;td&gt;&lt;a href=\\\"([^\\\"]*)\\\"[^\\&gt;]*\\&gt;([^\\&lt;]*)&lt;\\\/a&gt;'<br>    $packages = (Select-String $regex -InputObject $response -AllMatches).Matches.Groups<br><br>    $result = $true<br>    for ($i = $packages.Count - 1; $i -ge 0; $i -= 3) {<br>        $url = $packages[$i - 1].Value;<br>        $name = $packages[$i].Value;<br>        $extCheck = @(\".appx\", \".appxbundle\", \".msix\", \".msixbundle\") | % { $x = $false } { $x = $x -or $name.EndsWith($_) } { $x }<br>        $archCheck = @(\"x64\", \"neutral\") | % { $x = $false } { $x = $x -or $name.Contains(\"_$($_)_\") } { $x }<br><br>        if ($extCheck -and $archCheck) {<br>            # Skip if package already exists on system<br>            $currentPackageFamilyName = (Select-String \"^[^_]+\" -InputObject $name).Matches.Value<br>            $installedVersion = (Get-AppxPackage \"$currentPackageFamilyName*\").Version<br>            $latestVersion = (Select-String \"_(\\d+\\.\\d+.\\d+.\\d+)_\" -InputObject $name).Matches.Value<br>            if ($installedVersion -and ($installedVersion -ge $latestVersion)) {<br>                Write-Host \"${currentPackageFamilyName} is already installed, skipping...\" -ForegroundColor \"Yellow\"<br>                continue<br>            }<br><br>            try {<br>                Write-Host \"Downloading package: $name\"<br>                $tempPath = \"$(Get-Location)\\$name\"<br>                Invoke-WebRequest -Uri $url -Method Get -OutFile $tempPath<br>                Add-AppxPackage -Path $tempPath<br>                Write-Host \"Successfully installed:\" $name<br>            } catch {<br>                $result = $false<br>            }<br>        }<br>    }<br><br>    return $result<br>}<br><br>Write-Host \"`n\"<br><br>function Install-Package-With-Retry {<br>    param (<br>        [string]$PackageFamilyName,<br>        [int]$RetryCount<br>    )<br><br>    for ($t = 0; $t -le $RetryCount; $t++) {<br>        Write-Host \"Attempt $($t + 1) out of $RetryCount...\" -ForegroundColor \"Cyan\"<br>        if (Install-Package $PackageFamilyName) {<br>            return $true<br>        }<br>    }<br><br>    return $false<br>}<br><br>$licenseDownloadUri = ((Invoke-RestMethod -Method GET -Uri $releasesUri).assets | Where-Object name -like $licenseFilenamePattern ).browser_download_url<br>$licenseFilename = ((Invoke-RestMethod -Method GET -Uri $releasesUri).assets | Where-Object name -like $licenseFilenamePattern ).name<br>$licenseJoinPath = Join-Path -Path $downloadDir -ChildPath $licenseFilename<br>Invoke-WebRequest -Uri $licenseDownloadUri -OutFile ( New-Item -Path $licenseJoinPath -Force )<br><br>$result = @(\"Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\") | ForEach-Object { $x = $true } { $x = $x -and (Install-Package-With-Retry $_ 3) } { $x }<br><br>$msiFilename = ((Get-ChildItem -Path $downloadDir) | Where-Object name -like $msiFilenamePattern ).name<br>$msiJoinPath = Join-Path -Path $downloadDir -ChildPath $msiFilename<br><br># Installing winget<br>Add-ProvisionedAppPackage -Online -PackagePath $msiJoinPath -LicensePath $licenseJoinPath -Verbose<br><br>Write-Host \"`n\"<br><br># Test if winget has been successfully installed<br>if ($result -and (Test-Path -Path \"$env:LOCALAPPDATA\\Microsoft\\WindowsApps\\winget.exe\")) {<br>    Write-Host \"Congratulations! Windows Package Manager (winget) $(winget --version) installed successfully\" -ForegroundColor \"Green\"<br>} else {<br>    Write-Host \"Oops... Failed to install Windows Package Manager (winget)\" -ForegroundColor \"Red\"<br>}<br><br># Cleanup<br>Push-Location $HOME<br>Remove-Item $downloadDir -Recurse -Force<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/github.com\/asheroto\/winget-install<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7829","post","type-post","status-publish","format-standard","hentry","category-research"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/7829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/comments?post=7829"}],"version-history":[{"count":6,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/7829\/revisions"}],"predecessor-version":[{"id":8335,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/posts\/7829\/revisions\/8335"}],"wp:attachment":[{"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/media?parent=7829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/categories?post=7829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pariswells.com\/blog\/wp-json\/wp\/v2\/tags?post=7829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}