是否可以从以提升的权限运行的批处理文件中运行没有提升权限的命令?
我知道这看起来像是一个重复的问题,但我已经尝试过这些解决方案,但它们对我不起作用。
我们需要使用我们的域帐户运行脚本,但也要提升执行它的权限。这在大多数设备上都不是问题,因为快捷方式以管理员身份运行并提示我们输入凭据。但是,如果用户是本地管理员,则不会提示我们输入凭据(只是提示是/否 UAC)。
我很困惑为什么这不起作用:
# 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 …
Run Code Online (Sandbox Code Playgroud)