This blog has moved to http://ThePowerShellGuy.com
Greetings /\/\o\/\/
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 : Monad msh