我正在寻找一个非常基本的脚本来计算在亚马逊网络服务使用PowerShell运行的EC2实例的数量。我已经找到了几种方法,但由于某些原因,当我尝试它们时,我没有得到我期望的结果。
我最接近的是这个:
$instancestate = (get-ec2instance).instances.state.name
$instancestate它返回:
stopped
running
stopped
stopped
running(该列表将列出大约80个实例)
我希望得到一个统计正在运行的数量的响应。
发布于 2014-12-02 03:59:06
其他的我不太确定,但我更喜欢显式地将我的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发布于 2021-10-28 08:49:39
对所有实例进行计数,分别对总数、运行中和停止的实例进行计数:
(Get-EC2Instance).Instances | group InstanceType | select Name,
@{n='Total';e={$_.Count }}, @{n='Running';e={($_.Group | ? { $_.state.Name -
eq "running" }).Count }}, @{n='Stopped';e={($_.Group | ? { $_.state.Name -eq
"stopped" }).Count }}有关更多示例,请参阅我的PowerShell one-liners cheat sheet。
https://stackoverflow.com/questions/26740777
复制相似问题