如何使用PowerShell获取云形成堆栈的EC2实例IpAddress?

时间:2018-04-21 07:03:59

标签: powershell amazon-ec2 amazon-cloudformation aws-powershell

我想使用PowerShell列出CloudFormation堆栈的EC2实例的IpAddresses。我正在尝试以下命令,但它没有返回IpAddress。

<div class="footerdot">
   <div class="dot">
      <img src="images/bluedot.png" alt="bluedot"/>
   </div>
</div>

1 个答案:

答案 0 :(得分:0)

我建议查看

基本示例:

PS C:\> Get-EC2InstanceStatus -InstanceId i-12345678

AvailabilityZone : us-west-2a
Events           : {}
InstanceId       : i-12345678
InstanceState    : Amazon.EC2.Model.InstanceState
Status           : Amazon.EC2.Model.InstanceStatusSummary
SystemStatus     : Amazon.EC2.Model.InstanceStatusSummary

PS C:\> $status = Get-EC2InstanceStatus -InstanceId i-12345678
PS C:\> $status.InstanceState

Code    Name
----    ----
16      running

然后,收集所有这样的IPv4地址:

$EC2Instances = Get-EC2Instance

foreach($instance in $EC2Instances.Instances){
  $addresses = "";
  foreach($networkInterface in $instance.NetworkInterfaces){
    $addresses = $addresses, $networkInterface.PrivateIpAddresses.PrivateIpAddress -join ","
  }
  "$($instance.InstanceID): $($addresses.Trim(','))"    
}

此外,计算像this这样的实例可能会有所帮助:

$filterRunning = New-Object Amazon.EC2.Model.Filter -Property @{Name = "instance-state-name"; Value = "running"}
$runningInstances = @(Get-EC2Instance -Filter $filterRunning)
# Count the running instances
$runningInstances.Count

另请参阅:AWS开发人员博客 - Scripting your EC2 Windows fleet using Windows PowerShell and Windows Remote Management