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

标签 powershell workflow

我正在尝试将 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/

相关文章:

workflow - JIRA - 状态转换的确认消息

database - 如果我们使用 git 编写代码,如何对 CMS 内容(例如 Drupal)进行版本控制

tfs - 子工作项状态是否可以在父状态更改时自动更改

JavaScript流程图/流程图库

php - 需要正则表达式来匹配多行,直到在公共(public)分隔符之间找到匹配

powershell - 为什么 Powershell 中的某些方法在其定义中使用接口(interface)

powershell - 纠缠变量应该放在哪里?

excel - 以管理员身份在宏中运行 Powershell 命令(已提升)

Git 保持独立的分支同步

powershell - PowerShell 的快速 ping 功能