gpt4 book ai didi

powershell - 如何让 PowerShell WorkFlow 访问以前导入的模块

转载 作者:行者123 更新时间:2023-12-04 19:05:37 24 4
gpt4 key购买 nike

我正在尝试将 PowerShell 工作流引入到一些现有脚本中,以利用并行运行功能。

目前在我不得不使用的工作流程中:

Inline
{
Import-Module My.Modules
Execute-MyModulesCustomFunctionFromImportedModules -SomeVariable $Using:SomeVariableValue
}

否则我会收到错误消息,指出它找不到自定义函数。必须有更好的方法来做到这一点?

最佳答案

文章在 http://www.powershellmagazine.com/2012/11/14/powershell-workflows/确认必须导入模块然后使用它们就是它的工作原理 - MS 通过为其所有常见的 PowerShell 命令创建 WF 事件来解决这个问题:

General workflow design strategy

It’s important to understand that the entire contents of the workflow get translated into WF’s own language, which only understands activities. With the exception of a few commands, Microsoft has provided WF activities that correspond to most of the core PowerShell cmdlets. That means most of PowerShell’s built-in commands—the ones available before any modules have been imported—work fine.

That isn’t the case with add-in modules, though. Further, because each workflow activity executes in a self-contained space, you can’t even use Import-Module by itself in a workflow. You’d basically import a module, but it would then go away by the time you tried to run any of the module’s commands.

The solution is to think of a workflow as a high-level task coordination mechanism. You’re likely to have a number of InlineScript{} blocks within a workflow because the contents of those blocks execute as a single unit, in a single PowerShell session. Within an InlineScript{}, you can import a module and then run its commands. Each InlineScript{} block that you include runs independently, so think of each one as a standalone script file of sorts: Each should perform whatever setup tasks are necessary for it to run successfully.

关于powershell - 如何让 PowerShell WorkFlow 访问以前导入的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25663034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com