Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Iron Speed
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old November 16th, 2007, 01:32 PM
jskinner123 jskinner123 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2007
Posts: 1 jskinner123 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 35 sec
Reputation Power: 0
Login Script - Saving to database help

Hello everyone. I currently have a login script that I will post at the end of this post, but it basiaclly writes the following information to a text file:

User Name
Computer Name
Date / Time
IP Address

Example output
Logon , 11/12/2007 3:37:15 PM , MRB*** , go**** , 10.192.***.**


I need to figure out how to write this information into a database, so that I can make a website for my boss to run reports against the information. If someone could show me how to write a variable to a database in a login script that would be great. Or even better modify my script to write the current outputs to a database instead of the .log file.

I also need to find a way to put the month, day, year, time in their own fields to allow for better sorting. Does anyone know how to grab these specific informations?

vb Code:
Original - vb Code
  1. Option Explicit
  2.  
  3. Dim objFSO, objLogFile, objNetwork, objShell, strText, intAns
  4. Dim intConstants, intTimeout, strTitle, intCount, blnLog
  5. Dim strUserName, strComputerName, strIP, strShare, strLogFile
  6.  
  7. strShare = "G:\"
  8. strLogFile = "userlogintime.log"
  9. intTimeout = 100
  10.  
  11. Set objFSO = CreateObject("Scripting.FileSystemObject")
  12. Set objNetwork = CreateObject("Wscript.Network")
  13. Set objShell = CreateObject("Wscript.Shell")
  14.  
  15. strUserName = objNetwork.UserName
  16. strComputerName = objNetwork.ComputerName
  17. strIP = Join(GetIPAddresses())
  18.  
  19. ' Log date/time, user name, computer name, and IP address.
  20. If objFSO.FolderExists(strShare) Then
  21. On Error Resume Next
  22. Set objLogFile = objFSO.OpenTextFile(strShare & "\" _
  23. & strLogFile, 8, True, 0)
  24. If Err.Number = 0 Then
  25. ' Make three attempts to write to log file.
  26. intCount = 1
  27. blnLog = False
  28. Do Until intCount = 3
  29. objLogFile.WriteLine "Logon , " & Month & " , " _
  30. & strComputerName & " , " & strUserName & " , " & strIP
  31. If Err.Number = 0 Then
  32. intCount = 3
  33. blnLog = True
  34. Else
  35. Err.Clear
  36. intCount = intCount + 1
  37. If Wscript.Version > 5 Then
  38. Wscript.Sleep 200
  39. End If
  40. End If
  41. Loop
  42. On Error GoTo 0
  43. If blnLog = False Then
  44. strTitle = "Logon Error"
  45. strText = "Log cannot be written."
  46. strText = strText & vbCrlf _
  47. & "Another process may have log file open."
  48. intConstants = vbOKOnly + vbExclamation
  49. intAns = objShell.Popup(strText, intTimeout, strTitle, _
  50. intConstants)
  51. End If
  52. objLogFile.Close
  53. Else
  54. On Error GoTo 0
  55. strTitle = "Logon Error"
  56. strText = "Log cannot be written."
  57. strText = strText & vbCrLf & "User may not have permissions,"
  58. strText = strText & vbCrLf & "or log folder may not be shared."
  59. intConstants = vbOKOnly + vbExclamation
  60. intAns = objShell.Popup(strText, intTimeout, strTitle, intConstants)
  61. End If
  62. Set objLogFile = Nothing
  63. End If
  64.  
  65. ' Clean up and exit.
  66. Set objFSO = Nothing
  67. Set objNetwork = Nothing
  68. Set objShell = Nothing
  69.  
  70. Wscript.Quit
  71.  
  72. Function GetIPAddresses()
  73. Dim sh, fso, env, workfile, ts, data, index, n, arIPAddress, parts
  74.  
  75. set sh = createobject("wscript.shell")
  76. set fso = createobject("scripting.filesystemobject")
  77. Set Env = sh.Environment("PROCESS")
  78. if Env("OS") = "Windows_NT" then
  79. workfile = Env("TEMP") & "\" & fso.gettempname
  80. sh.run "%comspec% /c ipconfig >" & Chr(34) _
  81. & workfile & Chr(34),0,true
  82. else
  83. 'winipcfg in batch mode sends output to
  84. 'filename winipcfg.out
  85. workfile = "winipcfg.out"
  86. sh.run "winipcfg /batch" ,0,true
  87. end if
  88. set sh = nothing
  89. set ts = fso.opentextfile(workfile)
  90. data = split(ts.readall,vbcrlf)
  91. ts.close
  92. set ts = nothing
  93. fso.deletefile workfile
  94. set fso = nothing
  95. arIPAddress = array()
  96. index = -1
  97. for n = 0 to ubound(data)
  98. if instr(data(n),"IP Address") then
  99. parts = split(data(n),":")
  100. 'if trim(parts(1)) <> "0.0.0.0" then
  101. if instr(trim(parts(1)), "0.0.0.0") = 0 then
  102. index = index + 1
  103. ReDim Preserve arIPAddress(index)
  104. arIPAddress(index)= trim(cstr(parts(1)))
  105. end if
  106. end if
  107. next
  108. GetIPAddresses = arIPAddress
  109. End Function


Moderator's Note: Syntax highlighting added by Nilpo.

Last edited by Nilpo : November 19th, 2007 at 01:44 AM.

Reply With Quote
  #2  
Old November 19th, 2007, 01:55 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Novice (500 - 999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 675 Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Days 18 h 35 m 6 sec
Reputation Power: 308
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo
MySpace
This can certainly be done, however, before going any further there are several things in your code that need to be fixed. I'm reworking your code now and I'll post back with explanations and your solution.
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Reply With Quote
  #3  
Old November 19th, 2007, 02:27 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Novice (500 - 999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 675 Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)Nilpo User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1Folding Points: 156663 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Days 18 h 35 m 6 sec
Reputation Power: 308
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo
MySpace
To begin, you do not need to recreate the WshShell or FileSystemObject in your function. These have already been made available globally and the same objects should be reused. Also, in VBS there is no need to use the Set = Nothing method of unloading objects in a subroutine or method. Since these objects are only available within the current scope, they are automatically released when exiting a subroutine or function.

Second, the Join function you are using to create a string from your array elements requires a second parameter to indicate a delimiter. If you want to push them together without separating them in the string, you should pass an empty ( "" ) string as a parameter instead.

It looks to me like you're attempting to log the system's current date and time. You're using the Month function but this is only getting you part way to where you want to be. VBScript's Now function returns a string containing the current date and time. Use this instead.

Next, I see you are trying to disable internal error handling by using the On Error Resume Next and On Error Goto 0 statements. You're approach is fine, however your placement is not. You are disabling error handling before you enter and IF statement, and then re-enabling it part way through. You should enable and disable at the same block level. If you disable before an IF statement, then you re-enable after. Alternatively, keep it to within the IF or ELSE block. Since your usage only excludes the production of a message box, I see no reason not to do this before and after the entire IF statement.

On the same lines, you are using the Err object as a conditional device. While this is a great device for constructing statements that only execute upon successful completion of another task, your usage again is not correct...

...Remember that the Err object will be fired for EVERY script error, even those that are non-fatal and do not cause execution to stop. Since this value changes so frequently, you need to control it before polling for its value. To do this, make sure that it is cleared IMMEDIATELY before whatever line or lines you wish to monitor. The Err object provides a method for this.

There is also no real purpose for using the Clear method when you are not actively monitoring the contents of the Err object. I've removed an extraneous call to this method that you had in the Else block of one of your IF statements.

Finally, be careful with your use of DIM statements. These are used to create memory allocations for objects or variables. Using them inside of a function or subroutine will only make them available within their scope. Further, adding a DIM statement after an object is created will clear its contents. The use of Option Explicit further complicates the use of these statements so be careful to get them in the right place. If you're not sure, you can always put them at the very top of your script. When using Option Explicit, you also need to be sure not to miss ANY Dim statements.

vb Code:
Original - vb Code
  1. Option Explicit
  2.  
  3. Dim objFSO, objLogFile, objNetwork, objShell, strText, intAns
  4. Dim intConstants, intTimeout, strTitle, intCount, blnLog
  5. Dim strUserName, strComputerName, strIP, strShare, strLogFile
  6.  
  7. strShare = "G:\"
  8. strLogFile = "userlogintime.log"
  9. intTimeout = 100
  10.  
  11. Set objFSO = CreateObject("Scripting.FileSystemObject")
  12. Set objNetwork = CreateObject("Wscript.Network")
  13. Set objShell = CreateObject("Wscript.Shell")
  14.  
  15. strUserName = objNetwork.UserName
  16. strComputerName = objNetwork.ComputerName
  17. strIP = Join(GetIPAddresses(), "")
  18.  
  19. ' Log date/time, user name, computer name, and IP address.
  20. If objFSO.FolderExists(strShare) Then
  21.     On Error Resume Next
  22.    
  23.     Err.Clear
  24.  
  25.     Set objLogFile = objFSO.OpenTextFile(strShare & "\" & strLogFile, 8, True, 0)
  26.  
  27.     If Err.Number = 0 Then
  28.         ' Make three attempts to write to log file.
  29.         intCount = 1
  30.         blnLog = False
  31.         Do Until intCount = 3
  32.             Err.Clear
  33.             objLogFile.WriteLine "Logon , " & Now & " , " & strComputerName & " , " & strUserName & " , " & strIP
  34.             If Err.Number = 0 Then
  35.                 intCount = 3
  36.                 blnLog = True
  37.             Else
  38.                 intCount = intCount + 1
  39.                 If WScript.Version > 5 Then
  40.                     WScript.Sleep 200
  41.                 End If
  42.             End If
  43.         Loop
  44.         If blnLog = False Then
  45.             strTitle = "Logon Error"
  46.             strText = "Log cannot be written."
  47.             strText = strText & VbCrLf & "Another process may have log file open."
  48.             intConstants = vbOKOnly + vbExclamation
  49.             intAns = objShell.Popup(strText, intTimeout, strTitle, intConstants)
  50.         End If
  51.         objLogFile.Close
  52.     Else
  53.         strTitle = "Logon Error"
  54.         strText = "Log cannot be written."
  55.         strText = strText & VbCrLf & "User may not have permissions,"
  56.         strText = strText & VbCrLf & "or log folder may not be shared."
  57.         intConstants = vbOKOnly + vbExclamation
  58.         intAns = objShell.Popup(strText, intTimeout, strTitle, intConstants)
  59.     End If
  60.     On Error Goto 0
  61. End If
  62.  
  63. ' Clean up and exit.
  64. Set objLogFile = Nothing
  65. Set objFSO = Nothing
  66. Set objNetwork = Nothing
  67. Set objShell = Nothing
  68.  
  69. WScript.Quit
  70.  
  71. Function GetIPAddresses()
  72.     Dim env, workfile, ts, data, arIPAddress, parts, index, n
  73.    
  74.     Set env = objShell.Environment("PROCESS")
  75.    
  76.     If env("OS") = "Windows_NT" Then
  77.         workfile = env("TEMP") & "\" & objFSO.gettempname
  78.         objShell.run "%comspec% /c ipconfig >" & Chr(34) & workfile & Chr(34),0,True
  79.     Else
  80.         'winipcfg in batch mode sends output to
  81.         'filename winipcfg.out
  82.         workfile = "winipcfg.out"
  83.         objShell.run "winipcfg /batch" ,0,True
  84.     End If