How can I access the properties of the IpPermissions Get-EC2SecurityGroup property?

  • I am trying to get a list of security groups. (Successful - Using Get-EC2SecurityGroup)
  • Get a list of specific IP packets associated with each security group. (Success - Using (Get-EC2SecurityGroup) .IpPermissions)
  • Return results only if FromPort = "xxx" (Unsuccessful - Not sure how to access the FromPort property that is returned in the result list)

I end up trying the following:

  1. Get a list of existing security groups and loop through each group.

  2. After going through each group, call IpPermissions and find the specific FromPort "xxx".

  3. If FromPort is a match, write down other properties: (FromPort, IpProtocol, IpRanges, ToPort, UserIdGroupPairs)

Problem I have

  1. I'm not sure how to make a loop using amazon objects

  2. I cannot access the properties even though they seem to be named and have values.

  3. I've tried using -Filter with many different iterations, with no success.

  4. The documentation seems to be self-regulatory and the examples I've followed don't get to this level of detail.

Results returned from (Get-EC2SecurityGroup) .IpPermissions

FromPort         : 123
IpProtocol       : tcp
IpRanges         : {0.0.0.0/0}
ToPort           : 123
UserIdGroupPairs : {}

      

+3


source to share


1 answer


Here's an example that works as you described:

  • Filters security group objects by FromPort
  • IpProtocol, IpRanges, ToPort, and UserIdGroupPairs are derived from the mapped security groups.

Code:



# Example using port 22
PS C:\> $port = 22
PS C:\> Get-EC2SecurityGroup | 
    ? { $_.IpPermissions.FromPort -eq $port } | 
    % { $_.IpPermissions } | 
    Select -property IpProtocol, IpRanges, ToPort, UserIdGroupPairs

      

Output:

IpProtocol    IpRanges        ToPort UserIdGroupPairs
----------    --------        ------ ----------------
tcp           {0.0.0.0/0}     22     {}
...           ...             ...    ...

      

+1


source







All Articles