PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.85k stars 375 forks source link

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

Open sheldonhull opened 2 weeks ago

sheldonhull commented 2 weeks ago

Steps to reproduce

$FilePath = ''
$ScriptDefinition = (Get-Content -Raw -Path $FilePath).Trim()
$SettingsFile = "$HOME/.config/powershell/PSScriptAnalyzerSettings.psd1"

if ($SettingsFile) {
    $FormatterSplat = @{
        Settings         = $SettingsFile
        ScriptDefinition = $ScriptDefinition
    }
    Invoke-Formatter @FormatterSplat | Out-File -FilePath $FilePath
}

Expected behavior

Formatting to complete.

Actual behavior

error details ```console $ $error[0] | Select-Object * PSMessageDetails : Exception : System.Management.Automation.RuntimeException: The following exception occurred while constructing the attribute "ColorTransformationAttribute": "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: " ---> System.Management.Automation.PSInvalidOperationException: 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: 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.InvokeAsMemberFunction(Object instance, Object[] args) at System.Management.Automation.Internal.ScriptBlockMemberMethodWrapper.InvokeHelpe r(Object instance, Object sessionStateInternal, Object[] args) at CallSite.Target(Closure, CallSite, Object) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr) --- End of inner exception stack trace --- at System.Management.Automation.Language.Compiler.GetAttribute(AttributeAst attributeAst) at System.Management.Automation.Language.Compiler.GetRuntimeDefinedParameter(Parame terAst parameterAst, Boolean& customParameterSet, Boolean& usesCmdletBinding) at System.Management.Automation.Language.Compiler.GetParameterMetaData(ReadOnlyColl ection`1 parameters, Boolean automaticPositions, Boolean& usesCmdletBinding) at System.Management.Automation.CompiledScriptBlockData.InitializeMetadata() at System.Management.Automation.CompiledScriptBlockData.get_CmdletBindingAttribute() at System.Management.Automation.CommandMetadata.Init(ScriptBlock scriptBlock, String name, Boolean shouldGenerateCommonParameters) at System.Management.Automation.CommandMetadata..ctor(CommandInfo commandInfo, Boolean shouldGenerateCommonParameters) at System.Management.Automation.CommandInfo.get_ExternalCommandMetadata() at System.Management.Automation.CommandInfo.get_Parameters() at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.UseCorrectCasing.Analyz eScript(Ast ast, String fileName)+MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass84_1 .b__2() TargetObject : CategoryInfo : InvalidOperation: (:) [Invoke-Formatter], RuntimeException FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand ErrorDetails : InvocationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at , : line 10 at , : line 1 PipelineIterationInfo : {0, 1, 0} ```

Environment data


$ $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.4.4
PSEdition                      Core
GitCommitId                    7.4.4
OS                             Darwin 23.6.0 Darwin Kernel Version 23.6.0: Mon Jul 29 21:13:04 PDT 2024; root…
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

$ (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.22.0
1.21.0

System Info

CleanShot 2024-10-02 at 12 50 01@2x

SydneyhSmith commented 1 week ago

We are having trouble reproducing this without the file path, file, settings etc. @sheldonhull