简体   繁体   English

在 AWS 中启动/停止实例并使用 powershell 等待

[英]Start/Stop Instances in AWS and Wait with powershell

I understand that it is quite easy to start and stop instances through Powershell:我知道通过 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}

Is there a quick and dirty way that I am just not seeing through the AWS Powershell Cmdlets or even the .NET SDK that would allow me to either wait until the action is complete.是否有一种快速而肮脏的方式,我只是没有通过 AWS Powershell Cmdlets 或什至 .NET SDK 看到它,可以让我等待操作完成。 And/Or update the collection of instance objects I gathered?和/或更新我收集的实例对象集合?

Or am I stuck with running the:或者我坚持运行:

$instances = Get-EC2Instance -Filter @{name="tag:Name"; values="SERVERNAMES"} | Select -ExpandProperty Instances

Command over and over until the state completely changes?一遍又一遍地指挥直到状态完全改变?

Sam Martin has a PowerShell module in Github with some PowerShell helper functions for AWS that you can find here: https://github.com/Sam-Martin/AWSWindowsHelpers/ Sam Martin在 Github 中有一个 PowerShell 模块,其中包含一些适用于 AWS 的 PowerShell 辅助函数,您可以在此处找到: https : //github.com/Sam-Martin/AWSWindowsHelpers/

His approach to this problem can be seen in his Wait-AWSWindowsHelperInstanceToStop and Wait-AWSWindowsHelperInstanceReady cmdlets, and is (as you've already suggested) simply to run a loop with a start-sleep until the instance is in the state you expect.他解决这个问题的方法可以在他的Wait-AWSWindowsHelperInstanceToStopWait-AWSWindowsHelperInstanceReady cmdlet 中看到,并且(正如您已经建议的)只是运行一个带有 start-sleep 的循环,直到实例处于您期望的状态。 Eg:例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM