TECHED: Start-Session Script

PowerShell Team

Before my TechEd sessions, I ran a Windows PowerShell script which used animated figures to inform the users how long it would be until the session would start.  It threw out some wisecracks along the way to keep things light and maintain their attention.  

A number of people asked me to post the script in the blog.  I’m a little reticent to do so as

  1. I threw the script together on the day before the session and then tweaked it right before the session so it is not a well thought out script.
  2. I don’t really know how to program against the MSAgent Control and I’m pretty sure that I’m doing it incorrectly.  I just created the dang thing and then started exploring and made a few lucky guesses.  I notice that if I crank the Sleep time down, I get strange behavior so it clearly is not correct. 

I’m going to share it anyway because people asked for it and because there are some cool techniques you might want to know about.  There are actually 2 scripts.  Start-Session and Invoke-MSAgent

I’ve included the content for both scripts below and have attached the Start-Session Script (blogging software doesn’t seem to let me attach 2 files – go figure!)

Enjoy!

Jeffrey Snover
Windows PowerShell Architect

#########  Start-Session.ps1 #########################

#PSMDTAG:Author:Jsnover
#PSMDTAG:Title:Start-Session
#PSMDTAG:Description:Sample script to start a session
#PSMDTAG:USE:Agent.Control

param( [DateTime]$StartTime, $SessionTitle=”Session”, $Speaker=”Jeffrey”)

function Invoke-Display()
{
    Invoke-MSAgent $Message -Character (Get-RandomElement $Characters) `
        -Size $CharacterSize `
        -MoveToX $Random.Next(500) -MoveToY $Random.Next(500) `
        -StartX  $Random.Next(500) -StartY  $Random.Next(500)
    Start-Sleep $SleepTime
}

function Get-RandomElement($Array)
{
   $Array[ $Random.Next( $Array.Count ) ]
}

$Sleeptime = 20
$CharacterSize = 250
$WiseCracks=(
    “PowerShell Rocks baby”,
    # I know that this is misspelled but it has to be to sound correct
    “Hay Hay, My My, the CLI will never die”,
    “Powershell has a wicked pissa scripting language”,   
    “Powershell is wicked easy to use”,
    “Powershell is like, ya know, wicked consistent”,
    “puke monkey guts”,
    “exchange uses powershell, so should you”,   
    “fish heads, fish heads, rolly polly fish heads, fish heads, fish heads, eat them up yumm”,”Jeffrey likes questions, ask them”,
    “Powershell has direct support for WMI, ADO, ADSI, XML, COM, and .NET”,
    “Shut up or I’ll replace you with a 2 line Powershell script, , just joking”,
    “PowerShell goes to 11”,
    “triple panic abort”
)

# Leverages the .NET Random number generator
$Random=New-Object Random

# Characters are found in .ACS files
$Path = $(Join-Path $env:windir “msagent\chars\*.acs”)
$Characters=@(dir $Path | foreach {($_.Name.Split(“.”))[0]})
while ($True)
{
    $till = $StartTime – [DateTime]::now
    if ($till.TotalSeconds -le 0)
    {
        # Now it is time to go so let’s get it going
        $Message = “hay $Speaker, Start the session”
        $SleepTime = 10
        $CharacterSize = 600
        while ($true)
        {
            Invoke-Display
        }
    }

    $Message = “$SessionTitle will start in $($Till.Minutes) minutes and $($till.Seconds) seconds”
    Invoke-Display

    $Message = Get-RandomElement $WiseCracks
    Invoke-Display

    $Message = Get-RandomElement $WiseCracks
    Invoke-Display
}

#########  Invoke-MSAgent.ps1 #########################

#PSMDTAG:Author:Jsnover
#PSMDTAG:Title:Invoke-MSAgent
#PSMDTAG:Description:Sample script to invoke the MSAGENT CONTROL
param($Messages=”Hello”, $size=250, $CharacterName=”Merlin”, $MoveToX=500, $MoveToY=500, $StartX=0, $StartY=0, $Async=$false)
$Random = New-Object System.Random
$CharacterFileName = Join-path $env:windir (“msagent\chars\{0}.acs” -f $CharacterName)
$AgentControl = New-Object -COMObject Agent.Control.2
$AgentControl.Connected=$True
[void]$AgentControl.Characters.Load($CharacterName, $CharacterFileName)

$Character = $AgentControl.Characters.Item($CharacterName)
$AnimationNames = @($Character.AnimationNames)
$Character.width = $Character.height=$Size
$action = $Character.MoveTo($StartX,$StartY)
$action = $Character.Show()
$action = $Character.MoveTo($MoveToX,$MoveToY)
#$action = $Character.Play($AnimationNames[$Random.Next($AnimationNames.Count)])
Foreach ($Message in @($Messages))
{
    $action = $Character.Speak($Message)
}
$action = $Character.Hide()
if (!$Async)
{
   while ($Character.Visible)
   {  Start-Sleep -MilliSeconds 250
   }
}
$Character = $Null
$AgentControl.Connected=$False
$AgentControl = $Null

 

start-session.ps1

0 comments

Discussion is closed.

Feedback usabilla icon