Created
February 24, 2014 13:11
-
-
Save kardeiz/9188152 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module PSTerminalServices | |
Function OctetToHours ($Octet) | |
{ | |
# Function to convert Octet value (byte array) into binary string | |
# representing logonHours attribute. The 168 bits represent 24 hours | |
# per day for 7 days, Sunday through Saturday. The values are converted | |
# into local time. If the bit is "1", the user is allowed to logon | |
# during that hour. If the bit is "0", the user is not allowed to logon. | |
$Bias = (Get-ItemProperty -Path HKLM:\System\CurrentControlSet\Control\TimeZoneInformation).Bias | |
# Account for negative bias. | |
If ($Bias -gt 10080){$Bias = $Bias - 4294967296} | |
$Bias = [Math]::Round($Bias/60, 0, [MidpointRounding]::AwayFromZero) | |
# Create an array of 168 bytes, representing the hours in a week. | |
$LH = New-Object 'object[]' 168 | |
For ($j = 0; $j -le 20; $j = $j + 1) | |
{ | |
For ($k = 7; $k -ge 0; $k = $k - 1) | |
{ | |
$m = 8*$j + $k - $Bias | |
If ($m -lt 0) {$m = $m + 168} | |
If ($m -gt 167) {$m = $m - 168} | |
If ($Octet[$j] -band [Math]::Pow(2, $k)) {$LH[$m] = "1"} | |
Else {$LH[$m] = "0"} | |
} | |
} | |
For ($j = 0; $J -le 20; $j = $J + 1) | |
{ | |
$n = 8*$j | |
If ($j -eq 0) {$Hours = [String]::Join("", $LH[$n..($n + 7)])} | |
Else {$Hours = $Hours + [String]::Join("", $LH[$n..($n + 7)])} | |
} | |
Return $Hours | |
} | |
Function MemberOfUserGroup ( $user ) { | |
$user.memberOf.contains('CN=SLL - Remote Desktop Users,OU=Security,OU=SLL - Groups,DC=sll,DC=local') | |
} | |
Function LogonHoursHelper ( $user, $date ) { | |
$hours = OctetToHours $user.logonHours | |
[int]$day_of_week = $date | Get-Date -UFormat %u | |
[int]$hour_of_day = $date | Get-Date -UFormat %H | |
$position = $day_of_week * 24 + $hour_of_day | |
Return ($hours[$position] -eq '0') | |
} | |
Function LogonHoursExpiring ( $user ) { | |
LogonHoursHelper $user, (Get-Date).AddMinutes(5) | |
} | |
Function LogonHoursExpired ( $user ) { | |
LogonHoursHelper $user, (Get-Date) | |
} | |
Filter BaseFilter { | |
if ( ($name = $_.UserName) -and ($name -ne '') -and ($_.State -eq 'Active') ) { | |
$user = Get-ADUser -Identity $name -Properties memberOf, logonHours | |
$member_of = MemberOfUserGroup $user | |
$hours_expiring = LogonHoursExpiring $user | |
if ( $member_of -and $hours_expiring ) { Return $_ } | |
} | |
$Message = "Important`nYour session is ending in five minutes. Please save your work and logoff." | |
Get-TSSession -ComputerName 'LENDINGLIBRARY1' | BaseFilter | Send-TSMessage -Text $Message | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment