Create a folder C:\Scripts and share it as Scripts$, make sure domain users have write access to it. (append may be enough, I haven't tested it yet)
Put the settings in the default domain policy:
- User Configuration / Policies / Administrative Templates / System / Logon / Run these programs at user logon = \\servername\Scripts$\log_on.vbs
- User Configuration / Policies / Windows Settings / Scripts / Logoff = \\servername\Scripts$\log_off.vbs
Save the following file as C:\Scripts\log_on.vbs:
'--------------------8<----------------------
' UNC path and name of the central log file,
' be sure all users have write access to it
sLogFilePath = "\\servername\CaramelScripts$\log_on_off.csv"
' Use e.g. "Logon" on the logon script, and "Logoff" in the logoff script
sType = "Logon"
Const OpenAsASCII = 0
Const OverwriteIfExist = -1
Const ForAppending = 8
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNetwork = CreateObject("WScript.Network")
Set oADSystemInfo = CreateObject("ADSystemInfo")
Set wshShell = CreateObject("WScript.Shell")
sClientName = "Not recorded"
sClientName = wshShell.ExpandEnvironmentStrings("%CLIENTNAME%")
sDateTime = Now
sComputer = oWshNetwork.ComputerName
sUser = oWshNetwork.UserName
Set oUser = GetObject("LDAP://" & oADSystemInfo.UserName)
sUserOU = Mid(oUser.Parent, 8)
' create a proper CSV format so it is easy to import to e.g. Excel
sLogLine = """" & sType & """,""" & sDateTime & """,""" & sUser _
& """,""" & sComputer & """,""" & sClientName & """,""" & sUserOU & """"
If oFSO.FolderExists(oFSO.GetParentFolderName(sLogFilePath)) Then
' Create entry in log file.
' If script is not able to open the file for write access, it
' will go into a loop and try several times, but will give up
' after 5 seconds (10 loops)
bUpdLogFinished = False
iLoops = 0
On Error Resume Next
Do
Err.Clear
' Open for appending
Set fLogFile = oFSO.OpenTextFile(sLogFilePath, ForAppending, True)
If Err.Number <> 0 Then
' Was not able to open the log file for writing
' Waiting 1/2 a second before trying again
WScript.Sleep 500
Else
fLogFile.WriteLine sLogLine
fLogFile.Close
bUpdLogFinished = True
End If
iLoops = iLoops + 1
Loop Until bUpdLogFinished Or iLoops > 10
On Error Goto 0
End If
'--------------------8<----------------------
Save the following file as C:\Scripts\log_off.vbs:
'--------------------8<----------------------
' UNC path and name of the central log file,
' be sure all users have write access to it
sLogFilePath = "\\servername\Scripts$\log_on_off.csv"
' Use e.g. "Logon" on the logon script, and "Logoff" in the logoff script
sType = "Logoff"
Const OpenAsASCII = 0
Const OverwriteIfExist = -1
Const ForAppending = 8
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oWshNetwork = CreateObject("WScript.Network")
Set oADSystemInfo = CreateObject("ADSystemInfo")
sDateTime = Now
sComputer = oWshNetwork.ComputerName
sUser = oWshNetwork.UserName
Set oUser = GetObject("LDAP://" & oADSystemInfo.UserName)
sUserOU = Mid(oUser.Parent, 8)
' create a proper CSV format so it is easy to import to e.g. Excel
sLogLine = """" & sType & """,""" & sDateTime & """,""" & sUser _
& """,""" & sComputer & """, ,""" & sUserOU & """"
If oFSO.FolderExists(oFSO.GetParentFolderName(sLogFilePath)) Then
' Create entry in log file.
' If script is not able to open the file for write access, it
' will go into a loop and try several times, but will give up
' after 5 seconds (10 loops)
bUpdLogFinished = False
iLoops = 0
On Error Resume Next
Do
Err.Clear
' Open for appending
Set fLogFile = oFSO.OpenTextFile(sLogFilePath, ForAppending, True)
If Err.Number <> 0 Then
' Was not able to open the log file for writing
' Waiting 1/2 a second before trying again
WScript.Sleep 500
Else
fLogFile.WriteLine sLogLine
fLogFile.Close
bUpdLogFinished = True
End If
iLoops = iLoops + 1
Loop Until bUpdLogFinished Or iLoops > 10
On Error Goto 0
End If
'--------------------8<----------------------
