Output Issues - AWS PowerShell

Hi,

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

}}

Thanks
Scott

Scott,
Welcome to the forum. :wave:t4:

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.

Thanks in advance.

Without having any experiences with AWS something like this may work:

$regions = 'eu-west-3' ,'eu-central-1'
$Objects = 
foreach ($region in $regions) {
    $instanceIds = (Get-EC2Instance -Region $regions).Instances | Select-Object -ExpandProperty instanceid
    foreach ($instanceId in $instanceIds) {
        $Instances = (Get-EC2Instance -Region $regions).Instances

        [PSCustomObject]@{
            Name             = ((Get-EC2Instance).Instances.Tags | Where-Object -FilterScript { $.Key -eq 'Name' }).Value
            InstanceID       = $Instances | Where-Object { $.InstanceId -eq $instanceId } | Select-Object -ExpandProperty InstanceId
            Platform         = $Instances | Where-Object { $.InstanceId -eq $instanceId } | Select-Object -ExpandProperty Platform
            PrivateIPAddress = $Instances | Where-Object { $.InstanceId -eq $instanceId } | Select-Object -ExpandProperty PrivateIpAddress
        }
    }
}
$Objects

Thanks Olaf,

I still get the same output for the Name field

It’s populating it with all the server names horizontally

i.e.

Server1,Server2,Server3

Each line has all the servers in at and not just the once server per line, if that makes sense

Displaying like this
Name

{Server1,Server2,Server3..}

I want it like this

Name

Server1
Server2
Server3

Cheers
Scott

… now I got what you mean. You have to add another nested loop for all server names.

Something like:

$ServerNameS = ((Get-EC2Instance).Instances.Tags | Where-Object -FilterScript { $.Key -eq 'Name' }).Value
foreach ($ServerName in $ServerNameS) {
    .....
}

Thanks Olaf, I will give that a whirl and let you know how it goes.

$regions = 'eu-central-1'
$Objects = 
foreach ($region in $regions) {
    $instanceIds = (Get-EC2Instance -Region $regions).Instances | Select-Object -ExpandProperty instanceid
    foreach ($instanceId in $instanceIds) {
        $Instances = (Get-EC2Instance -Region $regions).Instances
    $ServerNameS = ((Get-EC2Instance).Instances.Tags | Where-Object -FilterScript { $_.Key -eq 'Name' }).Value
    foreach ($ServerName in $ServerNameS) {
     $EC2Names = ((Get-EC2Instance).Instances.Tags | Where-Object -FilterScript { $_.Key -eq 'Name' }).Value
}
}
}

        [PSCustomObject]@{
            Name             = $EC2Names  | Where-Object -FilterScript { $_.Key -eq 'Name' }.Value | Select-Object -ExpandProperty $_.Key
            InstanceID       = $Instances | Where-Object { $_.InstanceId -eq $instanceId } | Select-Object -ExpandProperty InstanceId
            Platform         = $Instances | Where-Object { $_.InstanceId -eq $instanceId } | Select-Object -ExpandProperty Platform
            PrivateIPAddress = $Instances | Where-Object { $_.InstanceId -eq $instanceId } | Select-Object -ExpandProperty PrivateIpAddress
}
$Objects

Tried this, doesn’t return anything, just continuously runs, I assume it’s just looping round

$regions = 'eu-west-3' , 'eu-central-1'
$Objects = 
foreach ($region in $regions) {
    $instanceIds = (Get-EC2Instance -Region $regions).Instances | Select-Object -ExpandProperty instanceid
    foreach ($instanceId in $instanceIds) {
        $Instances = (Get-EC2Instance -Region $regions).Instances
        $ServerNameList = ((Get-EC2Instance).Instances.Tags | Where-Object -FilterScript { $_.Key -eq 'Name' }).Value
        foreach ($Name in $ServerNameList) {
            [PSCustomObject]@{
                Name             = $Name
                InstanceID       = $Instances | Where-Object { $_.InstanceId -eq $instanceId } | Select-Object -ExpandProperty InstanceId
                Platform         = $Instances | Where-Object { $_.InstanceId -eq $instanceId } | Select-Object -ExpandProperty Platform
                PrivateIPAddress = $Instances | Where-Object { $_.InstanceId -eq $instanceId } | Select-Object -ExpandProperty PrivateIpAddress
            }
        }
    }
}
$Objects

The creation of the new objects have to happen inside the most inner loop.

Thanks for that, I’m getting this error now

$.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 …
  •                                                         ~~~~~
    
    • CategoryInfo : ObjectNotFound: ($.Key:String) , CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException

I tried $.Key and also did this for $.InstanceId as $.InstanceId but this just duplicates IP addresses and Instance ID’s against server names.

I’m a bit lost with this

Maybe a typo … but should it not be ?

$_.Key and $_.InstanceId

@tonyd
Good catch … you’re totally right, thanks. :wink: :+1:t4:

I used the original code what was not formatted as code …

@ScottE
Sorry Scott.

I updated my code suggestion.

Thanks Guys, Yes, I picked up on that and changed it.

What’s happening now is that it’s duplicating the results many times instead of giving 1 result per server.

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.