Cannot add user to local group.

I’m attempting to import a bunch of users from a csv into local users, then add them to a group. The username and password are set to a mac address, this is for mac authentication.

The import works fine. All users are created but it just will not add them to a local group.

( I based my work off of this post. https://richardspowershellblog.wordpress.com/2008/04/17/local-users-and-groups/ )

My guess is that it’s not concatenating correctly and thats the issue, but I’m not really sure. I’ve done very little with powershell.

Code:

$objgroup = [ADSI]"WinNT://RadiusSVR/TEST,group" 
$target = [ADSI]"WinNT://RadiusSVR"

Import-Csv testlist1.csv | ForEach-Object { 
$newuser = $target.Create("user", $_.MAC) 
$newuser.SetPassword($_.MAC) 
$newuser.SetInfo() 
$newuser.FullName = ($_.NAME)
$newuser.SetInfo()
$newuser.psbase.InvokeSet('AccountDisabled', $false) 
$newuser.SetInfo() 

$objuser = [ADSI]"WinNT://RadiusSVR/" + $_.MAC
$objgroup.Add($objuser) 
}

Error:

Method invocation failed because [System.DirectoryServices.DirectoryEntry] does not contain a method named
‘op_Addition’.
At C:\Users\Administrator\Desktop\Import Script\Import1.ps1:13 char:2

  • $objuser = [ADSI]"WinNT://RadiusSVR/" + $_.MAC
    
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

Exception calling “Add” with “1” argument(s): "An invalid directory pathname was passed

Try using a subexpression.

$objuser = [ADSI]"WinNT://RadiusSVR/$($_.MAC)"

Bob,

Tried that. I get type mismatch errors.

I got it working by using net commands instead. I’ve tried like a dozen suggestions to get this to work and at this point I don’t believe it can.

Here is the error I get when I try that.

Exception calling "Add" with "1" argument(s): "Type mismatch. (Exception from   HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))"
At C:\Users\Administrator\Desktop\Import Script\Import1.ps1:14 char:2
+     $objgroup.Add($objuser)
  • ~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException
    • FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

Simple fix see https://blogs.technet.microsoft.com/heyscriptingguy/2014/10/03/adding-local-users-to-local-groups/

use the path when adding

$objgroup.Add($objuser.path)

Looks to me like it’s balking at
$objuser = [ADSI]“WinNT://RadiusSVR/” + $_.MAC

‘op_Addition’ makes the ‘+’ in + $.MAC jump out at me.
Maybe enclose it all in ()
( [ADSI]“WinNT://RadiusSVR/” + $
.MAC )
Or maybe try
[ADSI]“WinNT://RadiusSVR/$_.MAC”
Depending on the PS version you are using.

If using POSH3 or higher perhaps try creating a Desired State Push Script that says what accounts and groups must be present and what memberships each group must have?