Hi everybody,
I'm looking for some help on accomplishing the following:
using a single InputBox entry to prompt for the user name, to get the distinguished name and groups of the user. Then using that information to disable the account, move it to an OU, modify some attributes and remove all but Domain Users group. I have these functions in separate scripts which I need to simplify and combine into 1 script.
This is what I have so far:
[code:]
'this gets the user distinguished name
On Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, objArgs, usr
Set objArgs = Wscript.Arguments
if objArgs.Count <> 1 Then Wscript.Echo "FindUser UserName - UserName required."
if objArgs.Count <> 1 Then Wscript.Quit
usr = "N"
sam = objArgs(0)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & sam & "))"
strAttributes = "distinguishedName,sAMAccountName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName")
strSAM = objRecordSet.Fields("sAMAccountName")
usr = "Y"
Wscript.Echo strSAM & " """ & strDN & """"
objRecordSet.MoveNext
Loop
objConnection.Close
Set objConnection = Nothing
if usr = "N" Then Wscript.Echo "FindUser " & sam & " - NOT found."
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing
[/code]
------------------------------------------------------------
Then in a separate script I have:
[code:]
'this gets user groups
Set objUser = GetObject("LDAP://user name, OU info")
Set colGroups = objUser.Groups
For Each objGroup in colGroups
Wscript.Echo objGroup.CN
Next
[/code]
------------------------------------------------------------
using the values returned from above scripts, I want to then feed them into the strUserDN = "userdn" of the disable and move part of the script and into the Set objUser = GetObject _("LDAP://userdn") of the remove group memeberships part of the script.
I have a feeling I'm doing this wrong, but don't know where I'm going wrong, any help would be appreciated.
Thank you
I'm looking for some help on accomplishing the following:
using a single InputBox entry to prompt for the user name, to get the distinguished name and groups of the user. Then using that information to disable the account, move it to an OU, modify some attributes and remove all but Domain Users group. I have these functions in separate scripts which I need to simplify and combine into 1 script.
This is what I have so far:
[code:]
'this gets the user distinguished name
On Error Resume Next
Dim objConnection, objCommand, objRootDSE, strDNSDomain
Dim strFilter, strQuery, objRecordSet, objArgs, usr
Set objArgs = Wscript.Arguments
if objArgs.Count <> 1 Then Wscript.Echo "FindUser UserName - UserName required."
if objArgs.Count <> 1 Then Wscript.Quit
usr = "N"
sam = objArgs(0)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" & sam & "))"
strAttributes = "distinguishedName,sAMAccountName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName")
strSAM = objRecordSet.Fields("sAMAccountName")
usr = "Y"
Wscript.Echo strSAM & " """ & strDN & """"
objRecordSet.MoveNext
Loop
objConnection.Close
Set objConnection = Nothing
if usr = "N" Then Wscript.Echo "FindUser " & sam & " - NOT found."
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing
[/code]
------------------------------------------------------------
Then in a separate script I have:
[code:]
'this gets user groups
Set objUser = GetObject("LDAP://user name, OU info")
Set colGroups = objUser.Groups
For Each objGroup in colGroups
Wscript.Echo objGroup.CN
Next
[/code]
------------------------------------------------------------
using the values returned from above scripts, I want to then feed them into the strUserDN = "userdn" of the disable and move part of the script and into the Set objUser = GetObject _("LDAP://userdn") of the remove group memeberships part of the script.
I have a feeling I'm doing this wrong, but don't know where I'm going wrong, any help would be appreciated.
Thank you