Re: Recording AD Logons to SQL Database
hi all
i do this to keep an eye on changes made to hw or sw on the machines i
manage
the script runs client side and writes to a local lan db directly at each
login
and records a range of infomation
acctualy looks very similar to your final version
but with trimmings
as all our pcs are dell i use the bios seriam number as a unique value as
this is the dell service tag
phill
code to folow if anyone wants it
"kcadmin" <kcadmin@discussions.microsoft.com> wrote in message
news:9A4E60F6-18AC-4F97-83C7-5B821BB83AD6@microsoft.com...
> Thanks for the . Here is what I finally ended up with...
>
> Option Explicit
>
> Dim strServer, strInstance, strDatabase, strUid, strPwd, strConnect
> Dim adoConnection, adoCommand, objNetwork
> Dim strUserName, strComputerName, strSQL
>
> ' Specify SQL Server, Instance name (if any), and database.
> strServer = "SQLServerName"
> strInstance = ""
> strDatabase = "DatabaseName"
> strUid = "UserID"
> strPwd = "password"
>
>
> If (strInstance <> "") Then
> strServer = strServer & "\" & strInstance
> End If
>
> ' Connection string for database.
> ' This uses SQL 2000 Authentication.
> strConnect = "Driver=SQL Server;" _
> & "SERVER=" & strServer & ";" _
> & "DATABASE=" & strDatabase & ";" _
> & "UID=" & strUid & ";" _
> & "Pwd=" & strPwd & ";"
>
> ' Create ADO objects and connect to database.
> Set adoConnection = CreateObject("ADODB.Connection")
> adoConnection.ConnectionString = strConnect
> adoConnection.Open
>
> Set adoCommand = CreateObject("ADODB.Command")
> adoCommand.ActiveConnection = adoConnection
>
> ' Retrieve values.
> Set objNetwork = CreateObject("Wscript.Network")
> strUserName = objNetwork.UserName
> strComputerName = objNetwork.ComputerName
>
> ' Log date/time, user name, and computer name.
> strSQL = "INSERT INTO TableName " _
> & "(LogonDate, UserName, ComputerName) " _
> & "VALUES(" _
> & "GETDATE(), " _
> & "'" & strUserName & "', " _
> & "'" & strComputerName & "'" _
> & ")"
> adoCommand.CommandText = strSQL
> adoCommand.Execute
>
> ' Clean up.
> adoConnection.Close
>
> "kcadmin" wrote:
>
>> I've been trying to find a way to keep a running log of who logged on,
>> when
>> and from what pc by running the code below in my login script. The code
>> works, but the csv file method is limited since it tends to lock up the
>> file
>> if more than one is trying to write to it simultaneously. I want to do
>> basically the same thing, but log it directly to SQL. Any ideas?
>>
>>
>> '************************************************* ****
>> '** Record Logon Information
>> '************************************************* ****
>>
>> 'Check for the existance of the drop log and create new file if necessary
>>
>> Const FOR_APPENDING = 8
>> strOutputFile = "\\servername\drop$\ad\adlog.csv"
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> If objFSO.FileExists(strOutputFile) Then
>> Set objTextStream = objFSO.OpenTextFile(strOutputFile, FOR_APPENDING)
>> Else
>> Set objTextStream = objFSO.CreateTextFile(strOutputFile)
>> End If
>>
>>
>> 'get user info
>>
>> Set objSysInfo = CreateObject("ADSystemInfo")
>> Set objUser = GetObject("LDAP://" & objSysInfo.UserName)
>> Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
>> strMessage = objUser.CN &","& objComputer.CN &","& Now &","
>>
>>
>> 'write it to the file
>>
>> objTextStream.WriteLine strMessage
>> objTextStream.Close
|