Advertisement
atheos42

Get-LatestGitHub

Jul 23rd, 2022 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using namespace System.Management.Automation.Host
  2.  
  3. Clear-Host
  4.  
  5. function Get-LatestGitHubVersion{
  6. <#
  7.     .SYNOPSIS
  8.         A function to get the Latest Stable Version of a GitHub repository.
  9.     .DESCRIPTION
  10.         This function web scrapes a GitHub repository and
  11.         parses out the Latest Stable Version.
  12.         This function will return the latest version as a string.
  13.          
  14.     .PARAMETER repos
  15.         Repository.
  16.  
  17.     .EXAMPLE
  18.         Get-LatestGitHubVersion 'powershell/powershell'
  19.         Get-LatestGitHubVersion 'audacity/audacity'
  20. #>
  21.     Param ( [Parameter(Mandatory=1)][string]$repos
  22.     )
  23.     $url = "https://api.github.com/repos/$repos/releases/latest"
  24.     try{
  25.         $tag = Invoke-RestMethod $url
  26.         return $tag.tag_name
  27.     }catch{
  28.         $_.exception | Write-Warning
  29.         return $null
  30.     }
  31. }
  32.  
  33. function Download-LatestGitHubVersion{
  34. <#
  35.     .SYNOPSIS
  36.         A function to download the Lastest Stable Version of a GitHub repository.
  37.     .DESCRIPTION
  38.         This function web scrapes a GitHub repository and downloads
  39.         the latest stable version.  The download will be saved to Users Downloads folder.
  40.         This function will return the full path string of the downloaded file.
  41.  
  42.     .PARAMETER repos
  43.         Repository.
  44.     .PARAMETER filter
  45.         Filter string for Downloadable file.
  46.  
  47.     .EXAMPLE
  48.         Download-LatestGitHubVersion 'powershell/powershell' '*win*x64*.msi'
  49.         Download-LatestGitHubVersion 'audacity/audacity' '*64*.exe'
  50. #>
  51.     Param ( [Parameter(Mandatory=1)][string]$repos,
  52.             [Parameter(Mandatory=1)][string]$filter
  53.     )
  54.     $url = "https://api.github.com/repos/$repos/releases/latest"
  55.     try{
  56.         $tag = Invoke-RestMethod $url
  57.         $latest = $tag.assets | Where-Object { $_.name -like $filter }
  58.         $file = "$HOME\Downloads\$($latest.name)"
  59.         Write-Host $('Dowloading latest release to: ' + $file)
  60.         Invoke-WebRequest $latest.browser_download_url -Out $file
  61.         return $file
  62.     }catch{
  63.         $_.exception | Write-Warning
  64.         return $null
  65.     }
  66. }
  67.  
  68. Get-LatestGitHubVersion 'powershell/powershell'
  69. Get-LatestGitHubVersion 'audacity/audacity'
  70. Get-LatestGitHubVersion 'microsoft/typescript'
  71. Get-LatestGitHubVersion 'notepad-plus-plus/notepad-plus-plus'
  72.  
  73. Download-LatestGitHubVersion 'powershell/powershell' '*win*x64*.msi'
  74. Download-LatestGitHubVersion 'audacity/audacity' '*64*.exe'
  75. Download-LatestGitHubVersion 'notepad-plus-plus/notepad-plus-plus' '*x64*.exe'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement