src/private/Find-WinGetPackage.ps1
function Find-WinGetPackage { param ( [Parameter(Mandatory=$true)] [string] $Name, [string] $RequiredVersion, [string] $MinimumVersion, [string] $MaximumVersion ) # Throw an error if provided version arguments don't make sense Confirm-VersionParameters -Name $Name -MinimumVersion $MinimumVersion -MaximumVersion $MaximumVersion -RequiredVersion $RequiredVersion $options = $request.Options foreach( $o in $options.Keys ) { Write-Debug ( "OPTION: {0} => {1}" -f ($o, $options[$o]) ) } [array]$RegisteredPackageSources = Get-PackageSources if ($options -and $options.ContainsKey('Source')) { # Finding the matched package sources from the registered ones Write-Verbose ($LocalizedData.SpecifiedSourceName -f ($options['Source'])) if ($RegisteredPackageSources.Name -eq $options['Source']) { # Found the matched registered source $selectedSource = $options['Source'] } else { ThrowError -ExceptionName 'System.ArgumentException' ` -ExceptionMessage ($LocalizedData.PackageSourceNotFound -f ($options['Source'])) ` -ErrorId 'PackageSourceNotFound' ` -ErrorCategory InvalidArgument ` -ExceptionObject $options['Source'] } } else { # User did not specify a source. Now what? if ($RegisteredPackageSources.Count -eq 1) { # If no source name is specified and only one source is available, use it $selectedSource = $RegisteredPackageSources[0].Name } elseif ($RegisteredPackageSources.Name -eq $script:PackageSourceName) { # If multiple sources are avaiable but none specified, default to using WinGet.org - if present $selectedSource = $script:PackageSourceName } else { # If WinGetately.org is not present and no source specified, throw an exception ThrowError -ExceptionName 'System.ArgumentException' ` -ExceptionMessage $LocalizedData.UnspecifiedSource ` -ErrorId 'UnspecifiedSource' ` -ErrorCategory InvalidArgument } } Write-Verbose "Source selected: $selectedSource" $WinGetParams = @{ Search = $true Package = $name SourceName = $selectedSource } # WinGet does not support searching by min or max version, so if a user is picky we'll need to pull back the latest and see if it meets the requirements further down if ($requiredVersion) { $WinGetParams.Add('Version',$requiredVersion) } # Return the result without additional evaluation, even if empty, to let PackageManagement handle error management # Will only terminate if Invoke-WinGet fails to call WinGet.exe Invoke-WinGet @WinGetParams | Where-Object {Test-PackageVersion -Package $_ -RequiredVersion $RequiredVersion -MinimumVersion $MinimumVersion -MaximumVersion $MaximumVersion} } |