简体   繁体   English

如何使用PowerShell获取cloudformation堆栈的EC2实例IpAddress?

[英]How to get EC2 Instances IpAddress of a cloudformation stack using PowerShell?

I want to list down IpAddresses of EC2 Instances of a CloudFormation stack using PowerShell. 我想使用PowerShell列出CloudFormation堆栈的EC2实例的IpAddresses。 I'm trying the below command but it is not returning IpAddress. 我正在尝试以下命令,但未返回IpAddress。

Get-CFNStackResourceList -StackName 'teststack' -LogicalResourceId 'EC2Instance' -region 'eu-west-1'

I suggest to check with 我建议与

Basic example: 基本示例:

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

Then, collect all IPv4 addresses like this: 然后,像这样收集所有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(','))"    
}

Furthermore, it might be helpful to count the instances like 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

See also: AWS Developer Blog - Scripting your EC2 Windows fleet using Windows PowerShell and Windows Remote Management 另请参阅:AWS开发人员博客- 使用Windows PowerShell和Windows远程管理为EC2 Windows脚本编写脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM