如何使用powershell脚本来请求用户的管理员权限?打开模式并接受或输入管理密码。有可能吗?
发布于 2020-08-10 17:04:55
我已经写了一个小片段,把它添加到脚本的开头。
if(!([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList "-File `"$($MyInvocation.MyCommand.Path)`" `"$($MyInvocation.MyCommand.UnboundArguments)`""
Exit
}
}
这将检查脚本是否具有提升的权限,如果没有,则将显示UAC对话框(如果启用了安全桌面,则使用模式对话框)来要求管理员凭据自动提升。
发布于 2020-08-10 15:20:13
您应该检查是否不是admin,并运行新的进程(可能使用相同的args),但是使用RunAs
(意思是RequireElevation
)标志。请注意,如果UAC被降低或禁用,这可能根本不起作用。
start-process powershell –verb runAs
(您可以传递其他属性,如$PSCommandPath)
备注:
https://stackoverflow.com/questions/63342982
复制相似问题