Created
May 23, 2023 11:52
-
-
Save 2-click/0c014d7cd820fd639f7f770a7fc2d0ed to your computer and use it in GitHub Desktop.
RMM invoke Powershell 7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check for required PowerShell version (7+) | |
if (!($PSVersionTable.PSVersion.Major -ge 7)) { | |
try { | |
# Install PowerShell 7 if missing | |
if (!(Test-Path "$env:SystemDrive\Program Files\PowerShell\7")) { | |
Write-Output 'Installing PowerShell version 7...' | |
Invoke-Expression "& { $(Invoke-RestMethod https://aka.ms/install-powershell.ps1) } -UseMSI -Quiet" | |
} | |
# Refresh PATH | |
$env:Path = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path', 'User') | |
# Restart script in PowerShell 7 | |
pwsh -File "`"$PSCommandPath`"" @PSBoundParameters | |
} | |
catch { | |
Write-Output 'PowerShell 7 was not installed. Update PowerShell and try again.' | |
throw $Error | |
} | |
finally { exit $LASTEXITCODE } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment