6

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 build 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 vscode, 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#.

Isnt there a way to develop/test the scripts outside of Sitecore so I can use the tooling present in vscode? Would be even better if this was possible using C#.

  • Have a look at the remoting feature With this setup you can run your scripts within vscode. – Schuer May 28 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 May 29 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 May 31 at 12:29
0

So it seems that you can actually call C# from a 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 its C# one could even write a unit test for it using any of the popular frameworks if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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