 |
Control logon duration for users |
-
-
-
-
-
-
- function GetTimeSpanDescription
- {
- param([TimeSpan] $TS)
- $Desc=""
- if ($TS.days -gt 0) {$Desc=$Desc+$TS.days+" days,"}
- if ($TS.hours -gt 0) {$Desc=$Desc+$TS.hours+" hours,"}
- if ($TS.minutes -gt 0) {$Desc=$Desc+$TS.minutes+" minutes,"}
- if ($TS.seconds -gt 0) {$Desc=$Desc+$TS.seconds+ " seconds"}
- return $Desc
- }
-
- Function Test-RegistryValue($regkey, $name)
- {
- return(Get-ItemProperty $regkey $name -ErrorAction SilentlyContinue | Out-Null)
- }
-
-
-
-
- $AllowedDailyMinutes=60
- $AllowedSessionMinutes=20
- $AllowedRelogonTimeSpan=new-TimeSpan -minutes 20
- $username=$env:username
- $HomeDir=$Env:HomeDrive + $env:homepath
- $Date=(get-date).ToString('yyyyMMdd')
- $LogFileName=$HomeDir + '\' + $Date +'LogoffTimer.txt'
- $LoggingLevel=3
-
-
- $LogFile=New-item -type file $LogFileName -force
- $Now=(get-date)
- add-content $logfile "$Username logging on at $Now"
-
-
- $UsedDailyMinutes=0
- $DailyLogonCount=1
- $LastLogon=(get-date -year 2010 -month 12 -day 26)
- $UsedSessionMinutes=0
- if ((test-path "hkcu:\software\erenturk") -eq $false) {new-item -path hkcu:\software\erenturk}
- if ((test-path "hkcu:\software\erenturk\LogoffTimer") -eq $false)
- {
- if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry Key LogoffTimer does not exist, will create values"}
- new-item -path hkcu:\software\erenturk\LogoffTimer
- }
-
- if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","UsedDailyMinutes" -eq $False)
- {
- if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value UsedDailyMinutes does not exist, will create value"}
- new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes
- }
- else
- {
- $UsedDailyMinutes=(Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "UsedDailyMinutes").UsedDailyMinutes
- }
-
- if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","DailyLogonCount" -eq $False)
- {
- if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value DailyLogonCount does not exist, will create value"}
- new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "DailyLogonCount" -value $DailyLogonCount
- }
- else
- {
- $DailyLogonCount=[int](Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "DailyLogonCount").DailyLogonCount
- }
-
- if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","LastLogon" -eq $False)
- {
- if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value LastLogon does not exist, will create value"}
- new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "LastLogon" -value $LastLogon
- }
- else
- {
- $LastLogon=[system.datetime](Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "LastLogon").LastLogon
- }
-
- if (Test-RegistryValue "hkcu:\software\erenturk\LogoffTimer","UsedSessionMinutes" -eq $False)
- {
- if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Registry value UsedSessionMinutes does not exist, will create value"}
- new-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes
- }
- else
- {
- $UsedSessionMinutes=[int](Get-itemProperty -path hkcu:\software\erenturk\LogoffTimer -name "UsedSessionMinutes").UsedSessionMinutes
- }
- $lastLogonDelta=New-TimeSpan -start $LastLogon -end $now
- $lastLogonDeltaDesc=GetTimeSpanDescription($lastLogonDelta)
-
- if ($LastLogon.day -eq $now.day)
- {
- add-content $logfile "$username has logged on for $UsedDailyMinutes minutes Today, $DailyLogonCount times and last logged $lastLogonDeltaDesc ago"
- add-content $logfile "$username has used $usedSessionMinutes minutes session time on last logon"
- $DailyLogonCount=$DailyLogonCount+1
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "DailyLogonCount" -value $DailyLogonCount
- }
- else
- {
- if ($LoggingLevel -gt 2) {add-content $LogFile "INFO: Day has passed since logon, reseting counters"}
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "DailyLogonCount" -value $DailyLogonCount
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes
- add-content $logfile "$username is logging first time today,last logged on $lastLogonDeltaDesc ago"
- }
-
-
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "LastLogon" -value (get-date)
-
-
- $SessionTimeLeft=$AllowedSessionMinutes-$usedSessionMinutes
- if ($AllowedRelogonTimeSpan -gt $lastLogonDelta)
- {
-
- if ($SessionTimeLeft -eq 0)
- {
- $imeTillRelogon=New-TimeSpan $(Get-Date -hour $lastLogonDelta.hours -minute $LastLogonDelta.minutes) $(get-date -hour $AllowedRelogonTimeSpan.hours -minute $AllowedRelogonTimeSpan.minutes)
- $TimeTillRelogonDesc=GetTimeSpanDescription($imeTillRelogon)
- add-content $logfile "There is still $TimeTillrelogonDesc to next logon, user will be logged off"
- logoff
- }
- else
- {
- add-content $logfile "User will be allowed to logon for $SessionTimeLeft minutes"
- }
- }
- else
-
- {
- $UsedSessionMinutes=0
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes
- }
-
-
- $DailyTimeLeft=$AllowedDailyMinutes-$UsedDailyMinutes
- if ($DailyTimeLeft -eq 0)
- {
- add-content $logfile "Daily allowance is reached, user will be logged off"
- Logoff
- }
-
- $EndTime=$now.addMinutes($SessionTimeLeft)
- $TimeSpan=new-timespan $now $EndTime
- while ($timeSpan -gt 0)
- {
- $timeSpan = new-timespan $(get-date) $endTime
- sleep -Seconds 60
- $UsedDailyMinutes=$UsedDailyMinutes+1
- $usedSessionMinutes=$UsedSessionMinutes+1
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes
- $Remaining=GetTimeSpanDescription($timeSpan)
- add-content $logfile "$Remaining remaining..."
- }
- $UsedDailyMinutes=$UsedDailyMinutes+1
- $usedSessionMinutes=$UsedSessionMinutes+1
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedDailyMinutes" -value $UsedDailyMinutes
- set-itemProperty -path hkcu:\software\erenturk\logoffTimer -name "UsedSessionMinutes" -value $UsedSessionMinutes
- add-content $logfile "Session time allowance is reached, user will be logged off"
- logoff