Jump to content

jmhopkins

Members
  • Posts

    1
  • Joined

  • Last visited

Everything posted by jmhopkins

  1. Here is my simple vbs script on login which posts login time, name, and computer to an SQL Table. (It also puts the last logged in user and timestamp in the AD computer comment..) Call it from Group Policy Login Script. 'On Error Resume Next Set objSysInfo = CreateObject("ADSystemInfo") 'Bind to AD Set objNet = CreateObject("WScript.Network") strCompDN = objSysInfo.ComputerName 'DN for computer, e.g. "CN=VISTAWORKSTATION,OU=Child OU Name,OU=Parent OU Name,DC=domain,DC=com" Set wshShell = WScript.CreateObject( "WScript.Shell" ) strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" ) strCompName = objSysInfo.ComputerName Set objComp = GetObject("LDAP://" & strCompDN) 'IADsComputer object strUserDN = objSysInfo.UserName 'DN for user, e.g. "CN=John Smith,OU=Child OU Name,OU=Parent OU Name,DC=domain,DC=com" Set objUser = GetObject("LDAP://" & strUserDN) 'IADsUser object strUsrLogin = LCase(objNet.UserName) 'Parse out datestamp in text format. strNow = Now strDateStamp = DatePart("yyyy",strNow) & _ Right("0" & DatePart("m",strNow), 2) & _ Right("0" & DatePart("d",strNow), 2) & _ "@" & _ Right("0" & DatePart("h",strNow), 2) & _ Right("0" & DatePart("n",strNow), 2) 'RegExp object used to perform a simple match on IP address Set objRE = New RegExp objRE.IgnoreCase = True 'Note this regexp pattern isn't "correct" for matching an IPv4 address properly, but since WMI will return an 'array of IP addresses, this is sufficient to distinguish IPv4 vs IPv6 objRE.Pattern = "^\d+\.\d+\.\d+\.\d+$" strIP = "" 'Connect to WMI and retreive all network adapters Set objWMI = GetObject("winmgmts:") Set colNICs = objWMI.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 'Get the IP(s) assigned to whichever network adapter has our default gateway If colNICs.Count > 0 Then For Each objNIC in colNICs If IsArray(objNIC.DefaultIPGateway) Then arrIP = objNIC.IPAddress For i = 0 To UBound(arrip) If objRE.Test(arrIP(i)) Then strIP = strIP & " " & arrIP(i) Next strMAC = objNIC.MACAddress End If Next End If strIP = Trim(strIP) objComp.Description = strDateStamp & " " & strUsrLogin & " " & strIP 'Or, put them in separate attributes in AD 'objComp.Put "extensionAttribute1", strUsrLogin 'objComp.Put "extensionAttribute2", strIP 'objComp.Put "extensionAttribute3", strMAC objComp.Setinfo strLogFile = "'" & strComputerName & "', '" & strNow & "','" & strUsrLogin & "','" & strIP & "'" 'wscript.echo strLogFile Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=SqlServerName;Initial Catalog=Database;user id ='SqlUser';password='SqlPassword'" Set myConn = CreateObject("ADODB.Connection") Set myCommand = CreateObject("ADODB.Command" ) myConn.Open DB_CONNECT_STRING Set myCommand.ActiveConnection = myConn strCommandString = "INSERT INTO table_ComputerLog (ComputerName,LoginTime,Username,IPv4) VALUES( " strCommandString = strCommandString & strLogFile & ")" myCommand.CommandText = strCommandString myCommand.Execute myConn.Close Set objWMI = nothing Set colNICs = nothing
×
×
  • Create New...