-
Notifications
You must be signed in to change notification settings - Fork 106
/
AzureDisksReport.ps1
136 lines (112 loc) · 4.75 KB
/
AzureDisksReport.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
<#PSScriptInfo
.VERSION 1.0
.GUID 27a1d5f9-bd64-4086-8a49-3c730a9f6b56
.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: 8/24/2023 1:46 PM
Created by: Vikas Sukhija
Organization:
Filename: AzureDisksReport.ps1
===========================================================================
#>
<#
.DESCRIPTION
This will generate Azure Disks report across the organization
#>
param()
#################logs and variables##########################
$log = Write-Log -Name "CloudAzureDisks" -folder "logs" -Ext "log"
$Report = Write-Log -Name "CloudAzureDisks" -folder "Report" -Ext "csv"
$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 - CloudAzureDisks" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - CloudAzureDisks" -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
}
catch
{
$exception = $_.Exception.Message
Write-Log -message "exception $exception has occured loading Subscription - CloudAzureDisks" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "exception $exception has occured loading Subscription - CloudAzureDisks" -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
$disks=$null
$disks = Get-AzDisk
if($disks){
foreach ($disk in $disks) {
$mcoll = "" | Select SubscriptionName,SubscriptionId,DiskName,DiskID,DiskSizeGB,DiskSKUName,DiskLocation,ResourceGroupName
$mcoll.SubscriptionName = $subscriptionName
$mcoll.SubscriptionId = $subscriptionId
$mcoll.DiskName = $disk.Name
$mcoll.DiskID = $disk.Id
$mcoll.DiskSizeGB = $disk.DiskSizeGB
$mcoll.DiskSKUName = $disk.Sku.Name
$mcoll.DiskLocation = $disk.Location
$mcoll.ResourceGroupName = $disk.ResourceGroupName
$collinventory += $mcoll
}
}
}
##########################Error Handling################
if($error){
Write-Log -message "exception $errot has occured loading Disks - CloudAzureDisks" -path $log -Severity Error
Send-MailMessage -SmtpServer $smtpserver -From $from -To $erroremail -Subject "Error - CloudAzureDisks" -Body $Error[0].ToString()
}
##########################Export to CSV################
$collinventory | Export-Csv $report -NoTypeInformation
Send-MailMessage -SmtpServer $smtpserver -From $from -To $email1 -cc $email2 -bcc $erroremail -Subject "Report - CloudAzureDisks" -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 - CloudAzureDisks" -Attachments $log