0

When importing a PowerShell module that

#Requires -RunAsAdministrator

from my PowerShell profile, it throws an ScriptRequiresElevation,Microsoft.PowerShell.Commands.ImportModuleCommand error.

How can I either

  • a) add a condition/parameter that the module is only imported when running an elevated shell or
  • b) move the import to a profile that gets only loaded when running an elevated shell?
4

1 回答 1

2

The easiest is to add the condition check in your profile script, you can add the check to see if you are running as Administrator:

$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
    Import-Module NeedsAdminModule
}
于 2019-05-29T19:41:40.590 回答