如何调用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?
问题描述
我在单个模块中编写了一些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屋!