1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
Function Get-MonthlyReport {
Param(
[string][parameter(mandatory=$false)]$AccountId,
[string][parameter(mandatory=$false)]$BucketName,
[string][parameter(mandatory=$false)]$Month,
[string][parameter(mandatory=$false)]$Year
)
If($BucketName -eq $Null){
#If no BucketName was specified, assume it is the same as the account alias
$BucketName = Get-IAMAccountAlias
}
If($AccountID -eq $Null){
#If no AccountId was specified, use the account of the current user
$AccountID = (Get-IAMUser).ARN.Replace('arn:aws:iam::','').Substring(0,12)
}
#If no month and year were specified, use last month
If([System.String]::IsNullOrEmpty($Month)) {$Month = If((Get-Date).Month -eq 1){12}Else{(Get-Date).Month}}
If([System.String]::IsNullOrEmpty($Year)) {$Year = If($Month -eq 12){(Get-Date).Year - 1}Else{(Get-Date).Year}}
$Month = "{0:D2}" -f [int]$Month #Pad single digit with 0
#Download the report from S3 and save to the temp directory
$Key = "$AccountId-aws-billing-csv-$Year-$Month.csv"
$FileName = "$env:TEMP\$AccountId-aws-billing-csv-$Year-$Month.csv"
If(Test-Path $FileName) {Remove-Item $FileName}
$Null = Read-S3Object -BucketName $BucketName -Key $Key -File $FileName
#Import the file from the temp directory
Import-Csv $FileName
}
|