Pwsh 7 and System.Security.Principal (as well as other legacy compatibility libraries)

Context: need to add service accounts to SeBatchLogonRight and other Windows security user assignments via PowerShell. Yes, I know this can be done via GPO, but we cannot in this particular scenario. We are also trying to standardize on PowerShell 7 in our environment.

There are a variety of modules and scripts available to do this, like PSPrivilege and otherwise. These work great in 5.1, but fall flat on their face in 7.1.3. With 7 being built on .NET Core, many of the legacy libraries referenced in these modules are now retrieved via the Microsoft.Windows.Compatibility NuGet package. That said, the primary library in use here, System.Security.Principal, ships with 7 by default.

If you use the above module, you'll immediately get exceptions upon import:

(438,70): error CS1069: The type name 'IdentityReference' could not be found in the namespace
| 'System.Security.Principal'. This type has been forwarded to assembly
| 'System.Security.Principal.Windows, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
| Consider adding a reference to that assembly.         public static void
| RemoveAccountRights(SafeLsaHandle policy, IdentityReference account, string[] rights)

Updating the assembly name with System.Security.Principal.Windows makes no difference.

The part that is really puzzling is that you can call this assembly on the same system and it will work just fine using an inline reference:

(New-Object System.Security.Principal.NTAccount("DOMAIN","USER")).Translate([System.Security.Principal.SecurityIdentifier]).Value

I have also taken this a step further and manually pulled the requires assemblies into the module using this method, even though it shouldn't be necessary to do so. No change.

Any ideas? This has truly stumped me.

Posted Aug 4, 2021, 6:10 PM UTC · edited

04
BlackV•

effectively when anyone wants to get round this issue, you invoke a ps5 session inside you ps7 session (that's how MS get their 99% module compatibility)

the catch is you have to deal with de-serialised objects

I dont have a good answer

1
jborean93•

Can you raise an issue on PSPrivilege so I fix that. It's possible to do but I need to fix up the Add-Type declaration to work with .NET core.

As a workaround you can do something like

$s = New-PSSession -UseWindowsPowerShell
Invoke-Command $s { My-LegacyCmdlet ... }
Remove-PSSession $s

You do need to be careful with what variables you reference or pass in/get out as they will be serialized so this only works to an extent.

1
InaspectussOP•

I’ll be damned, the man himself!

Do you know what exactly changed? I’m genuinely curious to learn as I’ve been diving deeper into .NET myself and feel this is something that is good to understand. I’m amazed that I seem to be the only person reporting this to begin with.

Will get an issue opened up for that as well.

1
jborean93•

The behaviour of Add-Type and the referenced assemblies that it implicitly loads has changed between PowerShell 5 and 6+. It is a lot more conservative in what it loads alongside your code and the System.Security.Principal ones are typically one of those. This is an example of this in action and what needs to be done to fix it up https://gist.github.com/jborean93/a7c181b1ea0a4c0c8b815af9a8cbe508#file-start-processex-ps1-L8-L1735.

1