如何调用Start-Job,它依赖于与调用Start-Job的功能在同一Powershell模块中的功能? [英] How do I call Start-Job which depends on a function in the same powershell module as the function calling Start-Job?

查看:98
本文介绍了如何调用Start-Job,它依赖于与调用Start-Job的功能在同一Powershell模块中的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing some powershell to talk to the AWS API, in a single module. I have written one function, Get-CloudFormation, which returns the status of a CloudFormation. I've written another function, Delete-CloudFormation, which after firing off a delete-CF API request, tries to start a job which polls the status of the CloudFormation using my Get-CloudFormation.

我在Get-CloudFormation上调用Export-ModuleMember(但不是Delete-CloudFormation;这是一个私有函数). Get-CloudFormation在模块文件中的定义早于Delete-CloudFormation.

I call Export-ModuleMember on Get-CloudFormation (but not Delete-CloudFormation; that's a private function). Get-CloudFormation is defined earlier in the module-file than Delete-CloudFormation.

我的Start-Job通话(在Delete-CloudFormation内部)如下所示:

My Start-Job call (inside Delete-CloudFormation) looks like:

$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"
}

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

为什么?以及我该如何解决?

Why? And how do I fix it?

我发现

I found 7152090 which I think is similar, but calling Start-Job with -InitializationScript { Get-CloudFormation } gives roughly the same error.

如果我使用-InitializationScript { Import-Module ".\awsutils.psm1" }调用Start-Job,则.是我的个人资料的文档目录.即使我将变量绑定到Start-Job之外的Get-Location并像-InitializationScript { Import-Module "$location\awsutils.psm1" }一样调用它.

If I call Start-Job with -InitializationScript { Import-Module ".\awsutils.psm1" } then . is my profile's documents directory. Even if I bind a variable to Get-Location outside the Start-Job and call it like -InitializationScript { Import-Module "$location\awsutils.psm1" }.

推荐答案

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

move you module awsutils.psm1 in the canonical path for powershell modules:

$env:userprofile\documents\WindowsPowerShell\Modules\awsutils"

然后像这样初始化开始工作

then initialize start-job like this

-InitializationScript { Import-Module awsutils }

使用我的自定义模块和开始工作进行了测试.

Tested with my custom modules and start-job works.

也尝试一下,如果您不想移动psm1,

try also, if you don't want move your psm1 this:

-InizializationScript { import-module -name c:\yourpath\yourmodulefolder\ }

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

where yourmoduleforder contain only one psm1 file.

这篇关于如何调用Start-Job,它依赖于与调用Start-Job的功能在同一Powershell模块中的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆