All of the PowerShell blog posts so far were about using the console. Being the developer however, you probably think – well that’s good for admins, but what’s in it for me? How can I benefit from it in my code? Well you can. With the latest update you can run the PowerShell environment in your application and take advantage of its scripting power, by getting the results out of it and pulling them into your app.

PowerShellPlug

So How do I tap into the PowerShell goodness?


First of all, you download the PowerShell Console from its Sitecore Shared Source site. Reference the Cognifide.PowerShell.dll from your application and add the following to your class:

using Cognifide.PowerShell.Shell.Host;

Now you can run your PowerShell script by using code that looks like follows:

using (ScriptSession session = new ScriptSession())
{
    // sample script that returns processes
    var scriptResults = session.ExecuteScriptPart(
      "get-process | where-object { $_.ProcessName -eq 'w3wp' }",
      false);

    //iterate through the results to retrieve the
    foreach (Process process in scriptResults)
    {
        WebProcesses = WebProcesses + process.SessionId + ",";
    }
}

In the above sample the environment is used to pull all w3wp processes and create a coma separated list of their ID’s. While you may not find this sample very compelling, just think what you have the whole PowerShell filtering and processing engine at your disposal, that’s including the ability to traverse Sitecore tree and perform actions and creating the script dynamically in your code. You could think of it like a Regexp, but for your environment rather than for Strings.

If you want to get the results PowerShell would print out rather than the objects themselves, you can put “true” in the second parameter of ExecuteScriptPart and then iterate over the Output property of the session to access the results.

using (ScriptSession session = new ScriptSession())
{
    // sample script that lists w3wp processes list
    session.ExecuteScriptPart(
      "get-process | where-object { $_.ProcessName -eq 'w3wp' }", 
      true);

    //iterate through the output lines to display them 
    foreach (var output in session.Output)
    {
        WebProcesses = WebProcesses + output.Text + "</br>";
    }
}

Naturally you can do something at this level without the PowerShell Console for Sitecore (and here’s how). What the console gives you is the access to all the Sitecore specific elements like being able to access items like drives e.g. “cd master:\content\home\” etc as well as all of the custom commandlets.

To get access to the above functionality you need the console in version 2.0 beta5 or later.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...



This entry (Permalink) was posted on Friday, April 6th, 2012 at 2:22 pm and is filed under .Net Framework, C#, Open Source, PowerShell, Sitecore, Software Development, Uncategorized, Visual Studio, Web applications. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response , or trackback from your own site.