ThreadJob 模块似乎无缘无故停止工作

问题描述 投票:0回答:1

我使用的是 Powershell 5.1,IIRC 支持 ThreadJob OOB。 我有一个使用该模块的脚本,该模块过去可以正常工作。 现在我收到以下错误:

ForEach-Object : The term 'Start-ThreadJob' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.

诚然,我有一段时间没有运行脚本(或使用 ThreadJob),但即使我只是从终端调用

Start-ThreadJob
,我也会收到“无法识别”错误。 我已重新启动并运行 DISM 和 sfc,但没有发现任何问题。

这是在相当新的 Windows 10 安装上进行的。

我是不是忘记了什么? 我依稀记得很久以前经历过类似的事情,但不记得我是如何解决的。

multithreading powershell
1个回答
2
投票

Windows PowerShell - 仅适用于 Windows 的旧版 PowerShell 附带 Windows 版本,最新版本为 5.1.x - 附带

ThreadJob
模块,提供
Start-ThreadJob
cmdlet
- 只有现代、跨平台、按需安装 PowerShell(核心) 版本 (v6+) 才可以

但是,您可以在 Windows PowerShell按需安装模块,从 PowerShell Gallery,例如使用 Install-Module -Scope CurrentUser ThreadJob

以下

跨版本习惯用法在 Windows PowerShell 中运行时,在当前用户的范围内按需安装模块:

if ($PSVersionTable.PSVersion.Major -lt 6 -and -not (Get-Command -ErrorAction Ignore -Type Cmdlet Start-ThreadJob)) { Write-Verbose "Installing module 'ThreadJob' on demand..." Install-Module -ErrorAction Stop -Scope CurrentUser ThreadJob } # Getting here means that Start-ThreadJob is now available. Get-Command -Type Cmdlet Start-ThreadJob
    
© www.soinside.com 2019 - 2024. All rights reserved.