如何计算运行EC2实例?
我正在寻找一个非常基本的脚本来计算使用PowerShell在AWS上运行的EC2实例的数量。我发现了几种方法,但由于某种原因,当我尝试它们时,我没有得到我期望的结果。如何计算运行EC2实例?
我最接近的是这样的:
$instancestate = (get-ec2instance).instances.state.name
$instancestate
返回:
stopped
running
stopped
stopped
running
(这样的例子不胜枚举约80实例)
我希望有一个统计响应那些正在运行。
从这个它看起来像下面将工作(我不知道有关过滤器语法):
$i = Get-EC2Instance -Filter @{Name = "instance-state-name"; Value = "running"}
$i.Count
不幸的是,这不起作用。这是错误“Get-EC2Instance:Can not bind parameter' Filter'。无法创建类型为“Amazon.EC2.Model.Filter”的对象 – 2014-11-04 22:51:23
下面是您使用过滤器的示例:http://docs.aws.amazon.com/powershell/latest/userguide/pstools-ec2-launch。 HTML – Swonkie 2014-11-05 20:23:52
我不知道别人,但我更喜欢明确将我的EC2过滤器分配给变量,然后在调用类似Get-EC2Instance
时列出它们。如果您需要在多个条件下进行筛选,则可以更轻松地使用筛选器。
这里是你以后工作的例子,在那里我有6个正在运行的实例:
# Create the filter
PS C:\> $filterRunning = New-Object Amazon.EC2.Model.Filter -Property @{Name = "instance-state-name"; Value = "running"}
# Force output of Get-EC2Instance into a collection.
PS C:\> $runningInstances = @(Get-EC2Instance -Filter $filterRunning)
# Count the running instances (more literally, count the collection iterates)
PS C:\> $runningInstances.Count
6
您是否尝试过:'(GET-ec2instance).count'? – arco444 2014-11-04 17:01:26
也许像'$ instancestate = get-ec2instance |其中{$ _。instances.state.name -eq“running”}; $ count = $ instancestate | measure-object |选择-expandproperty count'? – Paul 2014-11-04 17:15:46
不幸的是,上述解决方案都没有工作。第一个将计算所有实例(不只是那些正在运行的问题)。第二个不尊重_.instances.state.name - 出于某种原因,它会再次返回所有实例(过滤器不起作用 - 它显示所有正在运行并停止的事件)。我相信这可能是PowerShell CmdLet中的一个错误。 – 2014-11-04 18:37:40