microsoft / winget-cli

WinGet is the Windows Package Manager. This project includes a CLI (Command Line Interface), PowerShell modules, and a COM (Component Object Model) API (Application Programming Interface).
https://learn.microsoft.com/windows/package-manager/
MIT License
23.38k stars 1.46k forks source link

Winget not found - problems with active directory accounts and using winget #4804

Open unclesam87 opened 2 months ago

unclesam87 commented 2 months ago

Brief description of your issue

We like to use Winget to supply computers with complete software. Unfortunately, we are currently unable to install Winget in such a way that it is available to all users. This is a big problem for our admins (they are in a local admin group, but use domain accounts). Whenever they start an administrative cmd, they get an error that Winget cannot be found. This is our current code to install Winget on the devices:

function Install-WinGet {
  param (
    [string]$folder,
    [switch]$Prerelease
  )

  $tempFolderName = 'WinGetInstall'
  $tempFolder = Join-Path -Path $folder -ChildPath $tempFolderName
  New-Item $tempFolder -ItemType Directory -ErrorAction SilentlyContinue | Out-Null

  $apiLatestUrl = if ($Prerelease) { 'https://api.github.com/repos/microsoft/winget-cli/releases?per_page=1' } else { 'https://api.github.com/repos/microsoft/winget-cli/releases/latest' }
  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  $WebClient = New-Object System.Net.WebClient

  function Get-LatestUrl {
    ((Invoke-WebRequest $apiLatestUrl -UseBasicParsing | ConvertFrom-Json).assets | Where-Object { $_.name -match '^Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle$' }).browser_download_url
  }

  function Get-LatestHash {
    $shaUrl = ((Invoke-WebRequest $apiLatestUrl -UseBasicParsing | ConvertFrom-Json).assets | Where-Object { $_.name -match '^Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt$' }).browser_download_url
    $shaFile = Join-Path -Path $tempFolder -ChildPath 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.txt'
    $WebClient.DownloadFile($shaUrl, $shaFile)
    Get-Content $shaFile
  }

  function Get-VersionFromUrl($url) {
    if ($url -match '/releases/download/v([\d\.]+)/') {
      return $matches[1]
    }
    throw "Unable to extract version from URL: $url"
  }

  function Get-LicenseFileUrl($version) {
    $licenseAsset = (Invoke-WebRequest $apiLatestUrl -UseBasicParsing | ConvertFrom-Json).assets | Where-Object { $_.name -match "${version}_License1.xml$" }
    if ($licenseAsset -ne $null) {
      return $licenseAsset.browser_download_url
    }
    throw "License file not found for version $version in the release assets."
  }

  $desktopAppInstaller = @{
    fileName = 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle'
    url      = $(Get-LatestUrl)
    hash     = $(Get-LatestHash)
  }
  $vcLibsUwp = @{
    fileName = 'Microsoft.VCLibs.x64.14.00.Desktop.appx'
    url      = 'https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx'
    hash     = 'B56A9101F706F9D95F815F5B7FA6EFBAC972E86573D378B96A07CFF5540C5961'
  }
  $uiLibsUwp = @{
    fileName = 'Microsoft.UI.Xaml.2.8.x64.appx'
    url      = 'https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx'
    hash     = '249D2AFB41CC009494841372BD6DD2DF46F87386D535DDF8D9F32C97226D2E46'
  }

  $dependencies = @($desktopAppInstaller, $vcLibsUwp, $uiLibsUwp)
  Write-Host '--> Checking dependencies'
  foreach ($dependency in $dependencies) {
    $dependency.file = Join-Path -Path $tempFolder -ChildPath $dependency.fileName
    if (-Not ((Test-Path -Path $dependency.file -PathType Leaf) -And $dependency.hash -eq $(Get-FileHash $dependency.file).Hash)) {
      Write-Host @"
    - Downloading:
      $($dependency.url)
"@
      try {
        $WebClient.DownloadFile($dependency.url, $dependency.file)
      }
      catch {
        # Pass the exception as an inner exception
        throw "Error downloading $($dependency.url)."
      }
      if (-not ($dependency.hash -eq $(Get-FileHash $dependency.file).Hash)) {
        throw "Dependency hash does not match the downloaded file"
      }
    }
  }

  # Extract version from the WinGet URL
  $wingetVersion = Get-VersionFromUrl -url $desktopAppInstaller.url

  # Get the license file URL
  $licenseFileUrl = Get-LicenseFileUrl -version $wingetVersion
  $licenseFilePath = Join-Path -Path $tempFolder -ChildPath 'License1.xml'

  # Download the license file
  Write-Host "--> Downloading license file: $licenseFileUrl"
  try {
    $WebClient.DownloadFile($licenseFileUrl, $licenseFilePath)
  }
  catch {
    throw "Error downloading license file from $licenseFileUrl."
  }

  # Install dependencies
  Write-Host '--> Installing dependencies'
  Add-AppxPackage -Path $($vcLibsUwp.file)
  Add-AppxPackage -Path $($uiLibsUwp.file)

  # Install WinGet
  Write-Host '--> Installing WinGet'
  Add-AppxPackage -Path $($desktopAppInstaller.file)

  # Configure WinGet with the correct license file
  Write-Host '--> Configuring WinGet with the license file'
  Add-AppxProvisionedPackage -Online -PackagePath $($desktopAppInstaller.file) -LicensePath $licenseFilePath
}

If you have any better ideas or if it is a feature that is missing, please mark it - we are grateful for everything! And thanks for all the work on Winget!

Translated with DeepL.com (free version)

Steps to reproduce

install winget and use an different user from that system

Expected behavior

install winget and use it with every user (even ones that comes later to that system as an domain user;))

Actual behavior

winget couldnt be found

Environment

Windows-Paket-Manager v1.8.1911
Copyright (c) Microsoft Corporation. Alle Rechte vorbehalten.

Windows: Windows.Desktop v10.0.22631.4037
Systemarchitektur: X64
Paket: Microsoft.DesktopAppInstaller v1.23.1911.0

WinGet-Verzeichnisse
-----------------------------------------------------------------------------------------------------------------------
Protokolle                                   %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\Local…
Benutzereinstellungen                        %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\Local…
Verzeichnis für portierbare Links (Benutzer) %LOCALAPPDATA%\Microsoft\WinGet\Links
Portables Linkverzeichnis (Computer)         C:\Program Files\WinGet\Links
Portierbarer Paketstamm (Benutzer)           %LOCALAPPDATA%\Microsoft\WinGet\Packages
Portierbarer Paketstamm                      C:\Program Files\WinGet\Packages
Portierbares Paketstamm (x86)                C:\Program Files (x86)\WinGet\Packages
Installationsprogrammdownloads               %USERPROFILE%\Downloads

Links
-----------------------------------------------------------------------------------------
Datenschutzerklärung              https://aka.ms/winget-privacy
Lizenzvereinbarung                https://aka.ms/winget-license
Hinweise von Drittanbietern       https://aka.ms/winget-3rdPartyNotice
Startseite                        https://aka.ms/winget
Windows Store-Nutzungsbedingungen https://www.microsoft.com/en-us/storedocs/terms-of-sale

Administratoreinstellung                  Status
-----------------------------------------------------
LocalManifestFiles                        Deaktiviert
BypassCertificatePinningForMicrosoftStore Deaktiviert
InstallerHashOverride                     Deaktiviert
LocalArchiveMalwareScanOverride           Deaktiviert
ProxyCommandLineOptions                   Deaktiviert
DefaultProxy                              Deaktiviert
github-actions[bot] commented 2 months ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

Trenly commented 2 months ago

@unclesam87 - Have you tried using Add-AppxProvisionedPackage instead? IIRC, Add-AppxPackage only installs it for the current user, but Add-AppxProvisionedPackage will add it for the current user and all new users.

Edit: Whoops, didn’t read far enough; Now I see it at the bottom

stephengillie commented 2 months ago

I'm not sure, but could a Group Policy setting be causing an issue?

denelon commented 2 months ago

We built the Repair-WinGetPackageManager cmdlet in the Microsoft.WinGet.Client PowerShell module to help with bootstrapping WinGet.

Repair-WinGetPackageManager -Latest -force should ensure the latest stable version of WinGet (known to the version of the module) is installed and functioning.

unclesam87 commented 2 months ago

thank u! this function has to be run for every user who tries to run winget right? or can we automate this a bit?

denelon commented 2 months ago

You could leverage the Microsoft.WinGet.Client PowerShell module to automate the process of ensuring the latest stable version of WinGet is installed using the Repair-WinGetPackageManager cmdlet: Repair-WinGetPackageManager -Latest -Force