Cannot add user to local group.

This topic contains 4 replies, has 4 voices, and was last updated by Profile photo of Johnny Leuthard Johnny Leuthard 6 months, 1 week ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #36863
    Profile photo of Bruce Markey
    Bruce Markey
    Participant

    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

    #36867
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

    Try using a subexpression.

    $objuser = [ADSI]"WinNT://RadiusSVR/$($_.MAC)"
    #36878
    Profile photo of Bruce Markey
    Bruce Markey
    Participant

    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: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

    #36883
    Profile photo of Jonathan Warnken
    Jonathan Warnken
    Participant

    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)
    
    #36915
    Profile photo of Johnny Leuthard
    Johnny Leuthard
    Participant

    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?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.