Posts
49
Comments
83
Trackbacks
0
November 1999 Entries
Something fun, totally useless but educational before I go…
Ok, I couldn't help myself. One more post. Here's a script that is totally totally useless, but will teach you about a few features in PowerShell scripting:
  • Script blocks. These are *incredibly* powerful. If you master them, you will be able to do some pretty fantastic things.
  • Host APIs. Ever wanted to control your output (color, location of text etc.)? This will show you how to do that.
  • Passing in parameters into scripts. This is a great way to make your scripts generic, while making them have intelligent defaults.

What does the script do? It makes pretty pictures in the console :). Its basically a demo screensaver. The script takes scripts blocks as input, each script block is basically a functor which is used to define the coordinates of the console cursor. So the default function is basically x = x + 1 and y = y + 1, but because we use the param statement to make the script generic, you can define it to be anything! So try stuff like this:

drawit { $cx + 1 } { $cy + 2 }  -chars "."

Btw: you can also control the milliseconds between the draws and also the characters you want to use. Here is the script.

param( [ScriptBlock]$fx={$cX + 1} ,[ScriptBlock]$fy={$cY + 1}, $chars = @("_", "|", " ") , $m = 1 )

$wH = $host.ui.RawUI.WindowSize.Height - 1 $wW = $host.ui.RawUI.WindowSize.Width - 1
$random = new-object System.Random
$coords = new-object management.automation.host.coordinates [Console]::Clear()

#
$host.UI.RawUI.CursorSize = 0
$global:cX = $random.Next(0,($wW))
$global:cY = $random.Next(0,($wH))  
$fore = $random.Next(0, 15)
$back = "black"

while (1)
{  
    sleep -m $m  $cX = &$fx  $cY = &$fy    
 
    if( $cX -gt $wW -or $cX -eq 0)  
    {   
        $cX = $random.Next(0,($wW))   
        $fore = $random.Next(0, 15)  
    }  

    if( $cY -gt ($wH-1) -or $cY -eq 0)  
    {   
        $cY = $random.Next(0,($wH))   
       
#$back = $random.Next(0, 15)  
    }

    $coords.x = $cX  $coords.y = $cY  

    trap { $coords }  

    [void] $host.UI.RawUI.Set_CursorPosition( $coords )  
    write-host -fore $fore -back $back  $chars[ $random.Next(0, $chars.Length) ]
}

posted @ Tuesday, November 30, 1999 12:11 AM | Feedback (0)
And these are some of my favorite scripts… Part 1
I'm starting a new series that showcase different features of PowerShell and Exchange by posting scripts first and then letting you (the readers) figure out what the script does. I'll update the post with an explanation of the scripts after some time, but first, use the comments to post your solutions / questions / complaints :) The idea is to first a) get people to learn how to read scripts and then b) learn how to write them. As a side effect, there will be some new scripts out there for you to use (some will be just generic PowerShell and some Exchange specifc)  To kick things off,  here is a script that uses begin/end/process blocks and hashes to do something useful. Can you guess what it does? param($property=$null)begin {  $script:hash = @{} } process {  if(($script:property) -and ( $_.$script:property ))  {   $obj = ($_ | select-object $script:property).$script:property  }  else  {   $obj = $_   $script:property = "Name"  }  if( $script:hash.$obj -eq $null )  {   $script:hash.$obj = @(1, $_);  }  else  {   $script:hash.$obj[0]++  } } end {  foreach($key in $script:hash.keys | sort)  {   $result = new-object System.Management.Automation.PsObject   add-member -input $Result -type Note* -Name Input -value $script:hash.$Key[1]   add-member -input $Result -type Note* -Name $script:property -value $Key   add-member -input $Result -type Note* -Name Count -value $script:hash.$Key[0]   write-output $result  } }

posted @ Tuesday, November 30, 1999 12:11 AM | Feedback (0)
News
A little slow these days as I'm busy working on exchangelabs.com. I will try and post tidbits when I get some time. Enjoy the older posts till then!