These two commands are a pretty interesting combination. The first tells AWS to generate a credential report. Request-IAMCredentialReport is the step required to generate the report on AWS’s end. There’s some pretty good documentation on how that section works. The most interesting point to me is that AWS will only generate a report every 4 hours. This is important to note if you’re making changes, and re-running reports to double check they fixed an issue.
$awsProfiles = @("FirstProfileName","SecondProfileName");
set-DefaultAWSRegion us-east-1;
foreach ($awsProfile in $awsProfiles) {
write-host "Running audit on $awsProfile";
Set-AWSCredentials -ProfileName $awsProfile;
# Attempt to get an IAM credential report, if one does not exist, sleep to let one generate
$reportResult = Request-IAMCredentialReport -Force;
# Sleep for 15 seconds to allow the report to generate
start-sleep -s 15
try {
# Get IAM Credential report
$credReports = Get-IAMCredentialReport -AsTextArray;
} catch {
write-host "No credential report exists for this account, please run script again in a few minutes to let one generate";
exit;
}
# Empty list that will contain parsed, formatted credential reports
$credReportList = @();
# Get the headings from the report
$headings = $credReports[0].split(",");
# Start processing the report, starting after the headings
for ($i = 1; $i -lt $credReports.length; $i++) {
# Break up the line of the report by commas
$splitLine = $credReports[$i].split(",");
$lineMap = @{};
# Go through the line of the report and set a map key of the header for that column
for ($j = 0; $j -lt $headings.length; $j++) {
$lineMap[$headings[$j]] = $splitLine[$j];
}
# Add the formatted line to the final list
$credReportList += , $lineMap;
}
# Iterate over the report, using rules to evaluate the contents
foreach($credReport in $credReportList) {
# Check for users that have an active password, but not an active MFA device
if($credReport['password_enabled'] -eq "TRUE" -and $credReport['mfa_active'] -eq "FALSE") {
write-host "ALERT: User: $($credReport['user']) has a password enabled, but no MFA device"
}
}
write-host "";
}
This script assumes you have created AWS Powershell tools profiles that match the array on the first line.
And here is some example output of users I had to go have a chat with today to activate their MFA devices.
NOTE: You may need to run this script a couple times, if you haven’t generated an IAM Credential Report in a while.




