public/Install-Package.ps1
# Installs packages based on the provided Fast Package Reference generated by Find-Package function Install-Package { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidOverwritingBuiltInCmdlets', '', Justification='Required by PackageManagement')] [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string] $FastPackageReference ) Write-Debug -Message ($LocalizedData.ProviderDebugMessage -f ('Install-Package')) Write-Debug -Message ($LocalizedData.FastPackageReference -f $FastPackageReference) # If the fast package reference doesnt match the pattern we expect, throw an exception if ((-Not ($FastPackageReference -Match $script:FastReferenceRegex)) -Or (-Not ($Matches.name -And $Matches.version))) { ThrowError -ExceptionName "System.ArgumentException" ` -ExceptionMessage ($LocalizedData.FailToInstall -f $FastPackageReference) ` -ErrorId 'FailToInstall' ` -ErrorCategory InvalidArgument } $shouldContinueQueryMessage = ($LocalizedData.InstallPackageQuery -f "Installing", $Matches.name) $shouldContinueCaption = $LocalizedData.InstallPackageCaption # If the user opts not to install the package, exit from the script if (-Not ((Get-PromptBypass) -Or $request.ShouldContinue($shouldContinueQueryMessage, $shouldContinueCaption))) { Write-Warning ($LocalizedData.NotInstalled -f $FastPackageReference) return } $WinGetParams = @{ ID = $Matches.name Version = $Matches.version Source = $Matches.source } # Convert the PSCustomObject output from Cobalt into PackageManagement SWIDs, then validate what WinGet installed matched what we requested $swid = $( $result = Cobalt\Install-WinGetPackage @WinGetParams # If Cobalt didn't return anything, something went wrong and we need to throw our own exception if (-Not $result) { ThrowError -ExceptionName 'System.OperationCanceledException' ` -ExceptionMessage $LocalizedData.WinGetFailure ` -ErrorID 'JobFailure' ` -ErrorCategory InvalidOperation ` } ConvertTo-SoftwareIdentity -InputObject $result -Source $WinGetParams.Source ) | Where-Object {Test-PackageVersion -Package $_ -RequiredVersion $WinGetParams.version -ErrorAction SilentlyContinue} if (-Not $swid) { # Cobalt returned something, but not in the format we expected. Something is amiss. Write-Warning ($LocalizedData.UnexpectedWinGetResponse -f $FastPackageReference) } $swid } |