Jump to content

help with logon script (writing to active directory)


Recommended Posts

Posted

ive managed to write a script (well pinched and edited really) that when run dumps the computer name into a users active directory info. Currently it works but it dumps the info to description id like it to dump it in office but if i change description to office the script just bombs out

 

Const ADS_PROPERTY_CLEAR = 1

Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")
strCompName = LCase(objNetwork.ComputerName)
strUserDN = objSysInfo.userName
Set objUser = GetObject("LDAP://" & strUserDN)	' Bind to the user object in Active Directory with the LDAP provider.

objUser.Put "Description", (strCompName)	'...add the computer name to Active Directory profile...
objUser.SetInfo										' ...and save the changes.

  • Thanks 1
  • 1 month later...
Posted

Hi sted,

 

Thanks for the above script it has proved really useful for us we coupled it with a couple of things:

  1. Logoff script to clear this value
  2. Changed the AD schema to add a new option to the users right click in ADUC: Start VNC session

We did find one limitation it could not get the client name when a user logs into our terminal servers. We have pinched and modified some functions that allow you to get the clientname. Thought I'd post it back in case you find it useful.

 

'########################################################################################################################
'#	VERSION HISTORY													#
'#															#
'#	v1.00 - Log basic conputer name to AD based on						
'#	http://www.edugeek.net/forums/windows/89800-help-logon-script-writing-active-directory.html 			#
'#	v2.00 - Now works with Terminal Server ClientName								#
'#															#
'########################################################################################################################

On Error Resume Next

'Define the Update AD attribute and define all variables
Const ADS_PROPERTY_UPDATE = 2
Dim objSysInfo, objNetwork, strCompName, strUserDN, objUser, strClientName, strTerminalServerIdent

'Part of the computer name that is unique to terminal servers (e.g. XYZ-TS-00X) must be lower case.
strTerminalServerIdent = "-ts-"

'Create the objects required to get required information
Set objSysInfo = CreateObject("ADSystemInfo")
Set objNetwork = CreateObject("WScript.Network")

'Set the variables required
strCompName = LCase(objNetwork.ComputerName)
strUserDN = objSysInfo.userName

'check if the computer name is a Terminal Server
If Instr(strCompName,strTerminalServerIdent) > 0 then
	strClientName = clientName()
	If strClientName <> "" then
		strCompName = trim(strClientName) 	
	End If
End If

'Connect to the user/computer AD path (Bind)
Set objUser = GetObject("LDAP://" & strUserDN)	

'Add the computer name to the Office AD value for the user and save it
objUser.Put"physicalDeliveryOfficeName", Trim(Ucase(strCompName))
objUser.SetInfo

'Close/empty all the variables (tidy up)
strCompName = ""
strCompName = Null

strUserDN = ""
strUserDN = Null

strClientName = ""
strClientName = Null

Set objSysInfo = Nothing
Set objNetwork = Nothing
Set objUser = Nothing		

'############# FUNCTIONS REQUIRED #########################
Function sessionNumber()
	Dim oShell, oExec, sOutput, iUserPos, iUserLen, iStatePos
	Set oShell = CreateObject("WScript.Shell")
	Set oExec = oShell.Exec("query session %username%")
	sOutput = LCase(oExec.StdOut.ReadAll)
	iUserPos = InStr(sOutput,LCase(oShell.ExpandEnvironmentStrings("%username%")))
	iStatePos = InStr(sOutput,"active")
	iUserLen = Len(oShell.ExpandEnvironmentStrings("%username%"))
	sessionNumber = CInt(Trim(Mid(sOutput,iUserPos+iUserLen,iStatePos-iUserPos-iUserLen)))
End Function

Function clientName()
		Dim oShell
		Set oShell = CreateObject("WScript.Shell")
		On Error Resume Next
		If Err.Number = 0 Then
		clientName = LCase(oShell.RegRead("HKCU\Volatile Environment\"& sessionNumber() &"\CLIENTNAME"))
		Else
		clientName =  ""
		End If
	End Function

 

Mark!

Posted

no probs

 

my main purpose for my script is so that i can quickly see the spec of a given pc so it seems sensible to shove it in ad as it is a database and keeps as much info in the same place as poss.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...