Control logon duration for users


posted by erenturkm
12-30-2010

Downloads: 361
File size: 14.2kB
Views: 1,907

Embed
Control logon duration for users
  1. #**************************************************** 
  2. #        LogoffTimer 
  3. #      by Murat Erenturk 
  4. #        December 2010 
  5. #        Version 1.0 
  6. #***************************************************** 
  7. function GetTimeSpanDescription 
  8. param([TimeSpan] $TS
  9.     $Desc="" 
  10.     if ($TS.days -gt 0) {$Desc=$Desc+$TS.days+" days,"
  11.     if ($TS.hours -gt 0) {$Desc=$Desc+$TS.hours+" hours,"
  12.     if ($TS.minutes -gt 0) {$Desc=$Desc+$TS.minutes+" minutes,"
  13.     if ($TS.seconds -gt 0) {$Desc=$Desc+$TS.seconds+ " seconds"
  14.     return $Desc 
  15. #***************************************************** 
  16. Function Test-RegistryValue($regkey, $name)  
  17. {   
  18.     return(Get-ItemProperty $regkey $name -ErrorAction SilentlyContinue | Out-Null)   
  19. #***************************************************** 
  20. # MAIN 
  21. #***************************************************** 
  22. # Global Variables         
  23. $AllowedDailyMinutes=60 
  24. $AllowedSessionMinutes=20 
  25. $AllowedRelogonTimeSpan=new-TimeSpan -minutes 20 
  26. $username=$env:username 
  27. $HomeDir=$Env:HomeDrive + $env:homepath 
  28. $Date=(get-date).ToString('yyyyMMdd'
  29. $LogFileName=$HomeDir + '\' + $Date +'LogoffTimer.txt' 
  30. $LoggingLevel=
  31.  
  32. # Open log file 
  33. $LogFile=New-item -type file $LogFileName -force         
  34. $Now=(get-date
  35. add-content $logfile "$Username logging on at $Now"  
  36.  
  37. #Open registry 
  38. $UsedDailyMinutes=
  39. $DailyLogonCount=
  40. $LastLogon=(get-date -year 2010 -month 12 -day 26) 
  41. $UsedSessionMinutes=
  42. if ((test-path "hkcu:\software\erenturk") -eq $false) {new-item -path hkcu:\software\erenturk} 
  43. if ((test-path "hkcu:\software\erenturk\LogoffTimer") -eq $false)  
  44.     if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry Key LogoffTimer does not exist, will create values"}  
  45.     new-item -path hkcu:\software\erenturk\LogoffTimer 
  46. # read variable UsedDailyminutes 
  47. if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","UsedDailyMinutes"  -eq $False
  48.     if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value UsedDailyMinutes does not exist, will create value"
  49.     new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes 
  50. else 
  51.     $UsedDailyMinutes=(Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "UsedDailyMinutes").UsedDailyMinutes 
  52. # read variable DailyLogonCount 
  53. if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","DailyLogonCount"  -eq $False
  54.     if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value DailyLogonCount does not exist, will create value"
  55.     new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "DailyLogonCount" -value $DailyLogonCount 
  56. else 
  57.     $DailyLogonCount=[int](Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "DailyLogonCount").DailyLogonCount 
  58. # read variable LastLogon 
  59. if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","LastLogon" -eq $False
  60.     if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value LastLogon does not exist, will create value"
  61.     new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "LastLogon" -value $LastLogon 
  62. else 
  63.     $LastLogon=[system.datetime](Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "LastLogon").LastLogon 
  64. # read variable UsedSessionMinutes 
  65. if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","UsedSessionMinutes"  -eq $False
  66.     if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value UsedSessionMinutes does not exist, will create value"
  67.     new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes 
  68. else 
  69.     $UsedSessionMinutes=[int](Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "UsedSessionMinutes").UsedSessionMinutes 
  70. $lastLogonDelta=New-TimeSpan -start $LastLogon -end $now 
  71. $lastLogonDeltaDesc=GetTimeSpanDescription($lastLogonDelta
  72.  
  73. if ($LastLogon.day -eq $now.day
  74.     add-content $logfile "$username has logged on for $UsedDailyMinutes minutes Today, $DailyLogonCount times and last logged $lastLogonDeltaDesc ago" 
  75.     add-content $logfile "$username has used $usedSessionMinutes minutes session time on last logon" 
  76.     $DailyLogonCount=$DailyLogonCount+
  77.     set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "DailyLogonCount" -value $DailyLogonCount 
  78. else 
  79.     if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Day has passed since logon, reseting counters"
  80.     set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "DailyLogonCount" -value $DailyLogonCount 
  81.     set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes 
  82.     set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes 
  83.     add-content $logfile "$username is logging first time today,last logged on $lastLogonDeltaDesc ago" 
  84.  
  85. # Starting logon operations 
  86. set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "LastLogon" -value (get-date
  87.  
  88. # Check if enough time has passed after previous logons 
  89. $SessionTimeLeft=$AllowedSessionMinutes-$usedSessionMinutes 
  90. if ($AllowedRelogonTimeSpan -gt $lastLogonDelta)  
  91.  
  92.     if ($SessionTimeLeft -eq 0) 
  93.     
  94.         $imeTillRelogon=New-TimeSpan $(Get-Date -hour $lastLogonDelta.hours -minute $LastLogonDelta.minutes) $(get-date -hour $AllowedRelogonTimeSpan.hours -minute $AllowedRelogonTimeSpan.minutes
  95.         $TimeTillRelogonDesc=GetTimeSpanDescription($imeTillRelogon
  96.         add-content $logfile "There is still $TimeTillrelogonDesc to next logon, user will be logged off" 
  97.         logoff 
  98.     
  99.     else 
  100.     
  101.         add-content $logfile "User will be allowed to logon for $SessionTimeLeft minutes" 
  102.     
  103. else 
  104. # User has finished session, waited relogon interval and is logging again 
  105.     $UsedSessionMinutes=
  106.     set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes 
  107.  
  108. #Check if user has reached daily logon time limit 
  109. $DailyTimeLeft=$AllowedDailyMinutes-$UsedDailyMinutes 
  110. if ($DailyTimeLeft -eq 0) 
  111.     add-content $logfile "Daily allowance is reached, user will be logged off" 
  112.     Logoff 
  113.  
  114. $EndTime=$now.addMinutes($SessionTimeLeft
  115. $TimeSpan=new-timespan $now $EndTime 
  116. while ($timeSpan -gt 0) 
  117. $timeSpan = new-timespan $(get-date) $endTime 
  118. sleep -Seconds 60 
  119. $UsedDailyMinutes=$UsedDailyMinutes+
  120. $usedSessionMinutes=$UsedSessionMinutes+
  121. set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes 
  122. set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes 
  123. $Remaining=GetTimeSpanDescription($timeSpan
  124. add-content $logfile "$Remaining remaining..."   
  125. $UsedDailyMinutes=$UsedDailyMinutes+
  126. $usedSessionMinutes=$UsedSessionMinutes+
  127. set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes 
  128. set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes 
  129. add-content $logfile "Session time allowance is reached, user will be logged off" 
  130. logoff 

This script can be used as a logon script and works as a logoff timer. It works in the background and is designed for restricted user environment. If you want to see a detailed description, see by blog post at http://blogs.technet.com/b/erenturk/archive/2010/12/30/how-do-you-control-computer-usage-habits-for-children.aspx

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.