Run-ReportAzureMeterRates.ps1
<#PSScriptInfo
.VERSION 0.0.1.2 .GUID 2b32a6b1-3ba3-4b6c-a4dd-2c3f09f2f835 .AUTHOR Matthew Quickenden .COMPANYNAME Avanade / ACE .COPYRIGHT .TAGS Report HTML Azure Meters Rates .LICENSEURI https://github.com/azurefieldnotes/Reports .PROJECTURI https://github.com/azurefieldnotes/Reports .ICONURI .EXTERNALMODULEDEPENDENCIES AzureRM .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION A report to show Meter Rates from Azure. This is a prototype. #> #REQUIRES -Version 5 -Modules @{ModuleName='Avanade.AzureAD';ModuleVersion="1.2.3"},Avanade.AzureReports,ReportHTML,Avanade.ArmTools,Avanade.AzureAD.Graph using module Avanade.AzureReports [CmdletBinding(DefaultParameterSetName='ReportParameters')] param ( [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [Parameter(Mandatory=$false,ParameterSetName='ReportParametersObject')] [string] $TenantDomain="", [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [Parameter(Mandatory=$false,ParameterSetName='ReportParametersObject')] [string] $SubscriptionName="", [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [string] $LeftLogo ='https://azurefieldnotesblog.blob.core.windows.net/wp-content/2017/02/YourLogoHere.png', [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [string] $RightLogo ='https://azurefieldnotesblog.blob.core.windows.net/wp-content/2017/02/ReportHTML.png', [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [string] $reportPath, [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [string] $ReportName='Azure Meter Rates Report', [Parameter(Mandatory=$false,ParameterSetName='ReportParameters')] [Parameter(Mandatory=$false,ParameterSetName='ReportParametersObject')] [switch] $UseExistingData=$false, [Parameter(Mandatory=$false,ParameterSetName='ReportParametersObject')] [PSObject] $ReportParameterObject ) Import-Module -Name Avanade.Azure.Models -MinimumVersion 1.0.1 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { Install-Module -Name Avanade.Azure.Models -MinimumVersion 1.0.1 -Force } Import-Module -Name Avanade.ArmTools -MinimumVersion 1.6 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { Install-Module -Name Avanade.ArmTools -MinimumVersion 1.6 -Force } Import-Module -Name Avanade.AzureAD.Graph -MinimumVersion 1.0.1 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { Install-Module -Name Avanade.AzureAD.Graph -MinimumVersion 1.0.1 -Force } Import-Module -Name Avanade.AzureAD -MinimumVersion 1.2.3 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { Install-Module -Name Avanade.AzureAD -MinimumVersion 1.2.3 -Force } Import-Module -Name Avanade.AzureReports -MinimumVersion 1.0.2 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { Install-Module -Name Avanade.AzureReports - 1.0.2 -Force } Get-InstalledScript -Name export-azurereports -MinimumVersion 1.0 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { install-script Export-AzureReports -MinimumVersion 1.0 } Import-Module -Name ReportHTML -MinimumVersion 1.3.1.1 -ErrorAction SilentlyContinue -ErrorVariable ErrorMsg if ($ErrorMsg) { Install-Module -Name ReportHTML -MinimumVersion 1.3.1.1 -Force } if (!($UseExistingData)) { if ([string]::IsNullOrEmpty($TenantDomain)) { $TenantDomain = read-host -Prompt "enter your tenant domain, eg my.domain.com" } if ([string]::IsNullOrEmpty($SubscriptionName)) { $SubscriptionName = read-host -Prompt "enter your SubscriptionName domain, eg Enterprise" } $SubscriptionFilter={$_.DisplayName -EQ $SubscriptionName} $AzureReports = Export-AzureReports -GraphTenants $tenantDomain -SubscriptionFilter $SubscriptionFilter $SubscriptionData =$AzureReports | ? {$_.SubscriptionDisplayName -eq "$SubscriptionName" } $AzureRates = $SubscriptionData.RateCards $Data = $AzureRates | select MeterCategory,Unit, EffectiveDate ,MeterName,@{n='MeterRates';E={$_.MeterRates.replace('0=','')}} , MeterRegion,MeterSubCategory #| ? {$_.MeterRegion -ne ''} $MeterCategories = ($Data | group MeterCategory).name } else { Write-Warning "Reusing the data, helpful when developing the report" } $rpt = @() $rpt += Get-HTMLOpenPage -LeftLogoString $LeftLogo -RightLogoString $RightLogo -TitleText $ReportName $rpt += Get-HTMLTabheader $MeterCategories $mc = 1 $id = 1 foreach ($MeterCategory in $MeterCategories ) { Write-Progress -Id $id -Activity "Building Azure Meter Rates Report" -Status "Collecting Azure Meter Rates for $MeterCategory" -PercentComplete ($mc/ $MeterCategories.Count*100 ) #$MeterCategory = 'Azure App Service' $CategoryData = $Data | where {$_.MeterCategory -eq $MeterCategory} $Grouped = $CategoryData | group MeterSubCategory, metername | sort name $rpt += Get-HTMLTabContentOpen -TabName $MeterCategory -TabHeading " " $bar = Get-HTMLBarChartObject -ColorScheme Random $bar.Title = "Summary" $bar.DataDefinition.DataNameColumnName = 'MeterRegion' $bar.DataDefinition.DataValueColumnName = 'MeterRates' $bar.DataDefinition.DataSetName = $bar.Size.Height =400 $bar.Size.width =800 $bar.DataDefinition.AxisYTitle = ($CategoryData[0].metername + ' ' + $CategoryData[0].UNIT) $bar.ChartStyle.legendPosition ='none' $rpt += Get-HTMLBarChart -ChartObject $bar -DataSet ($CategoryData| Sort meterrates) $rpt += Get-HTMLContentOpen -HeaderText 'Anchor Links' -IsHidden $rpt += $Grouped | % {(Get-HTMLAnchorLink -AnchorName $_.name -AnchorText $_.name)+ ' | ' } $rpt += Get-HTMLContentClose $G=1 foreach ($Group in $Grouped ) { $Groupname = $Group.Name Write-Progress -Id ($id+1) -Activity "Processing Grouped Data" -Status "working with $Groupname" -PercentComplete ($g/ $Grouped.Count*100 ) -ParentId ($id) $bar = Get-HTMLBarChartObject -ColorScheme Random $bar.Title $bar.DataDefinition.DataNameColumnName = 'MeterRegion' $bar.DataDefinition.DataValueColumnName = 'MeterRates' $bar.DataDefinition.DataSetName = $Group[0].group[0].metername $bar.Size.Height =400 $bar.Size.width =800 $bar.DataDefinition.AxisXTitle = "Meter Region" $bar.DataDefinition.AxisYTitle = $Group[0].group[0].Unit $bar.ChartStyle.legendPosition ='Bottom' $rpt += Get-HTMLContentOpen -IsHidden -HeaderText $Group.Name -Anchor $Group.Name $rpt += Get-HTMLBarChart -ChartObject $bar -DataSet ($Group.Group | Sort meterrates) $rpt += Get-HTMLContentTable ($Group.Group | select EffectiveDate,MeterCategory,MeterSubCategory,MeterName,MeterRegion,Unit, MeterRates | Sort EffectiveDate ) $rpt += Get-HTMLContentclose $G++ } $rpt += Get-HTMLTabContentclose $mc++ } $rpt += Get-HTMLClosePage Save-HTMLReport -ReportContent $rpt -ReportPath $ReportPath -ReportName $ReportName.Replace(' ','') -ShowReport |