diff --git a/docs/overwrite_openssh_license_script.md b/docs/overwrite_openssh_license_script.md index 118efe6..a3280fa 100644 --- a/docs/overwrite_openssh_license_script.md +++ b/docs/overwrite_openssh_license_script.md @@ -2,7 +2,8 @@ The repository root contains `.\overwrite_openssh_license.ps1`. -The script overwrites `C:\Windows\System32\OpenSSH\LICENSE.txt` with: +The script relaunches itself elevated when needed, then overwrites +`C:\Windows\System32\OpenSSH\LICENSE.txt` with: ```text hello world diff --git a/overwrite_openssh_license.ps1 b/overwrite_openssh_license.ps1 index d1669ab..244d5d6 100644 --- a/overwrite_openssh_license.ps1 +++ b/overwrite_openssh_license.ps1 @@ -1,3 +1,26 @@ +$currentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() +$currentPrincipal = [Security.Principal.WindowsPrincipal]::new($currentIdentity) +$isElevated = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) + +if (-not $isElevated) +{ + $shellPath = Join-Path $PSHOME 'pwsh.exe' + + if (-not (Test-Path $shellPath)) + { + $shellPath = 'powershell.exe' + } + + Start-Process -FilePath $shellPath -Verb RunAs -ArgumentList @( + '-ExecutionPolicy', + 'Bypass', + '-File', + $PSCommandPath + ) + + exit +} + $targetPath = 'C:\Windows\System32\OpenSSH\LICENSE.txt' Set-Content -Path $targetPath -Value 'hello world'