Skip to content

Instantly share code, notes, and snippets.

@lantrix
Last active December 5, 2016 02:10
Show Gist options
  • Save lantrix/23a6de9d82fc31a6115b to your computer and use it in GitHub Desktop.
Save lantrix/23a6de9d82fc31a6115b to your computer and use it in GitHub Desktop.
A quick way to dump out all your AWS IAM Group Policies to the local file system
#!/usr/bin/env powershell
#requires -Modules AWSPowerShell
#requires -Version 2.0
<#
.SYNOPSIS
Script to output all the AWS IAM polcies
#>
Import-Module -Name AWSPowerShell
# For URL Decode of Policy document
$null = Add-Type -AssemblyName System.web
#Form Output for script
$null = Add-Type -AssemblyName System.Windows.Forms
#Current Path
$path = (Get-Item -Path '.\' -Verbose).FullName
#Notify User
$caption = 'Warning!'
$message = "This Script will override all current policies in:`n$path\Groups`nwith current AWS Policies! Do you want to proceed"
$yesNoButtons = 4
if ([Windows.Forms.MessageBox]::Show($message, $caption, $yesNoButtons) -eq 'NO')
{
Write-Output -InputObject 'Script Terminated'
Break
}
else
{
#delete existing policies stored locally
if (Test-Path -LiteralPath $path\Groups -PathType Container)
{
Remove-Item -Recurse -Force -Path $path\Groups
}
$search = @()
$groups = Get-IAMGroups
for ($i = 0; $i -lt $groups.Count; $i++)
{
Write-Verbose -Message 'Creating Dir: '
Write-Verbose -Message $groups[$i].GroupName
#create new dir
$null = New-Item -ItemType directory -Path $path\Groups\$($groups[$i].GroupName)
#Add this group to search array
$search += $groups[$i].GroupName
}
#Get policies for each group and write out to directories
foreach ($searchtype in $search)
{
Write-Verbose -Message 'Saving Policies for: '
Write-Verbose -Message $searchtype
$a = Get-IAMGroupPolicies -GroupName $searchtype
foreach ($this in $a)
{
$b = Get-IAMGroupPolicy -GroupName $searchtype -PolicyName $this
$c = $b.PolicyDocument
[web.httputility]::urldecode($c) > "$path\Groups\$searchtype\$($b.PolicyName).json"
}
}
Write-Verbose -Message 'Script Finished'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment