/\/\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 
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms"

# 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)
$r.OpenAsync()

# 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})   
$Mi.DropDownItems.Add($msi1)  

$Msi2 = new-object System.Windows.Forms.ToolStripMenuItem("&Quit")  
$msi2.add_Click({$form.close()})   
$Mi.DropDownItems.Add($msi2)  

$ms.Items.Add($mi)  
$form.Controls.Add($ms)  

# 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" 
$form.Controls.Add($rtbInput)

# 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" 
$form.Controls.Add($rtbOutput)

# Import some variables to MSH

$os = get-wmiobject win32_operatingsystem
$r.SessionStateProxy.SetVariable("os",$os)

# show Form 

$Form.Add_Shown({$form.Activate()})
$form.showdialog()  


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 :


Comments:
Blogger applepwc
hello,/\/\o\/\/
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

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?