/\/\o\/\/ PowerShelled

This blog has moved to http://ThePowerShellGuy.com Greetings /\/\o\/\/
$AtomFeed = ("Atom.xml")
$PreviousItems = (" MSH get-WmiMethodHelp function Update 3 "," out-dataGrid function for Monad "," Windows "Monad" Shell (MSH) Beta 3.1 "," Windows "Monad" Shell (MSH) Beta 3.1 "," scripting games Last Day MSH answers for event 7 a... "," Scripting Games almost over "," The Scripting Games Event 6 in MSH "," Scripting Games week 2 "," Monad Links "," MSH Concentration (scripting games part 4) "," ")

Wednesday, March 08, 2006


Hosting an MSH runspace from Monad

While I started my Monad hosting project, (redoing a management application I did in VB.NET 2002 to C# using an embedded MSH runspace), I did think - Hey, I think I can also do this from MSH, so I did build this example in MSH.

Tare a lot of MSH hosting examples already (hosting VB.NET / C# / Iron Phyton / Vbscripts) inline in an MSH script , but I think there was any about hosting MSH from MSH Yet ;-)

The script will create an MSH runspace, and creates a form to host it, it is only a basic example that will take the input in one RTBbox (the upper) and outputs the result on the other rtbbox (the lower).
you can use the menu to start the script in the upper inputbox, a button would be more handy but I wanted to keep the example simple (You can do ALT + F-R as a shortcut) .

Note that we are realy creating another monad engine here, not using the original MSH environment.

In the mean time the code in Lee Holms blog, in the last part of the LOGO series solved my problem interacting with object on the form I mentioned in former post, as you can give variables to the sessionstate, you can use on the form .

I implemented this in my C# host (much more usefull there), but it could also be done here I added the $os variable as an example.

the example looks like this :

# HostingDemo.MSH   
# Hosting MSH in a form from MSH 
# /\/\o\/\/ 2006   
# http://mow001.blogspot.com 

# make a runspace for processing commands from hosting form 

$rc = [System.Management.Automation.Runspaces.RunspaceConfiguration]::Create()
$r = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($rc)

$ri = new-object System.Management.Automation.RunspaceInvoke($r)

# Make form 

$form = new-object System.Windows.Forms.form  
$form2 = new-object System.Windows.Forms.form  
$Form.text = "Monad Hosting from a Form in MSH Demo"  
$form.Size =  new-object System.Drawing.Size(810,410

# Build Menu   

$MS = new-object System.Windows.Forms.MenuStrip  
$Mi = new-object System.Windows.Forms.ToolStripMenuItem("&File")  

$Msi1 = new-object System.Windows.Forms.ToolStripMenuItem("&Run")  
$msi1.add_Click({$result = $ri.invoke($rtbInput.text);$rtbOutput.appendtext($($result | out-string))  ; $result | out-host})   

$Msi2 = new-object System.Windows.Forms.ToolStripMenuItem("&Quit")  


# Add Rich Text Box for Input

$rtbInput = new-object System.Windows.Forms.RichTextBox 
$rtbInput.Location = new-object System.Drawing.Size(10,30
$rtbInput.size = new-object System.Drawing.Size(780,170
$rtbInput.Anchor = "top, left, right" 

# Add Rich Text Box for output

$rtbOutput = new-object System.Windows.Forms.RichTextBox 
$rtbOutput.Location = new-object System.Drawing.Size(10,210
$rtbOutput.size = new-object System.Drawing.Size(780,150
$rtbOutput.Anchor = "top, bottom, left, right" 

# Import some variables to MSH

$os = get-wmiobject win32_operatingsystem

# show Form 


as we give the $os variable to the sessionstate you can use the $os variable in your script in the form (not other variables from the host).

it's very basic and ofcourse the use of doing this from MSH is is a bit questionable, but I think it is a nice example to show how easy that it is to host monad in an .NET application.

my C# project is not of any use yet, but for another sample of hosting MSH from a .NET application see Karl Prossers MSH hosting example here MshAnalyzer , you can find a binary (altough not the latest at the moment) there also

enjoy your hosting,

Greetings /\/\o\/\/
Tags :

Blogger applepwc
I like your monad hosting very very much.It's fantastic.

Wish you can consummate it as good as the "MshAnalyzer".

A little bug:clear-host,and set-location can't work in your current version.

May be the $msi1.add_Click have some problems.
Post a Comment

Links to this post:

Create a Link

<< Home


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.
Web mow001.blogspot.com

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