如何计算正在运行的EC2实例? [英] How can I count running EC2 Instances?
问题描述
我正在寻找一个非常基本的脚本来计算使用PowerShell在AWS上运行的EC2实例的数量.我找到了几种方法,但是由于某种原因,当我尝试它们时,却没有得到预期的结果.
I'm looking for a very basic script to count the number of running EC2 instances at AWS using PowerShell. I have found several methods but for some reason when I try them, I do not get the results I expect.
我最近的是:
$instancestate = (get-ec2instance).instances.state.name
$instancestate
返回:
stopped
running
stopped
stopped
running
(该列表持续约80个实例)
(the list goes on for about 80 instances)
我希望得到一个统计正在运行的响应的信息.
I wish to have a response that counts those which are running.
推荐答案
我不确定其他对象,但是我更愿意将ec2过滤器明确分配给变量,然后在调用诸如Get-EC2Instance
之类的内容时将其列出.如果您需要在多个条件下进行过滤,这将使使用过滤器更加容易.
I'm not sure about others, but I prefer to explicitly assign my ec2 filters to variables, and then list them when calling something like Get-EC2Instance
. This makes it easier to work with filters if you need to to filter on multiple conditions.
这是您要做什么的一个有效示例,其中有6个正在运行的实例:
Here's a working example of what you're after, where I have 6 running instances:
# 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
这篇关于如何计算正在运行的EC2实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!