Skip to content

Commit

Permalink
Added automatic restart in elevated privileges
Browse files Browse the repository at this point in the history
  • Loading branch information
echalone committed Jan 19, 2021
1 parent 5c6ee68 commit 7c7c1b9
Showing 1 changed file with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ V 1.0.0: Initial version
.SYNOPSIS
Will fix the PowerShell 7 Core Module PowerShellGet Version 2.
Needs to be run with elevated/administrative privileges.
If not in elevated mode, the script will try to restart itself in elevated mode (for this, pwsh.exe needs to be found in the path environment variable).
.DESCRIPTION
Will fix the PowerShell 7 Core Module PowerShellGet Version 2.
Needs to be run with elevated/administrative privileges.
If not in elevated mode, the script will try to restart itself in elevated mode (for this, pwsh.exe needs to be found in the path environment variable).
Currently PowerShell 7 Core is delivered with the PowerShellGet Module Version 2 while Version 3 is still under development.
However, PowerShellGet Module Version 2 has a bug in PowerShell 7 that (it seems) will not be fixed by Microsoft.
If you have installed a powershell script via Install-Script into the AllUsers scope and are then updating the script
Expand Down Expand Up @@ -177,19 +182,40 @@ function Write-ErrorExt {
try {
Write-Host "--- 'PowerShell 7 Core PowerShellGet Module' fixing script started ---`r`n`r`n" -ForegroundColor DarkGreen

Write-Host "Checking for elevated privileges..." -NoNewline
if(!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "insufficient" -ForegroundColor Yellow
Write-Host "Relaunching as elevated process..." -NoNewline

# Relaunch as an elevated process:
$Parameters = @("-File",('"{0}"' -f $MyInvocation.MyCommand.Path))

# Set switches...
if(![string]::IsNullOrWhiteSpace($PowerShellCorePath)) { $Parameters += "-PowerShellCorePath"; $Parameters += $PowerShellCorePath }
if($NoConfirm.IsPresent) { $Parameters += "-NoConfirm" }

# Start new process...
Start-Process pwsh.exe $Parameters -Verb RunAs -ErrorAction Stop
Write-Host "done (exiting)"
exit
}
else {
Write-Host "sufficient" -ForegroundColor Green
}

if([string]::IsNullOrWhiteSpace($PowerShellCorePath)) {
Write-Host "Searching for PowerShell 7 Core..." -NoNewline
$PSPath = $null

if(Test-Path -Path "$Env:ProgramFiles\PowerShell\7\pwsh.exe") {
if(Test-Path -Path "$Env:ProgramFiles\PowerShell\7\pwsh.exe" -ErrorAction Stop) {
$PwshExePath = "$Env:ProgramFiles\PowerShell\7\pwsh.exe"

if(Test-PSCoreVersion -PSPath $PwshExePath) {
$PSPath = $PwshExePath
}
}

if($null -eq $PSPath -and (Test-Path -Path "${Env:ProgramFiles(x86)}\PowerShell\7\pwsh.exe")) {
if($null -eq $PSPath -and (Test-Path -Path "${Env:ProgramFiles(x86)}\PowerShell\7\pwsh.exe" -ErrorAction Stop)) {
$PwshExePath = "${Env:ProgramFiles(x86)}\PowerShell\7\pwsh.exe"

if(Test-PSCoreVersion -PSPath $PwshExePath) {
Expand Down Expand Up @@ -234,7 +260,7 @@ try {

$PwshExePath = "$PowerShellCorePath\pwsh.exe"

if(Test-Path -Path $PwshExePath) {
if(Test-Path -Path $PwshExePath -ErrorAction Stop) {
if(Test-PSCoreVersion -PSPath $PwshExePath) {
Write-Host "ok" -ForegroundColor Green
$PSPath = $PwshExePath
Expand All @@ -250,7 +276,7 @@ try {
}

if(![string]::IsNullOrWhiteSpace($PSPath)) {
$PSRoot = Split-Path -Path $PSPath -Parent
$PSRoot = Split-Path -Path $PSPath -Parent -ErrorAction Stop
$PSGModuleFile = "$PSRoot\Modules\PowerShellGet\PSModule.psm1"

Write-Host "Found PowerShell 7 Core at: ""$PSRoot"""
Expand All @@ -260,7 +286,7 @@ try {
Write-Host "found" -ForegroundColor Green

Write-Host "Reading in content of module file..." -NoNewline
$ModuleFileContent = Get-Content -Path $PSGModuleFile -Force -Raw
$ModuleFileContent = Get-Content -Path $PSGModuleFile -Force -Raw -ErrorAction Stop
Write-Host "done" -ForegroundColor Green

Write-Host "Checking for line to fix..." -NoNewline
Expand Down Expand Up @@ -319,6 +345,11 @@ finally {
Write-Host "`r`n`r`n--- 'PowerShell 7 Core PowerShellGet Module' fixing script ended ---" -ForegroundColor DarkGreen
}

if(!$NoConfirm.IsPresent) {
Write-Host "Press any key to close/end script..."
[System.Console]::ReadKey() | Out-Null
}

#and we're at the end

### --- END --- main script ###

0 comments on commit 7c7c1b9

Please sign in to comment.