8

I am trying to find a good way to work with PowerShell scripts that we use for SXA such as for scaffolding.

I have tried the PowerShell ISE client that was built into Sitecore but sometimes the UI was buggy and I was unable to run/test the scripts using this IDE.

Currently, I am copying the script over to VS Code, do my edits, copy it back, run it in Sitecore to test, etc. With no way to debug I have to rely purely on logs if errors occur for instance.

This is doable for simple scripts but for complex ones (think 500+ lines of PowerShell) it gets tedious. On top of that PowerShell is meant to be used as a scripting language and not meant to write complex programs like C#.

Isn't there a way to develop/test the scripts outside of Sitecore so I can use the tooling present in VS Code? It would be even better if this was possible using C#.

3
  • Have a look at the remoting feature With this setup you can run your scripts within vscode.
    – Schuer
    Commented May 28, 2019 at 12:00
  • Have you tried the debugging features in the ISE client added to Sitecore? It really is very good and I've never had any issues with it.
    – Richard Seal
    Commented May 29, 2019 at 14:27
  • Iam getting a 'You should only run a nested pipeline from within a running pipeline' when trying to run a script or sometimes it does nothing at all. Also the editor only shows a few lines of powershell code and its not possible to enlarge this area since the dragging functionality is broken for me (using Chrome).
    – Barsonax
    Commented May 31, 2019 at 12:29

2 Answers 2

0

So it seems that you can actually call C# from an SPE PowerShell script like so:

[Sitecore.Foundation.Alerts.Models.InfoMessage]$object = [Sitecore.Foundation.Alerts.Models.InfoMessage]::Warning("testing")

C#

namespace Sitecore.Foundation.Alerts.Models
{
  public class InfoMessage
  {
    public InfoMessage(string message, MessageType messageType)
    {
      this.Message = message;
      this.Type = messageType;
    }

    public string Message { get; set; }

    public MessageType Type { get; set; }

    public static InfoMessage Warning(string message) => new InfoMessage(message, MessageType.Warning);
  }
}

So for complex things that might be better done in C# you could use SPE to call into your C# program which does the actual work. Since it's C#, one could even write a unit test for it using any of the popular frameworks if needed.

0

I prefer doing it the other way around.

The code that must be maintained properly can be written in C# within your projects. Then, we can expose certain services in the C# project that SPE can use. We add these services to the DI, for SPE to use.

These services can then be accessed through the ServiceLocator or directly initialised in your SPE scripts. Add functions to get instances of these services to your module. e.g.

function Get-MyService
{
    $myServiceType= [System.Type]([Foundation.Services.IMyService])
    $myService = [Sitecore.DependencyInjection.ServiceLocator]::get_ServiceProvider().GetService($myServiceType)
    return $myService 
 }

Follow a proper structure to have your SPE functions. e.g.

Core > Functions > {You can have all your common functions here, which can be reused across modules}

{Your Module} > Functions > { Functions specific to your module}

{Your Module} > Scripts > { Scripts you use in your ISE}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.