Repository navigation
Expand file tree
/
Copy pathspbm-report.ps
More file actions
114 lines (81 loc) · 3.34 KB
/
Copy pathspbm-report.ps
File metadata and controls
114 lines (81 loc) · 3.34 KB
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
# Powershell collector script for vSAN storage policy compliance
# v 1.0 vMan.ch, 28.08.2017 - Initial Version
# v 1.5 fr0gger03, 01.12.2021 - updated for capacities
#
# Run the command below to store user and pass in secure credential XML for each environment
#
# $cred = Get-Credential
# $cred | Export-Clixml -Path "./config/HOME-VC.xml"
param
(
[String]$VC,
[String]$creds,
[String]$policyreport
)
#Logging Function
Function Log([String]$message, [String]$LogType, [String]$LogFile){
$date = Get-Date -UFormat '%m-%d-%Y %H:%M:%S'
$message = $date + "`t" + $LogType + "`t" + $message
$message >> $LogFile
}
$ScriptPath = (Get-Item -Path ".\" -Verbose).FullName
$mods = "$ScriptPath\Modules;"
$random = get-random
$RunDateTime = (Get-date)
$RunDateTime = $RunDateTime.tostring("yyyyMMddHHmmss")
$RunDateTime = $RunDateTime + '_' + $random
$LogFileLoc = $ScriptPath + '\Log\Logfile.log'
$ReportFileLoc = $ScriptPath + '\reports'
if($creds -gt ""){
$cred = Import-Clixml -Path "$ScriptPath\config\$creds.xml"
}
else
{
echo "Environment not selected, stop hammer time!"
Exit
}
Log -Message "Starting Script" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc
Log -Message "Connecting to $VC with credentials $creds" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc
Connect-VIServer -server $VC -Credential $cred -Force
$DiskPolicyReport = @()
Log -Message "Getting VM List from vCenter" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc
$vms = Get-VM
Log -Message "Running loop for VM Home Directory and VMDK Policy" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc
foreach ($vm in $vms){
$vmCluster = Get-Cluster -VM $vm
$vmDatastore = Get-Datastore -VM $vm
$VMHome = $vm | get-SpbmEntityConfiguration
Foreach ($VMH in $VMHome){
$DiskPolicyReport += New-Object PSObject -Property @{
VM = $vm
Cluster = $vmCluster
Datastore = $vmDatastore
Entity = 'VMHome'
Policy = $VMH.StoragePolicy
Status = $VMH.ComplianceStatus
DateTime = $VMH.TimeOfCheck
ProvisionedGB = $vm.ProvisionedSpaceGB
UsedGB = $vm.UsedSpaceGB
}
Remove-Variable VMH -Force -ErrorAction SilentlyContinue
}
$VMDisks = $vm | get-harddisk
Foreach ($disk in $VMDisks){
$VMDisk = $disk | get-SpbmEntityConfiguration
$DiskPolicyReport += New-Object PSObject -Property @{
VM = $vm
Cluster = $vmCluster
Datastore = [regex]::match($disk.Filename,'\[(.*?)\]').Groups[1].Value
Entity = $VMDisk.Entity
Policy = $VMDisk.StoragePolicy
Status = $VMDisk.ComplianceStatus
DateTime = $VMDisk.TimeOfCheck
}
Remove-Variable disk -Force -ErrorAction SilentlyContinue
}
Log -Message "Generating report $policyreport" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc
$DiskPolicyReport | select VM,Cluster,Datastore,Entity,Policy,Status,DateTime,ProvisionedGB,UsedGB | export-csv $ReportFileLoc\$policyreport -NoTypeInformation
}
Log -Message "Disconnecting from $VC" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc
Disconnect-VIServer -server $VC -Confirm:$false
Log -Message "Script Finished" -LogType "JOB-$RunDateTime" -LogFile $LogFileLoc