我正在编写一些 powershell 以在单个模块中与 AWS API 对话。我编写了一个函数,Get-CloudFormation
它返回 CloudFormation 的状态。我编写了另一个函数 ,Delete-CloudFormation
它在触发 delete-CF API 请求后,尝试启动一个作业,使用我的Get-CloudFormation
.
我呼吁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
with-InitializationScript { Get-CloudFormation }
给出了大致相同的错误。
如果我调用 Start-Job ,-InitializationScript { Import-Module ".\awsutils.psm1" }
那么.
就是我的个人资料的文档目录。即使我将变量绑定到Get-Location
外部Start-Job
并像-InitializationScript { Import-Module "$location\awsutils.psm1" }
.