Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace System.Management.Automation.Host
- Clear-Host
- function Get-LatestPSVersion{
- <#
- .SYNOPSIS
- A function to get the Latest Stable Version of PowerShell.
- .DESCRIPTION
- This function web scrapes the GitHub repository of PowerShell and
- parses out the Latest Stable Version of PowerShell.
- This function will return the PowerShell version as a string.
- .EXAMPLE
- Get-LatestPSVersion
- #>
- $url = "https://api.github.com/repos/powershell/powershell/releases/latest"
- try{
- $tag = Invoke-RestMethod $url
- return $tag.tag_name
- }catch{
- $_.exception | Write-Warning
- return $null
- }
- }
- function Download-LatestPSVersion{
- <#
- .SYNOPSIS
- A function to download the Lastest Stable Version of PowerShell.
- .DESCRIPTION
- This function web scrapes the GitHub repository of PowerShell and downloads
- the latest stable version. The download will be saved to Users Downloads folder.
- This function will return the full path string of the downloaded file.
- .EXAMPLE
- Download-LatestPSVersion
- #>
- $url = "https://api.github.com/repos/powershell/powershell/releases/latest"
- try{
- $tag = Invoke-RestMethod $url
- $latest = $tag.assets | Where-Object { $_.name -like "*win*x64*.msi" }
- $file = "$HOME\Downloads\$($latest.name)"
- Write-Host $('Dowloading latest release to: ' + $file)
- Invoke-WebRequest $latest.browser_download_url -Out $file
- return $file
- }catch{
- $_.exception | Write-Warning
- return $null
- }
- }
- function Is-PSCurrent{
- <#
- .SYNOPSIS
- A function to determine if the current version of PowerShell is Up To Date.
- .DESCRIPTION
- This fucntion will call another function to get the Latest version of PowerShell, and then
- compare it to the currently installed version. Returns True or False.
- .EXAMPLE
- Is-PSCurrent
- #>
- [version]$current = $PSVersionTable.PSVersion
- $tmp = Get-LatestPSVersion
- if($tmp[0] -eq 'v'){
- [version]$latest = $tmp.split('v')[1]
- }else{
- [version]$latest = $tmp
- }
- if($current -lt $latest){
- return $false
- }else{
- return $true
- }
- }
- function Get-YesNo{
- <#
- .SYNOPSIS
- A function to prompt user for a YES or NO.
- .DESCRIPTION
- This function uses System.Management.Automation.Host to prompt the user for a Yes or No reponse.
- Function returns a boolean value of TRUE or FALSE.
- .PARAMETER Title
- Title of UI
- .PARAMETER Question
- Question or Description of UI
- .EXAMPLE
- $answer = Get-YesNo 'Continue?' 'Do You Wish To Continue?'
- #>
- Param ( [Parameter(Mandatory=1)][string]$Title,
- [Parameter(Mandatory=1)][string]$Question
- )
- $answer = $Host.UI.PromptForChoice($Title, $Question, @('&Yes', '&No'), 1)
- if ($answer -eq 0) { return $true }
- return $false
- }
- if(Is-PSCurrent){
- Write-Host $('PowerShell Is Up To Date: ' + $(Get-LatestPSVersion))
- }else{
- if ((Get-YesNo 'Upgrade PowerShell' 'Are you sure you want to Upgrade PowerShell?')) {
- Start-Process (Invoke-Expression Download-LatestPSVersion)
- }else{
- Write-Host 'User Canceled PowerShell Upgrade, PowerShell is Not Up To Date.'
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement