Powershell – Cannot add users to local group via powershell

powershellwindows-server-2008-r2

Trying to import a large list of users for mac authentication. I can create the users but no matter what I try I cannot add them to a local group. I continually get "an invalid directory pathname was passed" to the add function.

Here is the actual 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:12 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



$Computername = $env:COMPUTERNAME
$objgroup = [ADSI]"WinNT://./TEST" 
$target = [ADSI]"WinNT://$Computername"

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://./" + $_.MAC
    $objgroup.Add($objuser) 
}

I've tried replacing WinNT://. with WinNT://RadiusSVR but same issue, same if I try WinNT://$Computername.

Before someone asks, yes I mean to set username and password to the mac address. The users are created just fine, but its getting them into said group that is the issue.

Best Answer

Try the following:

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