function Get-PackageFileV2 { <# .SYNOPSIS Downloads a specific file from proget and returns it. .PARAMETER FeedSource [string] Source feed used to look up the package by .PARAMETER Name [string] Package name to lookup .PARAMETER Version [string] Package version to lookup .PARAMETER PackagePath [string] Path in the proget folder for the file to retrieve the contents from .PARAMETER Credential [PSCredential] Credential used for talking to feeds as needed #> [CmdletBinding(DefaultParameterSetName = 'RawArgs')] [OutputType([string])] Param( [Parameter(Mandatory = $true, ParameterSetName = 'RawArgs')] [string]$FeedSource, [Parameter(Mandatory = $true, ParameterSetName = 'RawArgs')] [string]$Name, [Parameter(Mandatory = $true, ParameterSetName = 'RawArgs')] [string]$Version, [Parameter(Mandatory = $true, ParameterSetName = 'Package')] [object]$Package, [Parameter(Mandatory = $true)] [string]$PackagePath, [Parameter(Mandatory = $false)] [PSCredential]$Credential = $null ) ## TODO: Can this pull from the local filesystem if it exists? ## This would let us fetch faster if the versions match as we could avoid network hops. $loglead = (Get-LogLeadName) if ($PSCmdlet.ParameterSetName -eq 'Package') { $FeedSource = $Package.Feed.Source $Name = $Package.Name $Version = $Package.Version } # Replace package path back slashes with forward slashes, to play nicely in the URL. $packagePath = $packagePath.Replace("\", "/") # Make sure the input feed URL is a nuget feed. $feedUri = $null try { $feedUri = [System.Uri]::new($FeedSource) if ($feedUri.Segments.Count -lt 3) { throw "$logLead : FeedSource [$FeedSource] parameter for package [$Name] should have at least three url segments (host, nuget, feed name) and does not have enough feed segments provided." } $segmentCompare = 'nuget/' if ($feedUri.Segments[1] -ne $segmentCompare) { throw "$logLead : FeedSource [$FeedSource] parameter for package [$Name] should use the url segment [$segmentCompare], not [$($feedUri.Segments[1])]." } } catch { Write-Error "$logLead : FeedSource [$FeedSource] parameter for package [$Name] was supplied incorrectly. Expected url pattern should look like: [https://feed.com/nuget/feed.name]" Write-Error $_.Exception.Message return } # we now have a valid feedUri, it has the right segment count, and we can infer otherwise as needed $feedName = $feedUri.Segments[2].TrimEnd('/') $baseUrl = $feedUri.GetComponents([System.UriComponents]::SchemeAndServer, [System.UriFormat]::SafeUnescaped) $downloadUrl = "$baseUrl/package-files/download?packageId=$Name&version=$Version&feedName=$feedName&path=$packagePath" # If a PSCredential is specified build a basic authentication header. $headers = (Get-BasicAuthHeader -Credential $Credential) try { Write-Host "$loglead : Querying for package files at endpoint: $downloadUrl" $response = Invoke-ProgetRequest -URI $downloadUrl -Headers $headers # StreamReader interprets byte order marks and skips it, if it exists. $result = [System.IO.StreamReader]::new($response.RawContentStream).ReadToEnd() Write-Host "$loglead : Done Querying for package files at endpoint: $downloadUrl" return $result } catch { Write-Host $_.Exception.Message return $null } }