-
Notifications
You must be signed in to change notification settings - Fork 106
/
AzureStorageAccountsUsage.ps1
147 lines (124 loc) · 6.85 KB
/
AzureStorageAccountsUsage.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<#PSScriptInfo
.VERSION 1.0
.GUID 4b2fa65a-c028-47cc-bf2b-e87ac67c663f
.AUTHOR Vikas Sukhija
.COMPANYNAME TechWizard.cloud
.COPYRIGHT Vikas Sukhija
.TAGS
.LICENSEURI https://techwizard.cloud/
.PROJECTURI https://techwizard.cloud/
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES https://techwizard.cloud/
.PRIVATEDATA
===========================================================================
Created with: ISE
Created on: 10/31/2023 1:46 PM
Created by: Vikas Sukhija
Organization:
Filename: AzureStorageAccountsUsage.ps1
===========================================================================
#>
<#
.DESCRIPTION
This solution will replace cloudhealth inventory report for Azure Disk report
#>
param()
#################logs and variables##########################
$log = Write-Log -Name "AzureStorageAccountsUsage" -folder "logs" -Ext "log"
$Failedlog = Write-Log -Name "Error-AzureStorageAccountsUsage" -folder "logs" -Ext "log"
$Report = Write-Log -Name "AzureStorageAccountsUsage" -folder "Report" -Ext "csv"
$logrecyclelimit = "60"
$smtpserver = "smtpserver"
$from = "[email protected]"
$email1 = "[email protected]"
$erroremail = "[email protected]"
$logrecyclelimit = "60"
#################get-credentials##########################
if(Test-Path -Path ".\Password.xml"){
Write-Log -Message "Password file Exists" -path $log
}else{
Write-Log -Message "Generate password" -path $log
$Credential = Get-Credential
$Credential | Export-Clixml ".\Password.xml"
}
#############################################################
$Credential = $null
$Credential = Import-Clixml ".\Password.xml"
#######################################################################
try {
Write-Log -message "Start ......... Script" -path $log
Connect-AzAccount -Credential $Credential
Write-Log -message "Loaded All Modules" -path $log
}
catch {
$exception = $_.Exception.Message
Write-Log -message "exception $exception has occured loading Modules - AzureStorageAccountsUsage" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - AzureStorageAccountsUsage" -Body $($_.Exception.Message)
break;
}
################################Query all SQL instances########################
try {
Write-Log -message "Query all Subscriptions in Azure" -path $log
# Get all subscriptions
$subIds = Get-AzSubscription | where { $_.State -eq "Enabled" -and $_.Name -ne "Access to Azure Active Directory" } | Select-Object Id, Name
Write-Log -message "Found ........$($subIds.count) Subscriptions" -path $log
}
catch {
$exception = $_.Exception.Message
Write-Log -message "exception $exception has occured loading Subscription - AzureStorageAccountsUsage" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - Write-Log -message "exception $exception has occured loading Subscription - AzureStorageAccountsUsage" -path $log -Severity Error " -Body $($_.Exception.Message)
break;
}
##########################Loop thru all subscripotions and get SQL Instances################
$collinventory = @()
foreach ($subId in $subIds) {
$subscriptionId = $subscriptionName = $null
$subscriptionId = $subid.Id
$subscriptionName = $subid.Name
Set-AzContext -SubscriptionId $subscriptionId
Write-Log -message "Processing ........$subscriptionName" -path $log
$storageAccounts = $null
$storageAccounts = Get-AzStorageAccount
if ($storageAccounts) {
Write-Log -message "Found ........$($storageAccounts.count) Storage Accounts" -path $log
foreach ($storageAccount in $storageAccounts) {
$resourceId = $metric = $null
Write-Log -message "Processing................$($storageAccount.StorageAccountName)" -path $log
$mcoll = "" | Select SubscriptionName, SubscriptionId, StorageAccountName, StorageAccountID, StorageAccountType, StorageAccountLocation, ResourceGroupName, SizeInGB
$mcoll.SubscriptionName = $subscriptionName
$mcoll.SubscriptionId = $subscriptionId
$mcoll.StorageAccountName = $storageAccount.StorageAccountName
$mcoll.StorageAccountID = $storageAccount.Id
$mcoll.StorageAccountType = $storageAccount.Kind
$mcoll.StorageAccountLocation = $storageAccount.Location
$mcoll.ResourceGroupName = $storageAccount.ResourceGroupName
$resourceId = "/subscriptions/$subscriptionId/resourceGroups/$($storageAccount.ResourceGroupName)/providers/Microsoft.Storage/storageAccounts/$($storageAccount.StorageAccountName)"
$metric = (Get-AzMetric -ResourceId $resourceId -MetricName "UsedCapacity" -StartTime "02:00:00" -EndTime "04:00:00")
$mcoll.SizeInGB = [Math]::Round($metric.data[0].Average / (1GB), 2)
$collinventory += $mcoll
if ($error){
Write-Log -message "Error $($error) has occured - $subscriptionName - $($storageAccount.StorageAccountName) - $($container.Name) - resource group $($storageAccount.ResourceGroupName)" -path $log -Severity Error
Write-Log -message "Error has occured - $subscriptionName - $($storageAccount.StorageAccountName) - $($container.Name) - resource group $($storageAccount.ResourceGroupName)" -path $Failedlog -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - $subscriptionName - $($storageAccount.StorageAccountName) - $($container.Name) - AzureStorageAccountsUsage" -Body $Error[0].ToString()
$error.clear()
}
}
}
}
##########################Error Handling################
if ($error) {
Write-Log -message "exception $($error) has occured loading Disks - AzureStorageAccountsUsage" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - AzureStorageAccountsUsage" -Body $Error[0].ToString()
}
##########################Export to CSV################
$collinventory | Export-Csv $report -NoTypeInformation
$sumofstorage = $collinventory | ForEach-Object { $_.SizeInGB } | Measure-Object -Sum | Select-Object -ExpandProperty Sum
Send-MailMessage -SmtpServer $smtpserver -From $from -To $email1 -bcc $erroremail -Subject "Report: $($collinventory.count) Azure Storage Accounts - Total $sumofstorage GB" -Attachments $Report
Disconnect-AzAccount
###############################Recycle logs ###############################################
Set-Recyclelogs -foldername "logs" -limit $logrecyclelimit -Confirm:$false
Write-Log -Message "Script Finished" -path $log
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Log - AzureStorageAccountsUsage" -Attachments $log