I know this looks like a duplicate question, but I've tried the solutions and they haven't worked for me.
We need to run a script with our domain accounts but also execute it elevated. This isn't an issue on most devices, since the shortcut runs as admin and prompts us for a credential. However, if the user is a local admin, we are not prompted for a credential (just a yes/no UAC prompt).
I'm confused why this is not working:
# Get identity of script user
$identity = [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()
# Elevate the script if not already
if ($identity.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host -F Green 'ELEVATED'
} else {
Start-Process PowerShell -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"& '$PSCommandPath'`""
Exit
}
# Ensure the script has domain privileges
if ($identity.IsInRole('[domain]\[admin group]')) {
Write-Host -F Green 'DOMAIN ADMIN'
} else {
Start-Process PowerShell -Verb RunAsUser "-NoProfile -ExecutionPolicy Bypass -Command `"& '$PSCommandPath'`""
Pause # required, otherwise the Exit below closes the UAC prompt
Exit
}
Pause
When the self-elevated script runs as user and domain credentials are entered, it loses elevation... i.e. when Start-Process -Verb RunAsUser powershell is run from an elevated PowerShell, it is not itself elevated.
I also tried the following:
Start-Process powershell -verb RunAs -argumentlist "Start-Process powershell.exe -Verb RunAsUser `"& path\to\script.ps1`""
Which fails because the domain admin does not have access to the script directory... unless they're elevated.