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

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

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

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

我建议检查

基本示例:

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