SitecorePowerShell / Console

Sitecore PowerShell Extensions
https://doc.sitecorepowershell.com/
Other
114 stars 71 forks source link

There is no Runspace available to run scripts in this thread #1349

Closed alan-null closed 2 months ago

alan-null commented 2 months ago

Expected Behavior

No errors

Actual Behavior

There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: 
             ...               

Of type: System.Management.Automation.CmdletInvocationException
Stack trace:
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at Spe.Core.Host.ScriptSession.ExecuteCommand(Boolean stringOutput, Boolean marshallResults)
   at Spe.Core.Host.ScriptSession.<>c__DisplayClass150_0.b__0()
   at Spe.Core.Utility.SpeTimer.Measure[T](String message, Boolean log, Func`1 action)
   at Spe.Client.Applications.PowerShellIse.ExecuteInternal(ScriptSession scriptSession, String script)
   at Spe.Client.Applications.ScriptRunner.Run()

Inner Exception:There is no Runspace available to run scripts in this thread. You can provide one in the DefaultRunspace property of the System.Management.Automation.Runspaces.Runspace type. The script block you attempted to invoke was: 
             ...               
Of type: System.Management.Automation.PSInvalidOperationException
Stack trace:
   at System.Management.Automation.ScriptBlock.GetContextFromTLS()
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.DoInvokeReturnAsIs(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)
   at Microsoft.PowerShell.Commands.Internal.Format.MshExpression.GetValue(PSObject target, Boolean eatExceptions)
   at Microsoft.PowerShell.Commands.Internal.Format.MshExpression.GetValues(PSObject target, Boolean expand, Boolean eatExceptions)
   at Microsoft.PowerShell.Commands.Internal.Format.ComplexControlGenerator.ExecuteFormatTokenList(TraversalInfo level, PSObject so, List`1 formatTokenList, List`1 formatValueList)
   at Microsoft.PowerShell.Commands.Internal.Format.ComplexControlGenerator.ExecuteFormatControl(TraversalInfo level, ControlBase control, PSObject so, List`1 formatValueList)
   at Microsoft.PowerShell.Commands.Internal.Format.ComplexViewGenerator.GenerateComplexViewEntryFromDataBaseInfo(PSObject so, Int32 enumerationLimit)
   at Microsoft.PowerShell.Commands.Internal.Format.ComplexViewGenerator.GeneratePayload(PSObject so, Int32 enumerationLimit)
   at Microsoft.PowerShell.Commands.Internal.Format.OutOfBandFormatViewManager.GenerateOutOfBandData(TerminatingErrorContext errorContext, MshExpressionFactory expressionFactory, TypeInfoDataBase db, PSObject so, Int32 enumerationLimit, Boolean useToStringFallback, List`1& errors)
   at Microsoft.PowerShell.Commands.Internal.Format.InnerFormatShapeCommand.ProcessOutOfBandObjectOutsideDocumentSequence(PSObject so)
   at Microsoft.PowerShell.Commands.Internal.Format.InnerFormatShapeCommand.ProcessObject(PSObject so)
   at System.Management.Automation.CommandProcessor.ProcessRecord()

Steps to Reproduce the Problem

Simplified

For convenience I created a package with everything needed to reproduce the issue:

  1. Install RunspaceException-1.0.zip
  2. Open /sitecore/system/Modules/PowerShell/Script Library/RunspaceException/Functions/Example1 in ISE
  3. Run

Manual

Here's a complete list of steps:

  1. Enable ScriptedItemEventHandler for item:added event

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:security="http://www.sitecore.net/xmlconfig/security/">
    <sitecore role:require="Standalone or ContentManagement or XMCloud" security:require="Sitecore">
    <events>
      <event name="item:added">
        <handler type="Spe.Integrations.Tasks.ScriptedItemEventHandler, Spe" method="OnEvent" />
      </event>
    </events>
    </sitecore>
    </configuration>
  2. Add SPE module with item:added handler integration point image

  3. Open ISE and run this script

    1..2 | % { 
    $folder = [guid]::NewGuid().ToString("N")
    New-Item -Path "/sitecore/templates/User Defined" -Name $folder -ItemType "System/Media/Media folder"
    }
AdamNaj commented 2 months ago

Same problem as #996. Fixed.