Q: Why 'Shiny?'

A: Firefly. (see http://en.wikipedia.org/wiki/Firefly_%28TV_series%29)

Q: How do you automate powershell from a .Net application?

The first step in automating powershell is to add a reference to System.Management.Automation.dll

To add this reference, edit the .csproj file (in a cs project, for example) in a text editor

<Reference Include="System.Management.Automation" />

to the relevant section.
(thanks to Andrew Webb for that tip http://www.microsoft.com/communities/newsgroups/list/en-us/default.aspx?dg=microsoft.public.windows.powershell&mid=8e8bfac9-ec9b-4c0b-b340-0e50e510a4b2)



After that, you need to create a "Runspace" and invoke a command against a "Pipeline" on that runspace.

Here's a snippet from ShinyPower 0.0.0.0

//bring in the Runspaces namespace
using System.Management.Automation.Runspaces;

...
//in a function, do this...
Runspace myRunSpace;
myRunSpace = RunspaceFactory.CreateRunspace();
myRunSpace.Open();
//This command will retrieve the syntax
// of the 'Out-File' cmdLet
Pipeline pipe = myRunSpace.CreatePipeline("get-Command Out-File | Format-List | out-string");
resultObject = pipe.Invoke();
string clearText = resultObject0.ToString().Trim;
MessageBox.Show(clearText);


Q: What other C# tools/samples are there that automate Powershell?

A: There's a groovy Turtle Logo application (called 'MSH Logo') from Lee Holmes available here:
lurl:http://www.leeholmes.com/blog/MSHLogoAllowingUsersToExtendItsFunctionality.aspx
Also, Scott hanselman has posted up a snippet or two, browse his powershell category here: http://www.hanselman.com/blog/CategoryView.aspx?category=PowerShell

Last edited Oct 3, 2006 at 5:53 AM by secretGeek, version 6

Comments

No comments yet.