How to Embed PowerShell Within a C# Application
- Posted: Apr 06, 2010 at 8:21 PM
- 12,700 Views
- 3 Comments
Download
How do I download the videos?
- To download, right click the file type you would like and pick “Save target as…” or “Save link as…”
Why should I download videos from Channel9?
- It's an easy way to save the videos you like locally.
- You can save the videos in order to watch them offline.
- If all you want is to hear the audio, you can download the MP3!
Which version should I choose?
- If you want to view the video on your PC, Xbox or Media Center, download the High Quality WMV file (this is the highest quality version we have available).
- If you'd like a lower bitrate version, to reduce the download time or cost, then choose the Medium Quality WMV file.
- If you have a Zune, WP7, iPhone, iPad, or iPod device, choose the low or medium MP4 file.
- If you just want to hear the audio of the video, choose the MP3 file.
Right click “Save as…”
- High Quality WMV (PC, Xbox, MCE)
- MP3 (Audio only)
- MP4 (iPod, Zune HD)
- Mid Quality WMV (Lo-band, Mobile)
Join James Brundage, Tester from the Windows PowerShell team, and Bruce Kyle for a quick introduction to how embed PowerShell within your C# application.
See how you can easily reference the PowerShell assembly and start embedding PowerShell cmdlets inside of a C# application with PowerShell V2.
Sample Code:
using System.Management.Automation;
using System.Management.Automation.Runspaces;
/* Calls the Get-Process PowerShell command and returns the output as a string */
foreach (string str in PowerShell.Create().
AddScript("Get-Process").
AddCommand("Out-String").Invoke<string>())
{
Console.WriteLine(str);
}
NOTE: You'll need to add a reference to the version of PowerShell you have installed in your GAC to the Visual Studio project file. To do so, you'll open the project file as a text file and add the following line into the <ItemGroup> section:
<Reference Include="System.Management.Automation" />
For more news, tips, and links to developer training, see the
US ISV Community blog.
Comments Closed
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
One thing I'd be really intreasted in is any guidance on how to make my apps manageble by powershell, how to handle communication from the cmdlet to the app etc. At the moment I have a WCF service I call from the powershell command but am intreasted on how others do it.
Cheers,
Stephen.
Why didn't you guys use a tripod? The tiny screen in the video is very hard to read and the camera shake makes it even harder. Fortunatelly, the code was posted to the right of the video (and the subtitles helped, too), because it was nearly impossible to make it out on the screen. In the end, I think this entire exercise would have been better as a post to a blog.
How to handle the scenario of calling Exchange Server 2010 Commandlets and powershell commandlets in C# ??
Exchange Commandlets require remote runspace to be created for them to run. By doing so, we will not able to run powershell commandlets like "Where-Object" in the same runspace.
Creating a local runspace would enable us to execute powershell commandlets and not Exchange 2010 commandlets.
We can maintain separate runspaces, one local runspace to run powershell commandlets and one remote runspace to run Exchange 2010 COmmandlets.
But, How to execute a pipeline of Exchange 2010 commandlets and powershell commandlets like this: "Get-ExchangeServer | Select-Object Name" ??
Remove this comment
Remove this thread
close