powershell - 如何powershell脚本要求管理员权限
问题描述
如何执行 powershell 脚本来向用户请求管理员权限?打开模式并接受或输入管理员密码。可能吗?
解决方案
我写了一个小片段,将它添加到脚本的开头。
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 对话框(如果启用了安全桌面,则为模式对话框)以要求管理员凭据自动提升。
推荐阅读
- postgresql - 在函数 postgres 内每次迭代后提交更改
- vue.js - 将组件从应用程序移动到库时出错
- pycharm - 在 PyCharm 中配置远程 Python 解释器:错误文件 pycharm_helpers_5
- r - 如何标准化表格中的这些原始数字,以便进行卡方检验?
- java - 如何使用鼠标监听器和计时器计算点击次数
- r - R - 生存:survfit.object(cumhaz =基线或居中?)
- html - 允许在 IE 中调整窗口大小
- r - 用条件 LOCF 估算 NA
- java - Kafka 消息过滤
- c# - 如何设置 IHttpClientAdapter.GetAsync 以返回具有 IsSuccessStatusCode 特定值的 HttpResponseMessage?