Auto adding computer to AD groups during deployment (VBScript)

Introduction

The goal of this script is adding computer to AD groups during deployment.

Scenarios

There are many scenarios where there is a need to add a computer to an AD-group during deployment, for instance to enable the computer to use a wireless network or adding the computer to an application group.

Script

The following shows how to use the script with MDT(Microsoft Deployment Toolkit).
Step 1: Copy the script to folder "DeploymentShare\Scripts".
Step 2: In the task sequence, add a "Run command line".
 
And in the "Command line", you can input like the following:
 
Cscript.exe %SCRIPTROOT%\AddGroup.vbs  Group1 Group2 Group3
Note Please run the script with AD administrator permission, otherwise it will fail to add the computer to AD group.
 
 Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

VB Script
Edit|Remove
Function Addgroup(groupname) 
    Set oRs = oConnection.Execute("SELECT adspath FROM 'LDAP://" & strDomainPath & "'" & "WHERE objectCategory='group' AND " & "Name='" & GroupName & "'") 
    If Not oRs.EOF Then 
        strAdsPath = oRs("adspath") 
    End If 
    If IsEmpty(strAdsPath) = False  Then  
        Const ADS_SECURE_AUTHENTICATION = 1 
        Set objGroup = GetObject(stradspath)  
        Set objComputer = GetObject(strComputerDN) 
        If (objGroup.IsMember(objComputer.AdsPath) = False) Then 
            objGroup.PutEx ADS_PROPERTY_APPEND, "member"Array(computerdn) 
            objGroup.SetInfo 
        End If 
    End If  
End Function

Additional Resources

CreateObject