Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

User Account Properties Problem

Status
Not open for further replies.

Grunty

Technical User
Jul 30, 2002
100
GB
Const Enabled = 1
I am trying to set terminal services properties on a user account using the following code:

Const Enabled = 1
Const Disabled = 0
Set objUser = GetObject _
("LDAP://cn=Ben Hughes,ou=General Users,ou=Company Users,dc=company,dc=com")

objUser.TerminalServicesProfilePath = "\\server\share$\bhughes"
objUser.TerminalServicesHomeDirectory = "\\server\bhughes$"
objUser.TerminalServicesHomeDrive = "V:"
objUser.AllowLogon = Enabled

objUser.SetInfo

I get a runtime error 800A01B6 on each of the objUser methods, yet I can't see anything wrong with them. I had sytax errors in the LDAP line which threw up messages about not being able to find the object, so i know that line was ok once I had fixed it.

I have put this code into excel to debug and as I step through it, the objUser variable doesnt contain any data.

Can anyone help?

Thanks
 
Have you verified the distinguished name for the user is correct? I just tried the script you posted and it worked without any errors; queried the account for those values and they are returning with the values set.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Give this a try instead. Note the changes make this more universal, all you need to do is alter how you get UserName.

Code:
Const Enabled = 1
Const Disabled = 0

UserName = "bhughes"

Set objUser = GetObject _
   ("LDAP://" & SearchDistinguishedName(UserName))
 
objUser.TerminalServicesProfilePath = "\\server\share$\" & UserName
objUser.TerminalServicesHomeDirectory = "\\server\" & UserName &"$"
objUser.TerminalServicesHomeDrive = "V:"
objUser.AllowLogon = Enabled

objUser.SetInfo


Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
You can add something like this to Mark's code to make it prompt driven.

Code:
	strInputReturn1 = InputBox("Enter the Account Name of the user" & vbCrLf _
		& "to set their Terminal Server Settings. ")
			If strInputReturn1 = False Then Call Quit(1)
			If strInputReturn1 = "" Then Call Quit(2)

Call Main(strInputReturn1)
Call Quit(3)

Sub Main(strInputReturn1)

'Mark's example code' 

Const Enabled = 1
Const Disabled = 0

UserName = strInputReturn1

Set objUser = GetObject _
   ("LDAP://" & SearchDistinguishedName(UserName))
 
objUser.TerminalServicesProfilePath = "\\server\share$\" & UserName
objUser.TerminalServicesHomeDirectory = "\\server\" & UserName &"$"
objUser.TerminalServicesHomeDrive = "V:"
objUser.AllowLogon = Enabled

objUser.SetInfo
End Sub 

Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function
' End Mark's code example ' 

Sub Quit(Error1)
	If Error1 = "1" Then MsgBox("Canceled")
		If Error1 = "2" Then MsgBox("You didn't enter an Account Name." & vbCrLf & "Try again.")
			If Error1 = "3" Then MsgBox("Yea You Did It!!")
				WScript.Quit
End Sub


Thanks

John Fuhrman
Titan Global Services
 
Thanks for your replies.

I rechekced my own code and could find nothing wrong with it.

i also tried Mark's code and got the same error, havent tried adding sparkbyte's code yet, but i may well use it in a larger script I have.

I remmed out the terminal services lines and instead tried the following:

Const Enabled = 1
Const Disabled = 0
Set objUser = GetObject _
("LDAP://cn=Ben Hughes,ou=General Users,ou=Company Users,dc=company,dc=com")

objUser.description = "test"
objUser.SetInfo

This works fine which makes me wonder If I have some permissions or WSH version problem instead.

Any further help gratefully received, such a simple bit of code but it will save me hours of work if I can get it working.

Thanks again
 
I have found the reason for the failure, if not a solution.

The Terminal Services entries in vbscript will only run on a 2003 server, not on my XP SP2 machine. This is a workaround but I will see if I can get XP to impersonate 2003 somehow.
 
oh oh, yes, sorry Grunty, i was looking at this thread with bells ringing my head...you are right of course. i had the same issue and just accepted running my admin scripts on a w2k3 box, good luck
 
Hi all.

I would need to modify this script (with prompt) to modify all users of a group instead of a per user basis.

Thanks for all you help and time,
Milo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top