SAMAccount Name - MoveHere

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

SAMAccount Name - MoveHere

Post by mmacdonald »

Greetings:

I have been tinkering with a script that takes input from a CSV file and changes some of the attributes on a user object. I have been using the CN to pull back the user object but have run into some small problems. I have found that if I can pull the SAMAccount name back from AD, that would be better. I found a way I can do that. I am now stuck on how to use the MoveHere command on the object.

You can see from the code that I was using the LDAP path and grabbing the CN. Using the command string:

objCommand.CommandText = "SELECT samAccountName FROM 'LDAP://dc=affinia,dc=affiniagrp,dc=net' WHERE samAccountName = '" & strUserOldName & "'"

I am able to get the SAM account name.

I am trying to use the same bit of code to move the SAM account name.

Here's what I am doing:

' Create a connection to Active Directory 'Set objCont = GetObject("LDAP://" & oParentDN)Set objCont = GetObject("SELECT samAccountName FROM 'LDAP://dc=affinia,dc=affiniagrp,dc=net' WHERE samAccountName = '" & strUserOldName & "'")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")Set objCommand = CreateObject("ADODB.Command")objConnection.Provider = ("ADSDSOObject")objConnection.Open "Active Directory Provider"objCommand.ActiveConnection = objConnection
' Commands used to open the CSV file and select all of the recordsSet oConnection = CreateObject("ADODB.connection")Set oRecordSet = CreateObject("ADODB.recordset")oConnection.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & sCSVFileLocation & ";Extended Properties=""text;HDR=NO;FMT=Delimited"""oRecordSet.open "SELECT * FROM " & sCSVFile ,oConnectionobjCommand.Properties("SearchScope") = ADS_SCOPE_SUBTREE
Do Until oRecordSet.EOF ' Reads the values (cells) in the sInputFile file. strUserOldName = oRecordSet.Fields.Item(0).Value strUserNewName = oRecordSet.Fields.Item(1).Value strEmailAddr = oRecordSet.Fields.Item(2).Value objCommand.CommandText = "SELECT samAccountName FROM 'LDAP://dc=affinia,dc=affiniagrp,dc=net' WHERE samAccountName = '" & strUserOldName & "'" Set objRecordSet = objCommand.Execute If objRecordSet.RecordCount = 0 Then WScript.Echo "The SAMAccount name " & strUserOldName & " is not being used."' WScript.Echo Err.Number' WScript.Quit Else WScript.Echo "The SAMAccount name " & strUserOldName & " is being used and will be renamed to: " & strUserNewName objCont.MoveHere = "SELECT samAccountName FROM 'LDAP://dc=affinia,dc=affiniagrp,dc=net' WHERE samAccountName = '" & strUserOldName & "'", "cn=" & strUserNewName' objCont.MoveHere "LDAP://cn=" & strUserOldName & "," & oParentDN, "cn=" & strUserNewName Set objUser = GetObject("LDAP://cn=" & strUserNewName & "," & oParentDN) objUser.Put "sAMAccountName",strUserNewName objUser.Put "userPrincipalName",strUserNewName & "@" & oDomain objUser.Put "mail",strEmailAddr objUser.SetInfo' WScript.Echo Err.Number' WScript.Quit End If oRecordSet.MoveNext()Loop



Any suggestions would be appreciated.

Thank you.


User avatar
mmacdonald
Posts: 44
Last visit: Thu Apr 07, 2016 7:53 am

SAMAccount Name - MoveHere

Post by mmacdonald »

Yes. And it works for me. But....I have found two instances where I have trouble.

1 - Where the CN is different than the SAM account name. This is really not a big deal except that I have run into duplicate SAM accounts when I try the rename. I would prefer - if possible - to use the SAM account as opposed to the CN.

2 - Cases where there is a comma in the CN. Since I am using a CSV as my source, the 'extra' comma denotes a new field and results in an error. I would prefer to avoid the problem by using the SAM account name. I don't believe any of these have commas embedded in the name.

I guess I am thinking that it would be simpler and, in the long view, more reliable to use the SAM account name. Using the code I have, I can detect a duplicate name but I still have the problem where the CN might have a comma in it. This indicates a new field in the CSV.
This topic is 15 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked