如何调用Start-Job,它取决于与调用Start-Job的函数相同的PowerShell模块中的函数?

Pet*_*nce 8 powershell jobs powershell-2.0 powershell-jobs

我正在编写一些PowerShell,以便在单个模块中与AWS API进行通信.我编写了一个函数,Get-CloudFormation它返回CloudFormation的状态.我写了另一个函数,Delete-CloudFormation在启动了一个delete-CF API请求后,尝试启动一个使用my轮询CloudFormation状态的作业Get-CloudFormation.

我打电话Export-ModuleMemberGet-CloudFormation(但不是Delete-CloudFormation;这是一个私人功能). Get-CloudFormation先前在模块文件中定义的比Delete-CloudFormation.

我的Start-Job电话(内部Delete-CloudFormation)看起来像:

$job = Start-Job -Name "CloudFormationWaitForDeleteSuccess" -ScriptBlock {
    $status = ""
    $time = 0
    while($status -ne "DELETE_COMPLETE") {
        Write-Verbose ("Checking CloudFormation status")
        $stack = Get-CloudFormation -accessKey $accessKey -secretKey $secretKey -stackName $stackName
        $status = $stack.Status
        Start-Sleep -seconds 10
        $time += 10
    }
    Write-Host "CloudFormation delete-complete after $time seconds $stackName"
}
Run Code Online (Sandbox Code Playgroud)

Delete-CloudFormation运行时,我得到一个异常:

The term 'Get-CloudFormation' 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.
+ CategoryInfo          : ObjectNotFound: (Get-CloudFormation:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Run Code Online (Sandbox Code Playgroud)

为什么?我该如何解决?

我发现7152090我认为这是类似的,但调用Start-Job-InitializationScript { Get-CloudFormation }大致给出了同样的错误.

如果我调用Start-Job,-InitializationScript { Import-Module ".\awsutils.psm1" }那么.就是我的个人资料的文档目录.即使我将变量绑定到Get-Location外部Start-Job并将其称为-InitializationScript { Import-Module "$location\awsutils.psm1" }.

CB.*_*CB. 7

awsutils.psm1在powershell模块的规范路径中移动模块:

$env:userprofile\documents\WindowsPowerShell\Modules\awsutils"
Run Code Online (Sandbox Code Playgroud)

然后像这样初始化start-job

-InitializationScript { Import-Module awsutils }
Run Code Online (Sandbox Code Playgroud)

测试我的自定义模块和初始工作.

另外,如果你不想移动你的psm1:

-InizializationScript { import-module -name c:\yourpath\yourmodulefolder\ }
Run Code Online (Sandbox Code Playgroud)

其中yourmoduleforder只包含一个psm1文件.