Advertisement
atheos42

Get-LatestPS

Jul 23rd, 2022
57
1
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-LatestPSVersion{
  6. <#
  7.     .SYNOPSIS
  8.         A function to get the Latest Stable Version of PowerShell.
  9.     .DESCRIPTION
  10.         This function web scrapes the GitHub repository of PowerShell and
  11.         parses out the Latest Stable Version of PowerShell.
  12.         This function will return the PowerShell version as a string.
  13.  
  14.     .EXAMPLE
  15.         Get-LatestPSVersion
  16. #>
  17.     $url = "https://api.github.com/repos/powershell/powershell/releases/latest"
  18.     try{
  19.         $tag = Invoke-RestMethod $url
  20.         return $tag.tag_name
  21.     }catch{
  22.         $_.exception | Write-Warning
  23.         return $null
  24.     }
  25. }
  26.  
  27. function Download-LatestPSVersion{
  28. <#
  29.     .SYNOPSIS
  30.         A function to download the Lastest Stable Version of PowerShell.
  31.     .DESCRIPTION
  32.         This function web scrapes the GitHub repository of PowerShell and downloads
  33.         the latest stable version.  The download will be saved to Users Downloads folder.
  34.         This function will return the full path string of the downloaded file.
  35.  
  36.     .EXAMPLE
  37.         Download-LatestPSVersion
  38. #>
  39.     $url = "https://api.github.com/repos/powershell/powershell/releases/latest"
  40.     try{
  41.         $tag = Invoke-RestMethod $url
  42.         $latest = $tag.assets | Where-Object { $_.name -like "*win*x64*.msi" }
  43.         $file = "$HOME\Downloads\$($latest.name)"
  44.         Write-Host $('Dowloading latest release to: ' + $file)
  45.         Invoke-WebRequest $latest.browser_download_url -Out $file
  46.         return $file
  47.     }catch{
  48.         $_.exception | Write-Warning
  49.         return $null
  50.     }
  51. }
  52.  
  53. function Is-PSCurrent{
  54. <#
  55.     .SYNOPSIS
  56.         A function to determine if the current version of PowerShell is Up To Date.
  57.     .DESCRIPTION
  58.         This fucntion will call another function to get the Latest version of PowerShell, and then
  59.         compare it to the currently installed version.  Returns True or False.
  60.  
  61.     .EXAMPLE
  62.         Is-PSCurrent
  63. #>
  64.     [version]$current = $PSVersionTable.PSVersion
  65.     $tmp = Get-LatestPSVersion
  66.     if($tmp[0] -eq 'v'){
  67.         [version]$latest = $tmp.split('v')[1]
  68.     }else{
  69.         [version]$latest = $tmp
  70.     }
  71.     if($current -lt $latest){
  72.         return $false
  73.     }else{
  74.         return $true
  75.     }
  76. }
  77.  
  78. function Get-YesNo{
  79. <#
  80.     .SYNOPSIS
  81.         A function to prompt user for a YES or NO.
  82.     .DESCRIPTION
  83.         This function uses System.Management.Automation.Host to prompt the user for a Yes or No reponse.
  84.         Function returns a boolean value of TRUE or FALSE.
  85.  
  86.     .PARAMETER Title
  87.         Title of UI
  88.     .PARAMETER Question
  89.         Question or Description of UI
  90.  
  91.     .EXAMPLE
  92.         $answer = Get-YesNo 'Continue?' 'Do You Wish To Continue?'
  93. #>
  94.     Param ( [Parameter(Mandatory=1)][string]$Title,
  95.             [Parameter(Mandatory=1)][string]$Question
  96.     )
  97.     $answer = $Host.UI.PromptForChoice($Title, $Question, @('&Yes', '&No'), 1)
  98.     if ($answer -eq 0) { return $true }
  99.     return $false
  100. }
  101.  
  102. if(Is-PSCurrent){
  103.     Write-Host $('PowerShell Is Up To Date: ' + $(Get-LatestPSVersion))
  104. }else{
  105.     if ((Get-YesNo 'Upgrade PowerShell' 'Are you sure you want to Upgrade PowerShell?')) {
  106.         Start-Process (Invoke-Expression Download-LatestPSVersion)
  107.     }else{
  108.         Write-Host 'User Canceled PowerShell Upgrade, PowerShell is Not Up To Date.'
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement