How to add AD user account to local administrator group

-1

I am trying to add AD Domain Account to local Administrator group through VBScript /Powershell but nothing helps. FYI: Adding account after Domain Join This is has to be performed for provisioning the admin access.

This is what I have done so far.

    function global:AddUser(){
    $SERVER = "vmtest" #Computer Name
        $userNameToAdd = "DomainName\DomainID" 
        $group =[ADSI]"WinNT://$SERVER/Administrators,group"
             $group =[ADSI]"WinNT://" + $SERVER + "/Administrators,group"
             $group.add("WinNT://" + $env:userdomain + "/" + $userNameToAdd)
            if ($?) { 
                Write-Host "User $userNameToAdd added to local Administrators at $Server."
                $date = date
                Write-Output "$date User $userNameToAdd added to local Administrators at $Server." >> RemoteMassAdmLocalAdmin.log
            } else { Write-Host $er }
}
AddUser

And Through VBScript

Function addtoAdminGroup(x,y)
    Dim DomainName
    Dim UserAccount
    Set net = WScript.CreateObject("WScript.Network")
    local = net.ComputerName
    DomainName = x
    UserAccount = y

    set group = GetObject("WinNT://"& local &"/Administrators")

    on error resume next
    group.Add "WinNT://"& DomainName &"/"& UserAccount &""
    CheckError
    End Function

    Function CheckError()
      if not err.number=0 then
      set ole = CreateObject("ole.err")
      MsgBox ole.oleError(err.Number), vbCritical
      err.clear
    else
      openConsole("Added User to Administrators group")
    end if
    end function

    addtoAdminGroup("<value of domain>","<domain or NT ID>")

But while executing I am getting error "Network Path not found". Please do help me.

However I am trying to perform same in here as answered by David but through script.

Deenath Kumar

Posted 2017-09-03T17:21:31.103

Reputation: 1

Answers

0

Finally I am ended with following command.

net localgroup administrators domainame\domainID /add

Deenath Kumar

Posted 2017-09-03T17:21:31.103

Reputation: 1