I’m having issues extracting the correct output from this script for the ‘Name’ field, it is displaying all the server names on one line separated by a comma, how do I get them to display vertically as induvials please?
#Get regions
$regions = 'eu-west-3'
$regions = 'eu-central-1'
$Objects = foreach($region in $regions){
#Get instance ids
$instanceIds = (Get-EC2Instance -Region $regions).Instances | select -ExpandProperty instanceid
#For each instance id
foreach($instanceId in $instanceIds){
#Get instance name
$instname = ((Get-EC2Instance).Instances.Tags | Where-Object -FilterScript {$_.Key -eq 'Name'}).Value
#Get instance i.d.
$instanceid = (Get-EC2Instance -Region $regions).Instances | ?{$_.InstanceId -eq $instanceId} | select -ExpandProperty InstanceId
#Get instance platform
$platform = (Get-EC2Instance -Region $regions).Instances | ?{$_.InstanceId -eq $instanceId} | select -ExpandProperty Platform
#Get private ips
$privateIp = (Get-EC2Instance -Region $regions).Instances | ?{$_.InstanceId -eq $instanceId} | select -ExpandProperty PrivateIpAddress
#Get instance public ip
#$publicIp = (Get-EC2Instance -Region $region).Instances | ?{$_.InstanceId -eq $instanceId} | select -ExpandProperty PublicIpAddress
#Print info
#"$instancename $instanceId $privateIp $platform"
$ObjectProperties = @{
# Assuming you've assigned something to $instancename, $instanceid, $platform & $privateIp above
Name = $instname
InstanceID = $instanceid
Platform = $platform
PrivateIPAddress = $privateIp
}
# Now create an object.
# When we just drop it in the pipeline like this, it gets assigned to $Objects
New-Object psobject -Property $ObjectProperties
}}
Your code is broken because you missed to format it as code. The forum software tried to interpret it and removed some characters.
Please use the preformatted text button ( </>). Place the cursor on an empty line, click the button and paste your code.
Please go back and edit your existing post - do not create a new post.
$.Key : The term ‘$.Key’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:7 char:92
… Instance).Instances.Tags | Where-Object -FilterScript { $.Key -eq 'Na …
Hmmm … I don’t have access to AWS and no experiences with it. So I don’t know how this …
Get-EC2Instance -Region $regions
and this …
(Get-EC2Instance).Instances.Tags
… may differ from each other or how they are related to each other. The idea is to do steps from the most general filter on the outer most loop to the most specific filter in the inner most loop.