Kev*_*gby 3 powershell elevated
我知道这看起来像是一个重复的问题,但我已经尝试过这些解决方案,但它们对我不起作用。
我们需要使用我们的域帐户运行脚本,但也要提升执行它的权限。这在大多数设备上都不是问题,因为快捷方式以管理员身份运行并提示我们输入凭据。但是,如果用户是本地管理员,则不会提示我们输入凭据(只是提示是/否 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 UAC prompt
Exit
}
Pause
Run Code Online (Sandbox Code Playgroud)
当自提升脚本在输入用户和域凭据时运行时,它会失去提升...即,当Start-Process -Verb RunAsUser powershell从提升的 PowerShell 运行时,它本身不会提升。
我还尝试了以下方法:
Start-Process powershell -verb RunAs -argumentlist "Start-Process powershell.exe -Verb RunAsUser `"& path\to\script.ps1`""
Run Code Online (Sandbox Code Playgroud)
这会失败,因为域管理员无权访问脚本目录......除非他们被提升。
您是否正在自动化某些操作或只是偶尔运行脚本?脚本目录是本地的还是网络上的?
正如您所注意到的,启动 powershell 的新实例runas不会更改用户,也runasuser不会提升进程。您需要以相反的顺序执行这两个操作。如果您以本地管理员身份登录,请使用 RunAsUser 启动 Powershell,或通过以下方式启动:
然后执行 runas 从那里提升(作为域管理员):
Start-Process PowerShell -Verb RunAs
Run Code Online (Sandbox Code Playgroud)
您可以使用 来检查您当前正在运行的用户whoami。结果应该是您的域帐户,即使已提升。
如果您正在远程管理 PC 并已使用 powershell,请改用 powershell 进行连接,因为会话始终会提升:
Enter-PSSession MyPCName -credential (get-credential -username domain\MyAdmin)
# remote session:
[MyPCName]: PS C:\WINDOWS\system32>
Run Code Online (Sandbox Code Playgroud)
我还建议尽可能不要使用本地管理员帐户。
| 归档时间: |
|
| 查看次数: |
83419 次 |
| 最近记录: |