2
votes

From the following block of 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()

I am getting the following exception.

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

The version of PowerShell is

$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

I wrote the script based on the PrincipalPermission class documentation.

1
Funky. I can reproduce the issue (and work around it by supplying the current username), but looking at the source, nothing seems to explain whyMathias R. Jessen
What are you trying to accomplish exactly?Mathias R. Jessen

1 Answers

0
votes

Simplified version that will work where the logged in user is explicitly a member of the Windows Group "Users".

[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()

If no SecurityException is raised, Demand succeeds. Reference PrincipalPermission.Demand Method () Remarks