1

PowerShell을 사용하여 AWS에서 실행중인 EC2 인스턴스의 수를 계산하는 아주 기본적인 스크립트를 찾고 있습니다. 몇 가지 방법을 찾았지만 어떤 이유로 시도해 볼 때 기대했던 결과를 얻지 못합니다. 반환실행중인 EC2 인스턴스를 어떻게 계산합니까?

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

:

stopped 
running 
stopped 
stopped 
running 

(목록은 약 80 인스턴스에 대한 계속이) 내가 계산하는 반응을 할

내가 가진 가장 가까운이있다 실행중인 것. 그 모습이에서

+0

당신이 봤어 : 내가 6 개 실행중인 인스턴스를 가질 경우 여기

, 당신이 계신의 작업 예입니다'(GET-ec2instance) .count'? – arco444

+0

어쩌면'$ instancestate = get-ec2instance | where {$ _. instances.state.name -eq "running"}; $ count = $ instancestate | 측정 개체 | -expandproperty count'를 선택 하시겠습니까? – Paul

+0

불행히도 위의 해결 방법 중 어느 것도 작동하지 않습니다. 첫 번째 것은 모든 인스턴스를 계산합니다 (질문 에서처럼 실행중인 인스턴스가 아닌). 두 번째 것은 _instances.state.name을 존중하지 않습니다 - 어떤 이유로 모든 인스턴스를 다시 반환합니다 (필터가 작동하지 않습니다 - 실행중인 모든 인스턴스와 중지 된 인스턴스를 보여줍니다). PowerShell CmdLet의 버그 일 수도 있습니다. –

답변

-1

http://docs.aws.amazon.com/powershell/latest/reference/Index.html?page=Get-EC2Instance.html&tocid=Get-EC2Instance

(I 필터 구문에 대한 확실하지 않다) 일 것이다 다음

$i = Get-EC2Instance -Filter @{Name = "instance-state-name"; Value = "running"} 
$i.Count 
+0

불행히도 이것은 작동하지 않습니다. "Get-EC2Instance : 매개 변수를 바인딩 할 수 없습니다. 필터링 '입니다. 유형이 "Amazon.EC2.Model.Filter"인 객체를 만들 수 없습니다. –

+0

여기에 필터를 사용하는 예제가 있습니다. http://docs.aws.amazon.com/powershell/latest/userguide/pstools-ec2-launch. HTML – Swonkie

1

나는 다른 사람에 대해 확실하지 않다하지만 명시 적으로을에 선호 변수에 내 ec2 필터를 지정하고 Get-EC2Instance과 같은 것을 호출 할 때 나열하십시오. 이렇게하면 여러 조건을 필터링해야 할 때 필터를 사용하는 것이 더 쉬워집니다.

# 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 
관련 문제