3

I have installed the popular Powershell Console Sitecore plugin, and so far I'm loving it.

I would like to be able to invoke a specific script programmatically in C# (as in, as a consequence of user input), but I could not find any valid resource pointing me in the right direction.

Is there any way of achieving this? Do the rules for classic Powershell scripts apply to the Sitecore version?

2
  • Be sure to check out sitecore.stackexchange.com Commented Oct 18, 2016 at 4:34
  • I'm also curious what you are building. Perhaps it could be completely built in SPE. There are rules you can apply to scripts and script libraries. Commented Oct 18, 2016 at 4:36

1 Answer 1

8

You can directly call any PowerShell script from your own code using the following

using Cognifide.PowerShell.Core.Host;
using Sitecore.Data.Items;

namespace MyProject
{
    public class TestSPE
    {
        public void Invoke()
        {
            using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true))
            {
                Item speScriptItem = Sitecore.Context.Database.GetItem("/path-or-id/to-spe-item");
                string script = speScriptItem["Script"];
                if (!string.IsNullOrEmpty(script))
                    scriptSession.ExecuteScriptPart(script);
            }
        }
    }
}

Be sure to add a reference to Cognifide.PowerShell.dll in your project.

If your script is not stored on a PowerShell Script item in Sitecore then you pass in a string with the SPE commands and script you wish to run.

You can read more in this blog post about Using PowerShell Console for Sitecore in Your Own Code

Update:

To create a "script item", create an item using Template from:

/sitecore/templates/Modules/PowerShell Console/PowerShell Script

This will provide you Script body where you can put your code. You can find examples of the default SPE scripts in the modules Script Library under /sitecore/system/Modules/PowerShell/Script Library.

Update 2:

To get return values from the script, you call to pass false for the stringOutput parameter on the method:

List<object> results; 
results = scriptSession.ExecuteScriptPart(script, false); 
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you, will try this later!
How do I save a script as a "script item"?
Thanks. I'm off soon and I'll be on holiday for a week, but I will definitely try your answer when I get back!
@EmanueleCiriachi Answer updated with namespaces, it has changed from the original article so it may be why it is complaining (I always use Resharper so don't always look at them!)
Ahh sorry, I misunderstood. SPE 4.1 is built using .Net Framework 4.5.2, so you'll need to change target framework of your project (or possibly older versions of SPE were build with 4.5, not sure)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.