This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
As I posted the following link in the NG as an example of getting the Logonhours from AD, I did think this would be a nice script to rebuild to a MSH script.
Document LogonHours http://www.rlmueller.net/Programs/LogonHours.txtthe reason I picked this script is that I did think this would be a lot more easy todo in MSH.
And...,Yep...
It only took me 20 lines to do it in MSH, as the original script was more as 2 pages.
(OK, I did leave out the long remarks in the beginning and the Commandline Help, and kept it a bit more basic but still the output is the same and you can see the power of Monad working here too ;-) .)
for Example Check the Timezone check in the Original script and the MSH version.
(as the info stored in the Byte-Array is UTC-Time)
Also working with Byte-Array's in MSH is realy easy compared to VBscript.
(also you can easy write them also, see also
MSH registry access )
and ofcource the rest of the syntactional sugar of Monad.
for the rest I think the padding might be interesting and the usage of DayOfWeek .
I did leave the getting of the AD user object outside the script, to make it more general, you can give the AD User Object as a parameter to the Function.
so the Usage of the Script is As follows :
MSH>$de = new-object system.directoryservices.directoryentry("cn=mow,ou=mowou,dc=mow,dc=adam")
MSH> get-Logonhours $de
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Sunday 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Monday 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1
Tuesday 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Wednesday 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Thursday 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Friday 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Saturday 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
MSH>
as you can see it Mimics the Normal view in AD U&C :
Note, the in the MS View a 1 below an hour means that the user is allowed to log on, from that hour to the next e.g. a 1 below 5 means allowed to logon from 5:00 till 6:00
so you can easy see the allowed logon Times from script.
so then now the script :
# Function Get-Logonhours
# Gets the Allowed logonhour of a given AD user
# /\/\o\/\/ 2005
Function get-logonHours ([system.directoryservices.directoryentry]$de){
$bits = @(1);$(for ($i = 1 ; $i -lt 8; $i++) {$bits += [int][math]::pow(2,$i)})
$bitArray = @()
# Fill BitArray
foreach ($byte in $de.logonHours[0]) {
$TB = @()
$bits | sort -desc | foreach {
if (($byte / $_) -ge 1){
$TB += 1
$byte -= $_
}Else{
$TB += 0
}
}
for ($i = 7 ; $i -ge 0 ; $i--){$bitArray += $tb[$i]}
}
# Change from UTC to Local Timezone
$o = [timezone]::CurrentTimeZone.GetUtcOffset((get-date)).TotalHours
$Local = $bitArray[(0 - $o)..(167 - $o)]
if ($o -lt 0) {for ($i = 0 ; $i -lt [math]::abs($o); $i++) {$local += $bitArray[$o - 1]}}
# Display Table
$hours = @();for ($i = 0 ; $i -lt 24 ; $i++) {$hours += $i.tostring().padleft(3)}
" $([string]::join('',$hours))"
for ($i = 0 ; $i -lt 7 ; $i++) {
"$(([dayOfWeek]::getnames([dayofweek])[$i]).PadRight(10)) $([string]::join(' ',$Local[(24 * $I)..(24 * $I + 23)]))"
}
}
Enjoy,
greetings /\/\o\/\/
PS if the logonhour of a user are never changed the value of the Logonhours property can be $null !!