Created
November 14, 2017 14:05
-
-
Save Sam-Martin/b7917e7b72f4f8ca22dae258f81c08df to your computer and use it in GitHub Desktop.
Get-CPUUtilisationAllInstancesMultipleAccounts.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ProfileNames = @( | |
"Profile1", | |
"Profile2" | |
) | |
foreach($Profile in $ProfileNames){ | |
Set-AWSCredential -ProfileName $Profile | |
$StartTime = (Get-Date).AddDays(-30) | |
$EndTime = (Get-Date) | |
$Period = 60*60 # 1 hr | |
$results = @() | |
$AccountName = Get-IAMAccountAlias | |
foreach($AWSRegion in (Get-AWSRegion).Region){ | |
$Instances = (Get-EC2Instance -Region $AWSRegion).instances | ?{$_.state.Name -eq 'running'} | |
Foreach($Instance in $Instances){ | |
$InstanceName = ($Instance.tags | ?{$_.key -eq 'Name'}).value | |
$Params = @{ | |
Namespace = 'AWS/EC2' | |
MetricName = 'CPUUtilization' | |
Dimension = @( | |
@{"Name"="InstanceId";Value=$Instance.instanceID} | |
) | |
Region = $AWSRegion | |
StartTime = $StartTime | |
EndTime = $EndTime | |
Period = $Period | |
Statistic = 'Maximum' | |
} | |
$stats = Get-CWMetricStatistic @Params | |
foreach($metric in $stats.Datapoints){ | |
$results += $metric | select maximum,timestamp, | |
@{L="InstanceID";E={$Instance.instanceid}}, | |
@{L="InstanceName";E={$InstanceName}}, | |
@{L="AWSAccount";E={$AccountName}} | |
} | |
} | |
} | |
$results | export-csv C:\Users\smartin\Documents\$AccountName.csv | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment