/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" 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 "," Tuning MSH in your Profile "," ")

Sunday, December 04, 2005

 


MSH Show-Byte Function



Sometimes you need read or set a options property that is a Byte.
then depending on the bits enabled in the byte options are On or Off.

but if you get the byte you will get a Number from 0 to 255.

the script below will create the Function show-Byte, that will show you a table of the Bits set, in the Byte Given.

So that you can easy see what properties are set.

You can use it like this :

MSH>show-byte 23
─────────────
bn = Bit Number
bv = Bit Value
Wght = Bit Weight
value = Value
─────────────
bn bv Wght value
┌─┐┌─┐┌───┐┌───┐
7││0││128││ 0
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
6││0││ 64││ 0
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
5││0││ 32││ 0
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
4││1││ 16││ 16
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
3││0││ 8││ 0
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
2││1││ 4││ 4
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
1││1││ 2││ 2
└─┘└─┘└───┘└───┘
┌─┐┌─┐┌───┐┌───┐
0││1││ 1││ 1
└─┘└─┘└───┘└───┘
───
Total: 23


in this example you can see that a value of 23 means that option 0,1,2 and 4, are enabled.

enjoy,

gr /\/\o\/\/


function Show-Byte ([byte]$byte) {
  $bits = @(1);$(for ($i = 1 ; $i -lt 8; $i++) {$bits += [int][math]::pow(2,$i)})
  $bitArray = @()
  $ValArray = @()
  $bits | sort -desc | foreach {
    if (($byte / $_) -ge 1){
      $ValArray += $_
      $bitArray += 1 
      $byte -= $_
    }Else{
      $bitArray += 0
      $ValArray += 0
    }
  }
  # Generate Byte Table
  "─────────────"
  "bn = Bit Number"
  "bv = Bit Value"
  "Wght = Bit Weight"
  "value = Value"
  "─────────────"
  "bn bv Wght value"
  for ($i = 7 ; $i -ge 0 ;$i--){
    $ShowArray = @"
┌─┐┌─┐┌───┐┌───┐
│$i││$($bitArray[(7 - $i)])││$(($bits[$i]).tostring().padleft(3))││$(($ValArray[(7 - $i)]).tostring().padleft(3))│
└─┘└─┘└───┘└───┘
"@
    $ShowArray
  }
  "            ───"
  "Total:      $((($ValArray | measure-object -sum).sum).tostring().padleft(3))"
}



Comments:
Blogger /\/\o\/\/
if you get this error :

Unrecognized token in source text.
At line:24 char:18
+ $ShowArray = @ <<<< "

remove a Space after :

$ShowArray = @"

then it will Work.

gr /\/\o\/\/

PS it also look like in some RSS readers, Some part of the text is missing (in mine Thunderbird for example) from the place where the first special characters come, please use the site then.
 
Blogger /\/\o\/\/
Made it in a One-liner :

MSH>$b = 23 ; for ($i=0;$i -lt 8;$i++){if ($b -band $bits[$i]) {"$i Set"} else {"$i"} }
0 Set
1 Set
2 Set
3
4 Set
5
6
7

how I came to this and the impact on the AD logonTimes script (Next post) I will blog about tomorrow

(Oops Later today ;-)

gr /\/\o\/\/
 
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?