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:
-
Get a list of existing security groups and loop through each group.
-
After going through each group, call IpPermissions and find the specific FromPort "xxx".
-
If FromPort is a match, write down other properties: (FromPort, IpProtocol, IpRanges, ToPort, UserIdGroupPairs)
Problem I have
-
I'm not sure how to make a loop using amazon objects
-
I cannot access the properties even though they seem to be named and have values.
-
I've tried using -Filter with many different iterations, with no success.
-
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 : {}
source to share
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 {}
... ... ... ...
source to share