Hi sted,
Thanks for the above script it has proved really useful for us we coupled it with a couple of things:
- Logoff script to clear this value
- 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.
Code:
'########################################################################################################################
'# 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!