【问题标题】:Starting/Stopping AWS EC2 Instances using AWS Powershell Tools使用 AWS Powershell 工具启动/停止 AWS EC2 实例
【发布时间】:2019-04-08 04:26:12
【问题描述】:

我正在尝试编写两个单独的脚本。一个脚本确定哪些 EC2 实例被停止,它启动它们并将启动的内容记录到一个文本文件中。

第二个脚本将读取文本文件并停止这些实例。为了调试/简单起见,我开始将两个脚本组合成一个脚本。

这是我的脚本:

$Instances = (Get-EC2Instance).instances
$start_instances = @()
$Instances | foreach { 
    $state = Get-EC2InstanceStatus -InstanceId $_.InstanceId  -IncludeAllInstance $true 
    $state = $state.InstanceState.Name

    if ($state -eq "stopped"){
        $start_instances += $_.InstanceId
    }
} 
[System.IO.File]::WriteAllText("C:\users\myusername\desktop\so.csv", $($start_instances  -join ','))

$shutdown_instances = [System.IO.File]::ReadAllText("C:\users\myusername\desktop\so.csv")

Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Stop-EC2Instance -Instance $shutdown_instances

启动和记录正在运行的实例可以正常工作。它是失败的停止实例。

我收到以下错误:

Stop-EC2Instance : Invalid id: "i-99edd755,i-8d647f58"
At C:\users\myusername\Desktop\aws-test.ps1:28 char:1
+ Stop-EC2Instance -Instance $shutdown_instances
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Stop-EC2Instance], AmazonEC2Exception
    + FullyQualifiedErrorId : Amazon.EC2.AmazonEC2Exception,Amazon.PowerShell.Cmdlets.EC2.StopEC2InstanceCmdl

那些实例 ID 确实有效,所以我不明白为什么它会抱怨。我正在通过 out-file 将实例 ID 写入文件并通过 get-content 获取它,但这似乎导致了不同的错误。

一旦我从文本文件中提取数据,我假设问题与数据格式有关。

编辑 所以我将脚本更改为:

 $start_instances = $start_instances | Select-Object @{Name='Name';Expression={$_}}
 $start_instances | Export-Csv -Delimiter ',' -NoTypeInformation -path C:\temp\instances.csv

 $stop_instances = @(Import-Csv -path C:\temp\instances.csv)

 $stop_instances | Stop-EC2Instance

但还是报错:

Stop-EC2Instance : No instances specified
At C:\temp\aws-test.ps1:22 char:19
+ $stop_instances | Stop-EC2Instance
+                   ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Stop-EC2Instance], AmazonEC2Exception
    + FullyQualifiedErrorId : Amazon.EC2.AmazonEC2Exception,Amazon.PowerShell.Cmdlets.EC2.StopEC2InstanceCmdlet

我也试过了:

Stop-EC2Instance -InstanceID $stop_instances

但这也会死:

Stop-EC2Instance : No instances specified
At C:\temp\aws-test.ps1:22 char:1
+ Stop-EC2Instance -InstanceId $stop_instances
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Stop-EC2Instance], AmazonEC2Exception
    + FullyQualifiedErrorId : Amazon.EC2.AmazonEC2Exception,Amazon.PowerShell.Cmdlets.EC2.StopEC2InstanceCmdlet

【问题讨论】:

  • 该消息听起来确实像是将以下字符串:i-99edd755,i-8d647f58 视为单个 ID 值,而不是两个单独的值。它似乎不喜欢你的 CSV 格式。
  • 如果我注释掉从 CSV 文件中读取数据的行,而是将我写入文本文件的变量提供给它,它会按预期工作。例如: Stop-EC2Instance -Instance $start_instances 按预期工作。当然,这对我来说不是一个有效的解决方案,因为我需要将它们分成两个单独的脚本。第二个脚本(关闭脚本)将无法访问该变量。这就是为什么我将实例名称写入文件的原因。
  • 不确定这与我的评论有什么关系。你能在一个变量中设置多个 ID 并让它工作吗?
  • 如果我在脚本之外执行 Stop-EC2Instance -Instance i-99edd755,i-8d647f58 它可以工作
  • 马克 B 是对的。 InstanceId 参数采用一个 ID 数组。您正在向它传递一长串用逗号分隔的 ID。这不是它想要的。尝试拆分它们,将它们添加到数组中,然后将其传递给 Stop-EC2Instance。另外,您确实知道有一个 Import-CSV cmdlet,对吧?

标签: powershell amazon-web-services


【解决方案1】:

InstanceId 参数需要数组。

你可以先声明一个空数组

$EC2Instance =@()

然后使用 Import-CSV 并通过管道将空数组附加到 csv 中的每个值

Import-CSV C:\Temp\Instance.csv | Foreach-Object {$EC2Instance += $_.InstanceId}

echo $EC2Instance

$EC2instance 现在将在数组中包含所需的实例 ID

然后就可以在命令中使用了

Stop-EC2instance -InstanceId $EC2instance

这对我有用。

【讨论】:

    最近更新 更多