2

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.

The closest I have is this:

$instancestate = (get-ec2instance).instances.state.name
$instancestate

which returns:

stopped
running
stopped
stopped
running

(the list goes on for about 80 instances)

I wish to have a response that counts those which are running.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Mike J
  • 1,200
  • 6
  • 20
  • 31
  • Have you tried: `(get-ec2instance).count`? – arco444 Nov 04 '14 at 17:01
  • maybe something like `$instancestate = get-ec2instance | where {$_.instances.state.name -eq "running"}; $count = $instancestate | measure-object | select -expandproperty count`? – Paul Nov 04 '14 at 17:15
  • Unfortunately neither of the above solutions work. The first one will count all instances (not just those which are running as in the question). The second one does not honor the _.instances.state.name - for some reason it returns all instances again (the filter doesn't work - it shows all that are running and also stopped). I believe it might be a bug in the PowerShell CmdLet. – Mike J Nov 04 '14 at 18:37
  • ok then i guess `(get-ec2instance).instances.state.name` returns an array? if so you could try `(get-ec2instance).instances.state.name | where {$_ -eq "running"}` if not please tell us what type of object it returns – Paul Nov 04 '14 at 18:54
  • I tried $instancestate = (get-ec2instance).instances.state.name | where {$_ -eq "running"}; but the result is the same - it shows all instances whether they are running/stopped/terminated etc. It says "Value" and immediately below it shows the state of all instances (but not only those which are "running"). – Mike J Nov 04 '14 at 19:57
  • 1
    you can get the object type by using `.gettype()` on $instancestate (`$instancestate.gettype()`. without that its just guessing. based on the output you describe (please allways include full output in question) replacing `$_` with `$_.value` might work – Paul Nov 04 '14 at 20:02
  • Apologies for the incorrect formatting. This worked as expected. Thank you Paul. – Mike J Nov 04 '14 at 21:05

2 Answers2

1

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.

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
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
0

Count all instances with separate counters for total, running and stopped ones:

(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 }}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sergey Sypalo
  • 1,223
  • 5
  • 16
  • 35