ElevatedPowershellExample.ps1
· 542 B · PowerShell
Raw
param([switch]$Elevated)
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated) {
# tried to elevate, did not work, aborting
} else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
'test'
| 1 | param([switch]$Elevated) |
| 2 | |
| 3 | function Test-Admin { |
| 4 | $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent()) |
| 5 | $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator) |
| 6 | } |
| 7 | |
| 8 | if ((Test-Admin) -eq $false) { |
| 9 | if ($elevated) { |
| 10 | # tried to elevate, did not work, aborting |
| 11 | } else { |
| 12 | Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition)) |
| 13 | } |
| 14 | exit |
| 15 | } |
| 16 | |
| 17 | 'test' |