-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-Current-BillingPeriod-StartDate-EndDate-and-CurrentCost-for-all-Subscriptions-in-Tenant.ps1
109 lines (78 loc) · 5.27 KB
/
Get-Current-BillingPeriod-StartDate-EndDate-and-CurrentCost-for-all-Subscriptions-in-Tenant.ps1
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<#
.SYNOPSIS
A script used to get the start date, the end date and het current cost for the current billing period of all subscriptions in a tenant.
.DESCRIPTION
A script used to get the start date, the end date and het current cost for the current billing period of all subscriptions in a tenant.
First of all the script will get the start date for the current billing period.
Then it will get the end date for the current billing period.
And finally it will calculate the current cost for the current billing period.
All of the above will be displayed per subscription in the tenant.
The script will work with all types of Azure Subscriptions (EA, Pay-as-you-Go, Visual Studio Enterprise, MSDN subscriptions, …), except with CSP subscriptions.
.NOTES
Filename: Get-Current-BillingPeriod-StartDate-EndDate-and-CurrentCost-for-all-Subscriptions-in-Tenant.ps1
Created: 26/01/2022
Last modified: 26/01/2022
Author: Wim Matthyssen
PowerShell: Azure Cloud Shell or Azure PowerShell
Version: Install latest Azure PowerShell modules (at least Az version 7.1.0 and Az.Billing version 2.0.0 is required)
Action: Change variables were needed to fit your needs.
Disclaimer: This script is provided "As Is" with no warranties.
.EXAMPLE
Connect-AzAccount
Get-AzTenant (if not using the default tenant)
Set-AzContext -tenantID "xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxx" (if not using the default tenant)
.\Get-Current-BillingPeriod-StartDate-EndDate-and-CurrentCost-for-all-Subscriptions-in-Tenant.ps1
.LINK
https://wmatthyssen.com/2022/01/27/azure-cloud-spend-get-the-start-date-end-date-and-current-cost-for-the-current-billing-period-of-all-subscriptions-in-a-tenant-with-azure-powershell/
#>
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Variables
$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$foregroundColor2 = "Yellow"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Suppress breaking change warning messages
Set-Item Env:\SuppressAzurePowerShellBreakingChangeWarnings "true"
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Start script execution
Write-Host ($writeEmptyLine + "# Script started. Depending on the amount of subscriptions, it will need around 1 - 2 minute(s) to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Get start date, end date and current cost
# Loop trough all subscriptions in tenant and run commands
$subscriptions = Get-AzSubscription
foreach ($sub in $subscriptions)
{
$sub | Select-AzSubscription
$currentBillingPeriod = Get-AzBillingPeriod -MaxCount 1
# Get start date
$startDate = $currentBillingPeriod.BillingPeriodStartDate.ToString("yyyy-MM-dd")
Write-Host ($writeEmptyLine + "# Current billing period start date: " + $startDate)`
-foregroundcolor $foregroundColor2
# Get end date
$endDate = $currentBillingPeriod.BillingPeriodEndDate.ToString("yyyy-MM-dd")
Write-Host ($writeEmptyLine + "# Current billing period end date: " + $endDate)`
-foregroundcolor $foregroundColor2
# Get current cost
try
{
$found = Get-AzConsumptionUsageDetail -StartDate $startDate -EndDate $endDate -ea 0
if ($found.PretaxCost){
$currentCost = $found | Measure-Object -Property PretaxCost -Sum
Write-Host ($writeEmptyLine + "# Current Cost of Subscription: " + $currentCost.Sum)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
} else {
$msg = $writeEmptyLine + "# Current Cost of Subscription: 0 or Cost Management is not supported for the subscription (type) with Subscription ID: {0}" -f $sub.ToString()
Write-Host $msg -foregroundcolor $foregroundColor1 $writeEmptyLine
}
} catch {
$msg="Error: {0}" -f $sub.ToString() ; Write-Host $msg -foregroundcolor $foregroundColor2 $writeEmptyLine
}
}
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script completed
Write-Host ($writeEmptyLine + "# Script completed" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------