我编写了一个 Powershell 模块,其中包含利用 IIS 的 WebAdministration
Powershell 模块的 cmdlet。但是,该模块要求将其加载到管理 Powershell session 中,否则会引发异常。正如您可能知道或猜测的那样,当 Powershell 未首次以管理员身份运行时,我的自定义模块无法加载。
这种行为是可以接受的并且是预期的。我的问题是加载此模块时是否有办法提升 shell 的权限(并遵守 UAC 设置)?
示例:(假设在此特定情况下,在我的 powershell 配置文件中找到 Import-Module WebAdministration
)
- 我单击任务栏上的 powershell 快捷方式。
- 打开具有标准用户权限的 powershell shell,并尝试导入模块。
- 发现模块需要提升权限。
- 系统会提示用户执行此操作,并且 powershell 会重新启动。
最佳答案
My question is whether or not there is a way to elevate the shell's permissions when this module is loaded?
如果可以在没有提示的情况下静默提升,这将是一个安全漏洞。但是您可以使用这样的代码来确保如果您没有以提升的方式运行,模块将抛出错误(并且不会加载):
$Elevated = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ( -not $Elevated ) {
throw "This module requires elevation."
}
关于Powershell 模块和管理员权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43073098/