获取分配给EC2实例的所有IP地址

时间:2017-04-17 18:50:45

标签: powershell amazon-web-services amazon-ec2 aws-powershell

以下功能以及我在脚本中收集的其他功能,用于收集AWS中所有内容的完整清单,运行时没有任何问题。

但是,当实例有多个接口时,我错过了第一个实例后分配给实例的所有IP地址。

在将详细信息写入Excel工作表之前,如何确保获取下面函数中每个实例的所有ip地址?

function Create-EC2InstanceWorksheet {

 #Creating EC2 Instances Worksheet

        # Add Excel worksheet
        $workbook.Worksheets.Add()

        # We need to create a sheet for the Instances
        $InstancesWorksheet = $workbook.Worksheets.Item(1)
        $InstancesWorksheet.Name = 'Instances'

        # Headers for the Instance worksheet
        $InstancesWorksheet.Cells.Item(1,1) = 'Region'
        $InstancesWorksheet.Cells.Item(1,2) = 'Instance Name'
        $InstancesWorksheet.Cells.Item(1,3) = 'Image ID'
        $InstancesWorksheet.Cells.Item(1,4) = 'Instance ID'
        $InstancesWorksheet.Cells.Item(1,5) = 'PEM File'
        $InstancesWorksheet.Cells.Item(1,6) = 'Instance Type'
        $InstancesWorksheet.Cells.Item(1,7) = 'Private IP'
        $InstancesWorksheet.Cells.Item(1,8) = 'Public IP'
        $InstancesWorksheet.Cells.Item(1,9) = 'VPC ID'
        $InstancesWorksheet.Cells.Item(1,10) = 'Subnet ID'
        $InstancesWorksheet.Cells.Item(1,11) = 'State'
        $InstancesWorksheet.Cells.Item(1,12) = 'Security Group Id'
        $InstancesWorksheet.Cells.Item(1,13) = 'Source/Dest Check'

        # Excel Cell Counter
        $row_counter = 3
        $column_counter = 1



    # Get the Ec2 instances for each region
    foreach($AWS_Locations_Iterator in $AWS_Locations){
        $EC2Instances = Get-EC2Instance -Region $AWS_Locations_Iterator

        # Iterating over each instance

foreach($ EC2Instances中的$ EC2Instances_Iterator.Instances.NetworkInterfaces.PrivateIpAddresses.PrivateIpAddress){

foreach($ EC2Instances_Iterator.Instances.NetworkInterfaces.Pr ... +〜 在foreach循环中的变量之后缺少'in'。

从foreach开始删除上面的代码并使用@AnthonyNeace提供的建议。替换为下面提供额外IP地址的foreach。

        foreach($instance in $EC2Instances.Instances){
            foreach($networkInterface in $instance.NetworkInterfaces){
            "$($instance.InstanceID): $($networkInterface.PrivateIpAddresses.PrivateIpAddress)";



            # Ignore if a region does not have any instances
            if($EC2Instances_Iterator.count -eq $null) {
            continue
            }
            # Populating the cells
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $AWS_Locations_Iterator
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.Tags.value
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.imageid
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.Instanceid
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.keyname.tostring()
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.Instancetype.Value
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.PrivateIpAddress
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.PublicIpAddress
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.VpcId
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.SubnetId
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.state.name.value
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.securitygroups.GroupId
            $InstancesWorksheet.Cells.Item($row_counter,$column_counter++) = $EC2Instances_Iterator.Instances.SourceDestCheck


            # Seting the row and column counter for next EC2 instance entry
            $row_counter = $row_counter + 1
            $column_counter = 1
        }

        # Iterating to the next region
        $row_counter = $row_counter + 1
    }

}

1 个答案:

答案 0 :(得分:0)

网络接口包含在Get-EC2Instance响应中的每个EC2实例中,因此以私有IP地址为例...您可以通过简单地遍历每个网络上公开的每个私有IP地址来访问私有IP地址接口。对于IPv6地址也是如此。

对象模型

IPv4示例

$EC2Instances_Iterator.Instances.NetworkInterfaces.PrivateIpAddresses.PrivateIpAddress

IPv6示例

$EC2Instances_Iterator.Instances.NetworkInterfaces.Ipv6Addresses.Ipv6Address

示例:使用foreach循环写入地址字符串

此示例将每个网络接口的地址构建为实例循环范围内的逗号分隔字符串。因此,为每个实例创建一个字符串。

powershell subexpression operator $()用于解析字符串中的复杂属性。

$EC2Instances = Get-EC2Instance

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

进一步阅读