/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH Show-Byte Function "," Let monad "drop" some chars "," getting an Inputbox in MOnad "," Get AD info into a nested HashTable from MSH "," Logging your MSH session "," MSH get-dateFormat function "," Working with a Path in MSH "," MSH MSN Instant messenger project (broken off) "," Running (Un-block) a downloaded script in MSH "," Timing the loading of your profile in MSH "," ")

Monday, December 05, 2005

 


get AD user allowed Logonhours from MSH



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.txt


the 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 !!


Comments: Post a Comment



<< Home

Archives

October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006  

$Links = ("PowerShell RC1 Docs"," PowerShell RC1 X86"," PowerShell RC1 X64"," Monad GettingStarted guide"," Monad Progamming Guide"," Monad SDK"," Monad videos on Channel 9"," MSH Community Workspace"," scripts.readify.net "," MonadSource"," www.reskit.net"," PowerShell Blog"," Under The Stairs"," computerperformance powershell Home"," proudlyserving"," MSH on wikipedia"," MSHWiki Channel 9"," Keith Hill's Blog"," Precision Computing"," PowerShell for fun"," MSH Memo (Japanese)"," monadblog")

find-blog -about "PowerShell","Monad" | out-Technorati.
find-blog -contains "","" | out-Technorati.
Google
 
Web mow001.blogspot.com

This page is powered by Blogger. Isn't yours?