Powershell 模块和管理员权限

标签 powershell

我编写了一个 Powershell 模块,其中包含利用 IIS 的 WebAdministration Powershell 模块的 cmdlet。但是,该模块要求将其加载到管理 Powershell session 中,否则会引发异常。正如您可能知道或猜测的那样,当 Powershell 未首次以管理员身份运行时,我的自定义模块无法加载。

这种行为是可以接受的并且是预期的。我的问题是加载此模块时是否有办法提升 shell 的权限(并遵守 UAC 设置)?

示例:(假设在此特定情况下,在我的 powershell 配置文件中找到 Import-Module WebAdministration)

  1. 我单击任务栏上的 powershell 快捷方式。
  2. 打开具有标准用户权限的 powershell shell,并尝试导入模块。
  3. 发现模块需要提升权限。
  4. 系统会提示用户执行此操作,并且 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/

相关文章:

powershell - 与Powershell的双重互斥参数

powershell - 如何在 ScriptBlock 中设置 $_?

azure - 实现重试逻辑后显示角色分配已存在错误

arrays - PowerShell分类表存储哈希表

scripting - Powershell:使用动态创建的参数运行msiexec

sorting - Powershell:对具有不均匀间隔的列的文本文件进行排序

powershell - 使用 powershell 命令处理用户名中的撇号(单引号) - Get-Azureaduser

windows - PowerShell WindowStyle -Hidden 适用于 Windows 10,但不适用于 Windows 7

azure - 如何使用 PowerShell 检索逻辑应用工作流设置?

powershell - 如何使用Powershell捕获DISM输出并使用进度条反射(reflect)状态?