Advertisement
Guest User

Timer.vbs - limit a user account's total usage time.

a guest
Sep 20th, 2010
2,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Dim objFSO, objShell, objTextFile
  4. Dim strText
  5. Dim prevDate
  6. Dim prevTime
  7. Dim timeLeft
  8. Const strDirectory = "C:"
  9. Const strFile = "\timelog.txt"
  10. Const checkTimeLeftVbs = "C:\checktimeleft.vbs"
  11. Const ForAppending = 8
  12. Const ForReading = 1
  13. Const ForWriting = 2
  14. Dim usageTime       'in minutes
  15.  
  16. Set objFSO = CreateObject("Scripting.FileSystemObject")
  17. Set objShell = WScript.CreateObject ("WScript.Shell")
  18.  
  19. Function openLogFile(N)
  20. Set objTextFile = objFSO.OpenTextFile(strDirectory & strFile, N, True)
  21. End Function
  22.  
  23. Function checkPrevInst()
  24. Call openLogFile(ForReading)
  25. prevDate = objTextFile.ReadLine
  26. prevTime = CInt(objTextFile.ReadLine)
  27. objTextFile.Close
  28. If prevDate <> "" Then
  29.     If DateDiff("d", prevDate, Date) >= 1 Then
  30.         Call logStartTime()
  31.     Else
  32.         'Continue monitoring
  33.         Call logTime()
  34.     End If
  35. Else
  36.     'Log file compromised...
  37.     Call logStartTime()
  38. End If
  39. End Function
  40.  
  41. Function logStartTime()
  42. Call openLogFile(ForWriting)
  43. objTextFile.WriteLine(Date)
  44. objTextFile.WriteLine("0")
  45. objTextFile.Close
  46. prevDate = Date
  47. prevTime = 0
  48. Call logTime()
  49. End Function
  50.  
  51. Function checkforChanges()
  52. Dim tempPrevDate
  53. Dim tempPrevTime
  54. Call openLogFile(ForReading)
  55. tempPrevDate = objTextFile.ReadLine
  56. tempPrevTime = CInt(objTextFile.ReadLine)
  57. objTextFile.Close
  58. If tempPrevDate = PrevDate Then
  59.     If tempPrevTime <> prevTime Then
  60.         prevTime = tempPrevTime
  61.         timeLeft = usageTime - prevTime
  62.         objShell.Run checkTimeLeftVbs
  63.     End If
  64. End If
  65. End Function
  66.  
  67. Function logTime()
  68. timeLeft = usageTime - prevTime
  69. objShell.Run checkTimeLeftVbs
  70. Do While timeLeft > 0
  71.     Call openLogFile(ForWriting)
  72.     objTextFile.WriteLine(prevDate)
  73.     objTextFile.WriteLine(prevTime)
  74.         objTextFile.WriteLine(timeLeft)
  75.     objTextFile.Close
  76.     WScript.Sleep 60000
  77.     Call checkforChanges()
  78.     prevTime = prevTime + 1
  79.     timeLeft = usageTime - prevTime
  80.     If timeLeft <= 5 Or timeLeft = 10 Or timeLeft = 15 Then
  81.         objShell.Run checkTimeLeftVbs
  82.     End If
  83. Loop
  84. 'Time exceeded
  85. Call timeExceeded()
  86. End Function
  87.  
  88. Function timeExceeded()
  89. Do While timeLeft <= 0
  90. Call endSession()
  91. WScript.Sleep 10000
  92. Loop
  93. End Function
  94.  
  95. Function endSession()
  96. objShell.Run "%windir%\SYSTEM32\rundll32.exe user32.dll,LockWorkStation", 0, False
  97. End Function
  98.  
  99. Function checkDay()
  100. If Weekday(Date, 1) <> "1" And Weekday(Date, 1) <> "6" And Weekday(Date, 1) <> "7" Then
  101.     usageTime = 60      'weekday time, in minutes
  102. Else
  103.     usageTime = 90      'weekend time, in minutes
  104. End If
  105. Call checkPrevInst()
  106. End Function
  107.  
  108. 'Main program
  109. Call checkDay()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement