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
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