The function below as well as others that I have in the script to collect a complete inventory of everything we have in AWS, runs without any problems.
However, I am missing all of the IP addresses that are assigned to the instance after the first one when the instance has more than one interface.
How can I make sure to get all the ip addresses of every instance in the function below before writing the details into the excel worksheet?
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_Iterator.Instances.NetworkInterfaces.PrivateIpAddresses.PrivateIpAddress in $EC2Instances){
foreach($EC2Instances_Iterator.Instances.NetworkInterfaces.Pr ... + ~ Missing 'in' after variable in foreach loop.
Remove the code above starting at foreach and used the suggestion provided by @AnthonyNeace. Replaced with the foreach below which does provide the additional ip addresses.
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
}
}