我在单个模块中编写了一些与AWS API交谈的PowerShell。我写了一个函数,Get-CloudFormation
,它返回CloudFormation的状态。我已经编写了另一个函数Delete-CloudFormation
,它在发出删除CF API请求后,尝试开始一个作业,该作业使用我的Get-CloudFormation
来轮询CloudFormation的状态。如何调用Start-Job,它取决于调用Start-Job的函数在相同的PowerShell模块中的函数?
我打电话Export-ModuleMember
Get-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"
}
当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
为什么?我该如何解决它?
我发现7152090我认为是相似的,但拨打Start-Job
与-InitializationScript { Get-CloudFormation }
给出了大致相同的错误。
如果我打电话给-InitializationScript { Import-Module ".\awsutils.psm1" }
开始工作,那么.
是我的个人资料的文档目录。即使我将变量绑定到Start-Job
之外的Get-Location
,并将其称为-InitializationScript { Import-Module "$location\awsutils.psm1" }
。
我想,如果在OP想成为更有活力的主脚本可以在模块复制到C:\ WINDOWS \ PowerShell模块目录,以便它可以只使用导入模块没有一个完整路径。它可以在以后删除...嘿嘿 – 2012-02-22 13:54:52
当然..这是一个想法! :) – 2012-02-22 13:59:09