VBS: Add domain user and group to local administrators RRS feed

  • Question

  • I have a piece of VBS code that I have modified that basically adds a specified domain user and group to the PCs local Administrators group. It works on Windows 7, but not on Windows 8 at all.

    Call AddUserToGroup("./Administrators", "myDomain.net/NetworkAdminis")
    Call AddUserToGroup("./Administrators", "myDomain.net/Domain Admins")
    Call addDomainUser("myDomain", "myUserGroup")
    Sub AddUserToGroup(local, domain)
    	On Error Resume Next
    	Dim objLocalGroup
    	Dim objDomainGroup
    	Dim server
    	For Each server in servers
    		Set objLocalGroup = GetObject("WinNT://" & local & ",group")
    		Set objDomainGroup = GetObject("WinNT://" & domain & ",group")
    		With objLocalGroup
    		End With			
    	Set objLocalGroup = Nothing
    	Set objDomainGroup = Nothing
    End Sub
    Sub addDomainUser(strDomain, strUser)
        Dim strComputer
        Dim objWshNet
        Dim objGroup
        Dim objUser
        Set objWshNet = CreateObject("WScript.Network")
        strComputer = objWshNet.ComputerName
        Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
        Set objUser = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
        If Not objGroup.IsMember(objUser.ADsPath) Then
            objGroup.Add (objUser.ADsPath)
        End If
        Set objWshNet = Nothing
        Set objGroup = Nothing
        Set objUser = Nothing
    End Sub

    I have debugged the code line by line using VBA's IDE and there seems to be no error condition firing. It executes all lines, but it is not adding the users and groups as it did with Windows 7 and below. The script is being run as local administrator.
    • Edited by Rick131 Tuesday, April 28, 2015 11:39 AM
    Monday, April 27, 2015 7:22 PM


  • Hi Rick131,

    This catalog is concentrating on  Windows 8.1 network issue .Considering this issue is more likely a script issue .It is recommended to aks for help from our script forum .They are more familiar with the script issue .Thanks for your understanding and cooperation.
    Script Center

    Best regards

    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    • Marked as answer by Bruce Wooding Wednesday, May 6, 2015 8:28 PM
    Wednesday, April 29, 2015 2:34 AM