mahesh_gowda
asked on
VB script to be used in Active Directory
Hello,
Need a script which does the following in Active Directory.
1.Enumerates the user members of the group specified and for each user exports the attributes defined in the objAttributes dictionary object to a text delimetered text file.
2.Imports user attributes from the file specified in the USER_LIST variable, attaches to AD and creates each account if one does not exist.
Regards,
MG
Need a script which does the following in Active Directory.
1.Enumerates the user members of the group specified and for each user exports the attributes defined in the objAttributes dictionary object to a text delimetered text file.
2.Imports user attributes from the file specified in the USER_LIST variable, attaches to AD and creates each account if one does not exist.
Regards,
MG
The second is a bit more difficult.
What kind of format do we have for the import file?
Chris
ASKER
Hello Chris,
Thank you for your quick reply.. The format will be text file.
Regards,
MG
Thank you for your quick reply.. The format will be text file.
Regards,
MG
I know, but you must be more specific than that.
VbScript doesn't have a specific "import this file" function, it has to be told everything about the file. What columns am I to expect? What delimiter is being used? If it's comma delimited, do I have to deal with quotes?
Chris
ASKER
Hello Chris,
It should something like,
objLog.WriteLine("User Input File:" & vbTab & USER_LIST)
objLog.WriteLine("Default Password for users" & vbTab & DEFAULT_PASSWORD)
objLog.WriteLine("Target AD Location:" & vbTab & strOU)
objLog.WriteBlankLines(2)
objLog.WriteLine("samAccou ntName" & vbTab & "Display Name" & vbTab & "cn")
Will this help? Sorry for not being a good explainer..
Regards,
MG
It should something like,
objLog.WriteLine("User Input File:" & vbTab & USER_LIST)
objLog.WriteLine("Default Password for users" & vbTab & DEFAULT_PASSWORD)
objLog.WriteLine("Target AD Location:" & vbTab & strOU)
objLog.WriteBlankLines(2)
objLog.WriteLine("samAccou
Will this help? Sorry for not being a good explainer..
Regards,
MG
Perfect, thanks :)
I'll be right back...
Chris
Actually, do you have a format for USER_LIST as well?
Chris
ASKER
Is this the one you asked for? I am not sure..
Dim strMid
Dim strItem
Dim strLine
Dim strSn
Dim strGivenName
Dim strCity
Dim strCountry
Dim strSuffix
Dim strScriptPath
Dim strLog
Dim strOU
Dim strMid
Dim strItem
Dim strLine
Dim strSn
Dim strGivenName
Dim strCity
Dim strCountry
Dim strSuffix
Dim strScriptPath
Dim strLog
Dim strOU
This is to start us off. I'm still not quite clear on what's to happen with the USER_LIST / attribute list.
However, this creates user accounts (if the username doesn't already exist) and sets a couple of properties then enables the account.
Chris
Const IMPORT_FILE = "C:\Temp\Users.txt"
Function ReadFile
Dim objFile : Set objFile = objFSO.OpenTextFile(IMPORT_FILE, 1, False, 0)
' Global variables
strAttributeList = Split(objFile.ReadLine, vbTab)(1)
strDefaultPassword = Split(objFile.ReadLine, vbTab)(1)
strTargetOU = Split(objFile.ReadLine, vbTab)(1)
' Skip the two blank lines
objFile.SkipLine : objFile.SkipLine
Dim objUsers : Set objUsers = CreateObject("Scripting.Dictionary")
Do While Not objFile.AtEndOfStream
Dim arrLine : arrLine = Split(objFile.ReadLine, vbTab)
Dim strSAMAccountName : strSAMAccountName = arrLine(0)
Dim strDisplayName : strDisplayName = arrLine(1)
Dim strCN : strCN = arrLine(2)
If Not objUsers.Exists(strSAMAccountName) Then
objUsers.Add strSAMAccountName, Array(strDisplayName, strCN)
End If
Loop
Set ReadFile = objUsers
End Function
Function CheckUsers(objUsers)
Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
Dim strDomain : strDomain = objNetwork.UserDomain
Set objNetwork = Nothing
Dim strUsername
For Each strUsername in objUsers
Dim strUserDN : strUserDN = GetObjectDN(strUsername, strDomain)
If strUserDN <> "" Then
objLogFile.WriteLine "User Exists: " & strUsername & " :: " & strUserDN
objUsers.Remove strUsername
End If
Next
Set CheckUsers = objUsers
End Function
Function GetObjectDN(strObject, strDomain)
' Return Type: String
'
' Returns a Distinguished Name for an Object from it's NT SAM ID.
' This will only function for valid object types within an NT Domain structure.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_NAME_TYPE_NT4 = 3
On Error Resume Next : Err.Clear
Dim objNameTranslate : Set objNameTranslate = CreateObject("NameTranslate")
objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
objNameTranslate.Set ADS_NAME_TYPE_NT4, strDomain & "\" & strObject
Dim strObjectDN : strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
If Err.Number <> 0 Then ' Make the DN Blank for a Failed Search
strObjectDN = ""
End If
Set objNameTranslate = Nothing
On Error Goto 0
GetObjectDN = strObjectDN
End Function
Sub CreateUsers(objUsers, strTargetOU)
Dim objOU : Set objOU = GetObject("LDAP://" & strTargetOU)
Dim strUsername
For Each strUsername in objUsers
Dim objUser : Set objUser = objOU.Create("user", objUsers(strUsername)(1))
objUser.Put "sAMAccountname", strUsername
objUser.Put "displayName", objUsers(strUsername)(0)
objUser.SetInfo
objUser.SetPassword strDefaultPassword
objUser.AccountDisabled = False
objUser.SetInfo
objLogFile.WriteLine "Created User: " & strUsername & " :: " & objUser.Get("distinguishedName")
Set objUser = Nothing
Next
End Sub
'
' Main code
'
Dim strAttributeList, strDefaultPassword, strTargetOU
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objLogFile : Set objLogFile = objFSO.OpenTextFile("LogFile.txt", 2, True, 0)
Dim objUsers : Set objUsers = ReadFile
Set objUsers = CheckUsers(objUsers)
If objUsers.Count > 0 Then
CreateUsers objUsers, strTargetOU
End If
> Is this the one you asked for?
Not quite, I need to see the file it's supposed to be reading (rather than the vbscript parts).
Chris
ASKER
Hello Chris,
I am going to try this one and will get back to you as soon as possible..
I am going to try this one and will get back to you as soon as possible..
ASKER
Hello Chris,
You have been of Great help!! ON the second script, when and where we will be specifying the OU where the users from the users.txt will be imported and get created in that OU.
For example if I want the users in the users.txt to be created in test OU, how will I pass that in this script?
Regards,
MG
You have been of Great help!! ON the second script, when and where we will be specifying the OU where the users from the users.txt will be imported and get created in that OU.
For example if I want the users in the users.txt to be created in test OU, how will I pass that in this script?
Regards,
MG
Hey :)
This is an example of the file format it read (based on the VbScript code you posted above).
So, the script reads the target OU from the third line of the import file. Then after a two line break it starts reading the users. In this case, the script would skip "dentc" because I exist in my test domain, then create "bob" because he doesn't.
Chris
User Input File: Not sure at present
Default Password for users: Password123
Target AD Location: OU=Test,DC=domain,DC=net
dentc Chris Dent CN=Chris Dent
bob Bob CN=Bob
ASKER
Hello Chris,
Thank you for clarifying my doubts.. I will try this today and will get back to you if I have any questions. Once again you have been a great help!!
Regards,
MG
Thank you for clarifying my doubts.. I will try this today and will get back to you if I have any questions. Once again you have been a great help!!
Regards,
MG
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I suspect we'll want two separate scripts to handle this.
This is the first. Output is tab delimited.
Chris
Open in new window