运行以下命令将返回错误:
$job=Start-ThreadJob -name maya6 -InitializationScript {. $using:profile} -ScriptBlock {ichild} #this is an alias defined in the profile
错误:
InvalidOperation: A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer.
ichild: The term 'ichild' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
我也尝试过:
$job=start-threadJob {. $args ; ichild} -ArgumentList $profile #ichild is an alias defined in my profile
当我使用receive-job $job
它时,我的提示冻结了,并且我不断收到以下错误:
Oops, something went wrong.
Please report this bug with ALL the details below, including both the 'Environment' and 'Exception' sections.
Please report on GitHub: https://github.com/PowerShell/PSReadLine/issues/new?template=Bug_Report.yaml
Thank you!
### Environment
PSReadLine: 2.3.4
PowerShell: 7.4.6
OS: Microsoft Windows 10.0.26100
BufferWidth: 170
BufferHeight: 21
Last 49 Keys:
我以为using
是专门针对这个命令行程序的……
我在 pwsh7.4
您看到的是一个长期存在的错误- 请参阅GitHub 问题 #4530 - 该错误最初于 2017 年报告,它影响Windows PowerShell(旧版、随 Windows 附带的、仅适用于 Windows 的 PowerShell 版本,其最新版本为 5.1)和PowerShell (Core) 7(自 v7.5.0 起);不幸的是,自那时起它就很少受到关注。
简而言之:
Start-Job
以及以及Start-ThreadJob
意外地不支持传递$using:
给参数的脚本块中的引用,-InitializationScript
与-ScriptBlock
参数不同。解决方法:
或者:将您想要传递的代码添加到传递给参数
-InitializationScript
的主脚本块中-ScriptBlock
,其中支持引用;适用于您的情况:$using:
或者:如果可行的话,使用字符串插值将调用者范围内的值“烘焙”成一个字符串,您可以从中创建一个脚本块;适用于您的情况:
警告:
当文件以点源方式从线程作业(并行运行空间)或后台作业(子进程)获取时,其
$PROFILE
行为可能会有所不同:例如,automatic 的值$PROFILE
未在此处定义。这同样适用于 PowerShell 7 独有的
-Parallel
功能ForEach-Object
,该功能也使用并行运行空间(线程)。Oops, something went wrong.
您看到的错误与模块有关;PSReadLine
将其导入并行运行空间毫无意义,因为它的唯一目的是丰富交互式命令行编辑体验。如果不知道特定配置文件的详细信息,就无法诊断问题,但或许您可以通过将
PSReadLine
配置文件中任何与 相关的代码封装在 中来绕过该问题if (-not $PROFILE) { ... }
,以便在配置文件以点源形式从(线程)作业获取时将其排除。