'PowerShell Error Running Add-LocalGroupMember

I have two VMs that were built using the same base image. Both have the same version of PowerShell and both have the same admin user. When I run Add-LocalGroupMember on one I get an the following error:

PS C:\Users\DevTraining> Add-LocalGroupMember -Group "Remote Desktop Users" -Member "Groovy-Ruby"
Add-LocalGroupMember : Object reference not set to an instance of an object.
At line:1 char:1
+ Add-LocalGroupMember -Group "Remote Desktop Users" -Member "Groovy-Ru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-LocalGroupMember], NullReferenceException
    + FullyQualifiedErrorId : An unspecified error occurred.,Microsoft.PowerShell.Commands.AddLocalGroupMemberCommand

Both machines have ExecutionPolicy set to Unrestricted(for what it's worth). Both have the same system path and PowerShell is installed in the same location. I have tried running it in PowerShell ISE as Administrator. I have also tried running it in a script and the commandline(again FWIW). Surprisingly google as very little pertinent results. Here is the host information:


PS C:\Users\DevTraining> Get-Host


Name             : Windows PowerShell ISE Host
Version          : 5.1.17763.1971
InstanceId       : 74eadbcd-818b-4302-89f6-8cab287a9bd7
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace


Solution 1:[1]

I got this exception when trying to add a domain group to a local one:

Add-LocalGroupMember -Group "local group" -Member "domain\group"

In my case I was developing on a Hyper-V virtual machine and had just reset to a checkpoint. Trying the net localgroup command from this answer showed me the real error:

"The trust relationship between this workstation and the primary domain failed"

You get this when the cached password used by the computer system account to log in to the domain (distinct from the user account password) expires. In my case this was out of sync as a result of the checkpoint rollback. Consequently, the domain group name cannot be resolved and a null exception occurs.

You can fix this by signing out and back in as the local administrator and running this command in an elevated PowerShell session:

Reset-ComputerMachinePassword -Server DomainControllerNameOrIP -Credential Domain\AdminAccountName

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Roberts