在Powershell 4.0中调用Workflow中的函数

时间:2016-07-11 14:18:02

标签: powershell workflow powershell-v4.0

我有一组函数需要在Powershell脚本中从内部调用而不需要工作流,该脚本使用工作流来管理远程计算机。我已经尝试定义一个模块来传递给InlineScript,但使用下面的方法,内联脚本找不到动态模块“SharedFunctions”

#Script Only Functions
function funct1 {}
function funct2 {}

#Script and Workflow Functions
New-Module SharedFunctions {
    function sharedFunct1 {}
    function sharedFunct2 {}
} -Name SharedFunctions | Import-Module

#Workflow
workflow w1 {
    Param( [string[]] $computerList)

    foreach -parallel( $computer in $computerList ){
        #Shared Function out of Inline Script
        sharedFunct1


        InlineScript{
            #Shared Function inside InlineScript
            sharedFunct2
            #Do stuff
        } -PSRequiredModules "SharedFunctions"
    }
}

<# Local Script #>
funct1    
sharedFunct1
w1

我收到错误消息

The specified module 'SharedFunctions' was not loaded because no valid module file was found in any module directory.
    + CategoryInfo          : ResourceUnavailable: (FunctionModule:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
    + PSComputerName        : [localhost]

我读过可以在工作流程中导入模块。动态模块无法实现这一点吗?如何从工作流程中调用共享功能? sharedFunct1可能是一项任务,例如使用远程计算机上运行的每个线程的更新来更新主机上的日志文件。

1 个答案:

答案 0 :(得分:0)

似乎导入模块只能从文件而不是从内存完成。这似乎已经成功了 - 档案1 -

<# MAIN.PS1 #>
function funct1 {}
function funct2 {}

#Workflow
workflow w1 {

    Param( [string[]] $computerList)

    $modDir = "Some Directory"
    Import-Module $$modDir

    foreach -parallel( $computer in $computerList ){
        #Shared Function out of Inline Script
        sharedFunct1


        InlineScript{
            #Import module in each Inline Script
            Import-Module $using:$modDir

            #Shared Function inside InlineScript
            sharedFunct2
            #Do stuff
        }
    }
}

<# Local Script #>
funct1    
sharedFunct1
w1

档案2 -

<# MODULE.PSM1 #>    
#Script and Workflow Functions        
function sharedFunct1 {}
function sharedFunct2 {}

但是我会优先选择一个文件,所以如果有人知道如何将动态模块导入工作流程,我很乐意看到它。