functions/Get/Get-FileFromGithub.ps1
function Get-FileFromGithub { Param( [Parameter(Mandatory = $false)] [string]$Owner = 'lineupsystems', [Parameter(Mandatory = $true)] [string]$Repository, [Parameter(Mandatory = $true, ParameterSetName = 'OutFile')] [Parameter(Mandatory = $true, ParameterSetName = 'FileString')] [AllowEmptyString()] [string]$FileName, [Parameter(Mandatory = $true, ParameterSetName = 'OutFile')] [string]$OutFile, [Parameter(Mandatory = $false)] [string]$Ref ) begin { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $baseUri = "https://api.github.com"; $headers = Get-GithubHeaders # If Ref is not passed then load it from customer config $Ref = Get-DefaultBranch -Ref $Ref } process { # REST Building $argsUri = "repos/$Owner/$Repository/contents/$($FileName)?ref=$Ref" $response = Invoke-WebRequest -Uri ("$baseUri/$argsUri") -Headers $headers -UseBasicParsing -ErrorAction Stop # Data Handler $objects = $response.Content | ConvertFrom-Json $files = $objects | Where-Object { $_.type -eq "file" } | Select-Object -exp download_url if($files -is [array]){ $file = $files[0] } else{ $file = $files } if($PSBoundParameters.ContainsKey('OutFile')){ $targetDir = Split-Path $OutFile if (-not (Test-Path $targetDir)) { New-Item -Path $targetDir -ItemType Directory -ErrorAction Stop | Out-Null } Invoke-WebRequest -Uri $file -OutFile $OutFile -ErrorAction Stop } else{ Invoke-WebRequest -Uri $file -ErrorAction Stop | Select-Object -Expand Content } } } |