异常呼叫"需求"用" 0"参数:"主要许可请求失败。"

时间:2017-08-30 10:02:43

标签: .net powershell powershell-v2.0

来自PS的以下块

$myAppDomain = [System.AppDomain]::CurrentDomain
$myPrincipal = [System.Security.Principal.PrincipalPolicy]::WindowsPrincipal
$myAppDomain.SetPrincipalPolicy($myPrincipal)
$myPrincipalPermission = New-Object -TypeName System.Security.Permissions.PrincipalPermission -ArgumentList $null, "Administrators"
$myPrincipalPermission.Demand()

我收到以下异常。

Exception calling "Demand" with "0" argument(s): "Request for principal
permission failed."
At line:1 char:30
+ $myPrincipalPermission.Demand <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

PowerShell的版本是

$PSVersionTable
Name                           Value
----                           -----
CLRVersion                     2.0.50727.8762
BuildVersion                   6.1.7601.17514
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

我根据PrincipalPermission class documentation编写了脚本。

1 个答案:

答案 0 :(得分:0)

在登录用户明确是Windows“用户”成员的情况下使用的简化版本。

[System.AppDomain]::CurrentDomain.SetPrincipalPolicy([System.Security.Principal.PrincipalPolicy]::WindowsPrincipal)
$myCP = [System.Threading.Thread]::CurrentPrincipal
$myPP = New-Object -TypeName System.Security.Permissions.PrincipalPermission -ArgumentList $myCP.Identity.Name, "Users"
$myPP.Demand()

如果没有引发SecurityException,则Demand成功。参考 PrincipalPermission.Demand Method () Remarks