EDN Admin
Well-known member
Ive been writing some VBScripts lately to map network drives based on security groups and to create new users in AD based on a spreadsheet which Ive managed to get working fine.<br/>
<br/>
The next step to to assign security groups to users also but I cant seem to get this test script below to work. Script source http://www.computerperformance.co.uk/vbscript/vbscript_users_add_groups.htm
<br/>
It is telling me there is no such object on the server. The user "Test User" exists in "OU=General ,OU=Users ,OU=Active ,OU=TOP_OU ," and the groups I want to assign from Groups.txt are in "OU=Groups ,OU=Active ,OU=TOP_OU,"<br/>
<br/>
<div style="color:black; background-color:white
<pre><span style="color:green <br/><span style="color:green VBScript to create Groups<br/><span style="color:green AUTHOR: Guy Thomas<br/><span style="color:green COMPANY: Computer Performance<br/><span style="color:green Version 3.5 - May 2006<br/><span style="color:green ----------------------------------------------------------<br/><span style="color:blue Option <span style="color:blue Explicit<br/><span style="color:green On Error Resume next<br/><span style="color:blue Dim strOU, strGroup, strTextGroup, strUser, strFile<br/><span style="color:blue Dim strDNSDomain, objRootDSE, objFSO, objTextFile, intCounter<br/><span style="color:blue Dim objOU, objUser, objGroup<br/><br/><span style="color:blue Const ForReading = 1<br/><span style="color:blue Const ADS_PROPERTY_APPEND = 3<br/>intCounter = 0<br/><br/><span style="color:green strUser ("CN=Test User,") must exist in your OU.<br/><span style="color:green Set the Name of the OU which holds the user and groups<br/><span style="color:green NB introduce another variable if user and group are in different OUs<br/>strUser = <span style="color:#a31515 "CN=Test User,"<br/><span style="color:green strOU = "OU=Generic ,OU=Users ,OU=Active ,OU=TOP ,"<br/>strOU = <span style="color:#a31515 "OU=Groups ,OU=Active ,OU=TOP,"<br/>strFile = <span style="color:#a31515 "C:Usersuser.nameDocumentsScriptsCreateUserGroups.txt"<br/><br/><span style="color:green Open the file For Reading your Group Names<br/><span style="color:blue Set objFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")<br/><span style="color:blue Set objTextFile = objFSO.OpenTextFile(strFile, ForReading)<br/><br/><span style="color:green Here is the loop<br/><span style="color:blue Do<br/> strTextGroup = objTextFile.ReadLine<br/> strGroup = <span style="color:#a31515 "CN=" & strTextGroup & <span style="color:#a31515 " ,"<br/><br/> <span style="color:green Bind to Active Directory and get LDAP name<br/> <span style="color:blue Set objRootDSE = GetObject(<span style="color:#a31515 "LDAP://RootDSE")<br/> strDNSDomain = objRootDSE.<span style="color:blue Get(<span style="color:#a31515 "DefaultNamingContext")<br/><br/> <span style="color:green Prepare the OU and the Group<br/> <span style="color:blue Set objOU =GetObject(<span style="color:#a31515 "LDAP://" & strOU & strDNSDomain)<br/> Msgbox <span style="color:#a31515 "about to try and bind to " & <span style="color:#a31515 "LDAP://"& strGroup & strOU & strDNSDomain<br/> <span style="color:blue Set objGroup = GetObject(<span style="color:#a31515 "LDAP://"& strGroup & strOU & strDNSDomain)<br/><br/> <span style="color:green On Error Resume next<br/> <span style="color:green Add user to Group with .PutEx (put extended)<br/> <span style="color:green Set objGroup = GetObject ("LDAP://"& strGroup & strOU & strDNSDomain)<br/> objGroup.PutEx ADS_PROPERTY_APPEND, _<br/> <span style="color:#a31515 "member", Array(strUser & strOU & strDNSDomain)<br/> <br/> WScript.Echo <span style="color:#a31515 "member", strUser & strOU & strDNSDomain<br/> <br/> objGroup.SetInfo<br/><br/> intCounter = intCounter +1<br/> WScript.Echo strUser & <span style="color:#a31515 " has " & intCounter & <span style="color:#a31515 " new groups"<br/><br/><span style="color:blue Loop <span style="color:blue Until objTextFile.AtEndOfLine = <span style="color:blue true<br/>objTextFile.Close<br/><br/><span style="color:green End of add Groups VBScript<br/>[/code]
<br/>
<br/>
<br/>
Groups.txt<br/>
<br/>
Any help appreciated. From what I can tell it cant find the user.
<br/>
View the full article
<br/>
The next step to to assign security groups to users also but I cant seem to get this test script below to work. Script source http://www.computerperformance.co.uk/vbscript/vbscript_users_add_groups.htm
<br/>
It is telling me there is no such object on the server. The user "Test User" exists in "OU=General ,OU=Users ,OU=Active ,OU=TOP_OU ," and the groups I want to assign from Groups.txt are in "OU=Groups ,OU=Active ,OU=TOP_OU,"<br/>
<br/>
<div style="color:black; background-color:white
<pre><span style="color:green <br/><span style="color:green VBScript to create Groups<br/><span style="color:green AUTHOR: Guy Thomas<br/><span style="color:green COMPANY: Computer Performance<br/><span style="color:green Version 3.5 - May 2006<br/><span style="color:green ----------------------------------------------------------<br/><span style="color:blue Option <span style="color:blue Explicit<br/><span style="color:green On Error Resume next<br/><span style="color:blue Dim strOU, strGroup, strTextGroup, strUser, strFile<br/><span style="color:blue Dim strDNSDomain, objRootDSE, objFSO, objTextFile, intCounter<br/><span style="color:blue Dim objOU, objUser, objGroup<br/><br/><span style="color:blue Const ForReading = 1<br/><span style="color:blue Const ADS_PROPERTY_APPEND = 3<br/>intCounter = 0<br/><br/><span style="color:green strUser ("CN=Test User,") must exist in your OU.<br/><span style="color:green Set the Name of the OU which holds the user and groups<br/><span style="color:green NB introduce another variable if user and group are in different OUs<br/>strUser = <span style="color:#a31515 "CN=Test User,"<br/><span style="color:green strOU = "OU=Generic ,OU=Users ,OU=Active ,OU=TOP ,"<br/>strOU = <span style="color:#a31515 "OU=Groups ,OU=Active ,OU=TOP,"<br/>strFile = <span style="color:#a31515 "C:Usersuser.nameDocumentsScriptsCreateUserGroups.txt"<br/><br/><span style="color:green Open the file For Reading your Group Names<br/><span style="color:blue Set objFSO = CreateObject(<span style="color:#a31515 "Scripting.FileSystemObject")<br/><span style="color:blue Set objTextFile = objFSO.OpenTextFile(strFile, ForReading)<br/><br/><span style="color:green Here is the loop<br/><span style="color:blue Do<br/> strTextGroup = objTextFile.ReadLine<br/> strGroup = <span style="color:#a31515 "CN=" & strTextGroup & <span style="color:#a31515 " ,"<br/><br/> <span style="color:green Bind to Active Directory and get LDAP name<br/> <span style="color:blue Set objRootDSE = GetObject(<span style="color:#a31515 "LDAP://RootDSE")<br/> strDNSDomain = objRootDSE.<span style="color:blue Get(<span style="color:#a31515 "DefaultNamingContext")<br/><br/> <span style="color:green Prepare the OU and the Group<br/> <span style="color:blue Set objOU =GetObject(<span style="color:#a31515 "LDAP://" & strOU & strDNSDomain)<br/> Msgbox <span style="color:#a31515 "about to try and bind to " & <span style="color:#a31515 "LDAP://"& strGroup & strOU & strDNSDomain<br/> <span style="color:blue Set objGroup = GetObject(<span style="color:#a31515 "LDAP://"& strGroup & strOU & strDNSDomain)<br/><br/> <span style="color:green On Error Resume next<br/> <span style="color:green Add user to Group with .PutEx (put extended)<br/> <span style="color:green Set objGroup = GetObject ("LDAP://"& strGroup & strOU & strDNSDomain)<br/> objGroup.PutEx ADS_PROPERTY_APPEND, _<br/> <span style="color:#a31515 "member", Array(strUser & strOU & strDNSDomain)<br/> <br/> WScript.Echo <span style="color:#a31515 "member", strUser & strOU & strDNSDomain<br/> <br/> objGroup.SetInfo<br/><br/> intCounter = intCounter +1<br/> WScript.Echo strUser & <span style="color:#a31515 " has " & intCounter & <span style="color:#a31515 " new groups"<br/><br/><span style="color:blue Loop <span style="color:blue Until objTextFile.AtEndOfLine = <span style="color:blue true<br/>objTextFile.Close<br/><br/><span style="color:green End of add Groups VBScript<br/>[/code]
<br/>
<br/>
<br/>
Groups.txt<br/>
<br/><br/>
Management Users<br/>
TechUsers<br/>
Artists Users<br/>
<br/>
Any help appreciated. From what I can tell it cant find the user.
<br/>
View the full article