1

I am triggering an SPE script from the code. SPE script contains a few checks but finally has Export-UnicornConfiguration $configurationName.

The script runs without any errors when it is triggered through Execute Script inside CMS. The only issue is when it is triggered from the code. Am I missing anything while calling from the code?

Even from the code, all the other cmdlets like Get-Item and Get-UnicornConfiguration works fine. But below exception occurs with Export-UnicornConfiguration.

using (ScriptSession scriptSession = ScriptSessionManager.NewSession("Default", true))
{
   string script = scriptItem["Script"];
   scriptSession.SetVariable("ConfigurationName", _configurationName);
   if (!string.IsNullOrEmpty(script))
        scriptSession.ExecuteScriptPart(script, true);
}

Unicorn configuration

<configuration name="xxxxxxx_Content">                  
      <dataProviderConfiguration enableTransparentSync="false" />
      <predicate>
        <include name="xxxx" database="master" path="/sitecore/content/xxxxxxx">              
        </include>
      </predicate>          
      <syncConfiguration updateLinkDatabase="false" updateSearchIndex="false" />
 </configuration>

ERROR: Current context is not a job (Sitecore.Jobs.AsyncUI.InvalidContextException)at Sitecore.Jobs.AsyncUI.JobContext.get_Job()
at Sitecore.Jobs.AsyncUI.SendMessageMe ssage..ctor(Message message, Boolean postBack)
at Spe.Core.Host.ScriptingHostUserInterface.WriteProgress(Int64 sourceId, ProgressRecord record)
at System.Management.Automation.Intern al.Host.InternalHostUserInterface.WriteProgress(Int64 sourceId, ProgressRecord record)
at Unicorn.PowerShell.PowershellProgressStatus.Report(Int32 percent)
at Unicorn.SerializationHe lper.ReserializeConfigurations(IConfiguration[] configurations, IProgressStatus progress, ILogger additionalLogger) Export-UnicornConfiguration : Reserialize failed. Review preceding logs for details. At line:1 char:1

  • Export-UnicornConfiguration "xxxxxxx_Content"
    • CategoryInfo : NotSpecified: (:) [Export-UnicornConfiguration], InvalidOperationException
    • FullyQualifiedErrorId : System.InvalidOperationException,Unicorn.PowerShell.ExportUnicornConfigurationCommand

2 Answers 2

2

I was able to reproduce the issue while running the Export-UnicornConfiguration command through SPE Remoting.

It appears to be caused by the fact that Unicorn does not distinguish between Interactive and Non-Interactive sessions. The SPE Console and ISE have specific code implemented to signal when code related to the UI (like Write-Progress) is running in an Interactive mode.

At the moment there does not appear to be a convenient solution. A change would need to be made to the command to address this issue.

1
2

There are 2 ways how I would approach this:

1. Run scripts in "non-interactive" mode

The theory is that because you are running it remotely/via code some of the UI parts of the code might fail.

The trick I suggest you is to replace Write-Progress implementation so it won't do any server callbacks during execution.

Save it as Write-ProgressWebAPI script

function Write-Progress {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false)]
        $Activity,
        [Parameter(Mandatory = $false)]
        $CurrentOperation,
        [Parameter(Mandatory = $false)]
        $Status,
        [Parameter(Mandatory = $false)]
        $PercentComplete,
        [Parameter(Mandatory = $false)]
        [switch]$Completed
    )

    process {
        # do nothing
    }
} 

and import

Import-Function Write-ProgressWebAPI

at the beginning of your script with unicorn cmdlet call.

2. Rewrite ExportUnicornConfigurationCommand

Alternatively if 1 won't help, you can use source code and implement your own version or at least debug it ExportUnicornConfigurationCommand

Notice PowershellProgressStatus usage.

1
  • Thanks, Alan for the inputs, tried the first option but did not work, we are parking this implementation aside for now. Dec 17, 2021 at 2:47

Your Answer

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

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