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", "")
    Call AddUserToGroup("./Administrators", " Admins")
    Call addDomainUser("myDomain", "myUserGroup")
    Sub AddUserToGroup(local, domain)
    	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 Wednesday, April 29, 2015 2:08 PM
    Wednesday, April 29, 2015 1:40 PM


All replies