来自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
答案 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