exchange12rocks / PSGPPreferences

A way to manage Group Policy Preferences through PowerShell
MIT License
35 stars 2 forks source link

Can't run New-GPPGroupMember with variables in group name without creating exception #31

Closed Borgquite closed 2 years ago

Borgquite commented 2 years ago

OK, here goes

I'm trying to automate the steps in the following guides for managing a number of local groups via Group Policy Preferences:

However New-GPPGroupMember currently throws an exception if the group name passed can't be translated at runtime and the guide involves creating group names which include a preference process variable (https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/dn789194(v=ws.11)#preference-process-variables)

PS C:\Windows\system32> New-GPPGroupMember -N "%DomainName%\%ComputerName Administrators" -Action ADD
Exception calling "Translate" with "1" argument(s): "Some or all identity references could not be translated."
At C:\Program Files\WindowsPowerShell\Modules\PSGPPreferences\0.2.0\Groups\New-GPPGroupMember.ps1:14 char:9
+         $SID = ([System.Security.Principal.NTAccount]::new($Name)).Tr ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdentityNotMappedException

name                                      action sid
----                                      ------ ---
%DomainName%\%ComputerName Administrators    ADD    

Please could this exception be caught with a try / catch loop? I have tried using -ErrorAction SilentlyContinue but the exception is still thrown 'behind the scenes' and since I am trying to do this with PowerShell DSC and it doesn't like exceptions at all.

exchange12rocks commented 2 years ago

Ah, so that exception appears when there is no security identity with that name in the current domain. Of course when using variables here, there most likely won't be an account with such a name. I will add code to process this situation - the name will be saved, but the SID property will remain unpopulated.

exchange12rocks commented 2 years ago

@Borgquite Please check the dev branch

Borgquite commented 2 years ago

Thanks - seems to be suppressing the error. May need to wait for the new version to be deployed before I can fully test in DSC (can't really deploy dev modules to a production server)

Borgquite commented 2 years ago

Managed to get a test environment where I could update the module, works w/ DSC :)

exchange12rocks commented 2 years ago

Fixed in e5bc74ec5ec364de9a393e8ce5a1bcd67a72a271