I was wondering if someone could take a look at this script and tell me what could be causing a "Constraint Violation Occurred" error on the highlighted line towards the end of my script. The script halts on that line and gives off the following: (167, 3) (null): A constraint violation occurred.
Also, I would like to populate the TerminalServicesProfilePath and found this snippet that MarkdMac posted a few months ago. Is this the only way to add this attribute to a user's profile? I'm running the script with admin credentials directly on the Win 2K3 domain controller.
Thanks
Code:
Const ForReading = 1
sIOLocation = "C:\Scripts\"
sLogFile = sIOLocation & "IO.log"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set file = oFSO.OpenTextFile("c:\scripts\example.txt", ForReading )
set oLogOutput = oFSO.CreateTextFile(sLogFile)
oLogOutput.WriteLine Now & ": Log started"
Do While Not file.AtEndOfStream
aStr = split(File.ReadLine,",")
sLogon = aStr(0)
sFirstName = aStr(1)
sMiddleInit = aStr(2)
sLastName = aStr(3)
strOU = dctOU( aStr(4) )
sMailcluster = aStr(5)
sStorageGroup = aStr(6)
sMailStore = aStr(7)
sSGCapacity = aStr(8)
sDescription = aStr(9)
sEmployeeID = aStr(10)
sHomeFiler = aStr(11)
sCopyUser = aStr(12)
sAdminUser = aStr(13)
sCompTech = aStr(14)
sDisplayName = sFirstName & " " & sMiddleInit & ". " & sLastName
UsersAll = "OU=Users-ALL,"
Set oRootLDAP = GetObject("LDAP://rootDSE")
defaultNamingContext = (GetObject("LDAP://rootDSE")).Get("defaultNamingContext")
Set oContainer = GetObject("LDAP://" & strOU & usersAll & defaultNamingContext)
'put in to check if account already exists and if so, go to next record
On Error Resume Next
Set oNewUser = GetObject("LDAP://" & SearchDistinguishedName(sLogon))
If Err.Number = &H80005000 Then
' Build the User account
On Error GoTo 0
Set oNewUser = oContainer.Create("User","cn="& sFirstName & " " & sLastName)
oNewUser.put "sAMAccountName",lcase(sLogon)
oNewUser.put "givenName",lcase(sFirstName)
oNewUser.put "sn",lcase(sLastName)
oNewUser.put "UserPrincipalName",lcase(SLogon) & "@" & lcase(sLDAPdomain)
oNewUser.put "displayName",sDisplayName
oNewUser.put "name",sDisplayName
oNewUser.put "description",sDescription
oNewUser.put "mail",sFirstName & "_" & sLastName
oNewUser.put "employeeID",sEmployeeID
oNewUser.put "homeDirectory", "\\" & sHomeFiler & "\" & sLogon
oNewUser.put "homeDrive", "Z:"
oNewUser.put "givenName",sFirstName
oNewUser.put "initials",sMiddleInit
oNewUser.put "name",sDisplayName
' Write this information into Active Directory so we can
' modify the password and enable the user account
oNewUser.SetInfo
'If it was successful, continue processing
If err.number = 0 Then
oLogOutput.WriteLine Now & ": " & sLogon & ": Successfully created user account"
' Change the users password and turn off requirement to change at next login
oNewUser.SetPassword "jkl;1234"
oNewUser.Put "pwdLastSet", 0
' Enable the user account
oNewUser.Put "userAccountControl", 512
oNewUser.SetInfo
Else
oLogOutput.WriteLine Now & ": " & sLogon & ": Error creating account: " & err.number & err.description
End If
' If the password set and account enable was successful, indicate. Otherwise, write diagnostics.
If err.number = "0" Then
oLogOutput.WriteLine Now & ": " & sLogon & ": Successfully created user password and enabled account"
Else
oLogOutput.WriteLine Now & ": " & sLogon & ": Password or account enable error : " & err.number & err.description
End If
sLDAPExchangeServer = "/o=mydomain"
sLDAPExchangeServer = sLDAPExchangeServer & "/ou=First Administrative Group"
sLDAPExchangeServer = sLDAPExchangeServer & "/cn=Configuration/cn=Servers"
sLDAPExchangeServer = sLDAPExchangeServer & "/cn=" & sMailCuster
sLDAPhomeMDB = "CN=" & sMailStore & ","
sLDAPhomeMDB = sLDAPHomeMDB & "CN=" & sStorageGroup & " " & sMBCapacity & ","
sLDAPhomeMDB = sLDAPhomeMDB & "CN=InformationStore,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=" & sMailcluster & ","
sLDAPhomeMDB = sLDAPhomeMDB & "CN=Servers,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=First Administrative Group,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=Administrative Groups,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=SWN,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=Microsoft Exchange,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=Services,"
sLDAPhomeMDB = sLDAPhomeMDB & "CN=Configuration,"
sLDAPhomeMDB = sLDAPhomeMDB & "DC=mydomain,DC=com"
' Build and write the users Exchange attributes
oNewUser.put "mDBUseDefaults", sLDAPmDBUseDefaults
oNewUser.put "mail", lcase(SLogon) & "@" & lcase(sLDAPdomain)
oNewUser.put "msExchHomeServerName", sLDAPExchangeServer
oNewUser.put "mailnickname", left(sFirstName, 1) & sLastName
oNewUser.put "homeMDB", sLDAPHomeMDB
oNewUser.put "extensionAttribute15", Left(sMailCluster,3)
[b]oNewUser.SetInfo[/b]
If err.number = "0" Then
oLogOutput.WriteLine Now & ": " & sLogon & ": Successfully created user's Exchange attributes"
Else
oLogOutput.WriteLine Now & ": " & sLogon & ": Exchange attributes error : " & err.number & err.description
End If
Else
oLogOutput.WriteLine "Username " & sLogon & " already exists....proceeding to next record"
End If
Loop
Function SearchDistinguishedName( vSAN )
On Error Resume Next
Dim oRootDSE, oConnection, oCommand, oRecordSet
defaultNamingContext = GetObject("LDAP://rootDSE").Get("defaultNamingContext")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
'On Error GoTo 0
Set oCommand = CreateObject("ADODB.Command")
oCommand.ActiveConnection = oConnection
oCommand.CommandText = "<LDAP://" & defaultNamingContext & ">;" _
& "(&(objectCategory=User)" _
& "(samAccountName=" & vSAN & "));" _
& "distinguishedName;subtree"
Set oRecordSet = oCommand.Execute
' you must use a for/next here or you will get errors
' a recordset is always a collection
While Not oRecordSet.EOF
SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
oRecordset.MoveNext
Wend
oConnection.Close
End Function
Also, I would like to populate the TerminalServicesProfilePath and found this snippet that MarkdMac posted a few months ago. Is this the only way to add this attribute to a user's profile? I'm running the script with admin credentials directly on the Win 2K3 domain controller.
Thanks
Code:
'Set DSO = GetObject("WinNT:")
'Set usr = DSO.OpenDSObject("WinNT://TSPServer/markmac,user", "DomainName\Administrator", "Password", ADS_SECURE_AUTHENTICATION)
'Wscript.echo usr.TerminalServicesProfilePath
'usr.TerminalServicesProfilePath = "\\servername\shareName"
'usr.SetInfo
'WScript.echo usr.TerminalServicesProfilePath