0
votes

This simple Powershell Script (AWS modules) will get output a list of Polices Per Group (1 or multiple policies per Group). As you can see I loop through the Groups and can get the output. Using "Write-output" I can get a text "report" but I can't figure out how to send the output to something, assuming a variable/hashtable so that I can send it to Convertto-HTML or CVS

$Groups=(Get-IAmGroupList).GroupName

ForEach ($item in $Groups) {
 (Get-IAMAttachedGroupPolicies -GroupName $item).PolicyName

 }

Any ideas? I've looked so many places but its all so confusing to me and I'm not a powershell guy (this is an automation thing I'm trying to do for myself.

1
How do you use Write-Output? Use Convertto-Html the same way. - vrdse
I would do something like this after assigning the (Get-IAM..) to the $output variable: Write-Output "Report for $item Group" Write-Output $output Write-output "----" - stewtenn
I can hardly understand what you're struggling with then, but try to assign the output of ForEach to a variable. $PolicyNames = ForEach ($item in $Groups) { .... Then pipe the output to Convertto-HTML or csv repectively - vrdse
The issue is that the PolicyNames variable converted to HTML will only have policy names and not the Group it is tied to. Need to figure out how to put those together. (I'm very much a weekend only powershell person) - stewtenn

1 Answers

0
votes

So, I figured it out (we'll someone else showed me in another form.) You need to create a new object and add elements to it in a hashtable.

Given my example, a new PSobject is created and a hashtable is created with the relevant objects including those from 2 different variables. This does repeat the GroupName for each Policy (if there are more than one) but that's fine and is easy to understand. Its all saved to the $GroupPolicies variable that then can be send to "Convertto-HTML" This code (which I did not write), uses the % alias for the foreach loop

$GroupPolicies=Get-IAmGroupList  | % {
    $Groups = $_.GroupName

(Get-IAMAttachedGroupPolicies -GroupName $Groups).PolicyName | % {
        $PolicyName = $_

        #Add a new row to the output
        New-Object PSObject -Property @{
            Group = $Groups
            Policies = $policyName
 }

 } 
 }| Select-Object -Property Group, Policies