.net 启动/停止AWS中的实例并使用powershell等待

.net 启动/停止AWS中的实例并使用powershell等待,.net,powershell,amazon-web-services,.net,Powershell,Amazon Web Services,我知道通过Powershell启动和停止实例非常容易: $awsCreds = Get-AWSAutomationCreds Set-AWSCredentials -AccessKey $awsCreds.AccessKey -SecretKey $awsCreds.SecretKey Set-DefaultAWSRegion -Region us-east-1 $instances = Get-EC2Instance -Filter @{name="tag:Name"; values="SE

我知道通过Powershell启动和停止实例非常容易:

$awsCreds = Get-AWSAutomationCreds
Set-AWSCredentials -AccessKey $awsCreds.AccessKey -SecretKey $awsCreds.SecretKey
Set-DefaultAWSRegion -Region us-east-1

$instances = Get-EC2Instance -Filter @{name="tag:Name"; values="SERVERNAMES"} | Select -ExpandProperty Instances
$instances.InstanceId | foreach {Stop-EC2Instance $_ -ErrorAction SilentlyContinue}
是否有一种快速而肮脏的方式,我只是没有通过AWS Powershell cmdlet甚至.NET SDK看到,它允许我等待操作完成。和/或更新我收集的实例对象集合

还是我一直在运行:

$instances = Get-EC2Instance -Filter @{name="tag:Name"; values="SERVERNAMES"} | Select -ExpandProperty Instances
命令反复执行,直到状态完全改变?

在Github中有一个PowerShell模块,其中包含一些AWS的PowerShell帮助程序功能,您可以在此处找到:

他解决此问题的方法可以从Wait-awswindowshelperstancetop和Wait-awswindowshelperstancerady cmdlet中看出,正如您所建议的那样,只需运行一个带有启动睡眠的循环,直到实例处于您期望的状态。例如:

While((Get-EC2Instance -InstanceId $InstanceID -Region $Region).Instances[0].State.Name -ne 'stopped'){
    Write-Verbose "Waiting for instance to stop"
    Start-Sleep -s 10
}