forked from officecigar/windows_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled2.psm1
203 lines (141 loc) · 5.3 KB
/
Untitled2.psm1
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
function Get-FileReport { #Begin function Get-FileReport
[cmdletbinding(
DefaultParameterSetName = 'default'
)]
param(
[Parameter(
Mandatory = $false,
Position = 0,
ParameterSetName = 'default'
)]
[Alias('Path')]
[String[]]
$BasePath = (Get-Location),
[Parameter(
Mandatory = $false,
ParameterSetName = 'default'
)]
[String[]]
$FindExtension = @('.exe','.msi'),
[Parameter(
Mandatory = $false,
ParameterSetName = 'default'
)]
[String[]]
$FolderName = 'all',
[Parameter(
ParameterSetName = 'default'
)]
[String[]]
$OmitFolders,
[Parameter(
ParameterSetName = 'default'
)]
[Switch]
$AddTotal,
[Parameter(
ParameterSetName = 'default'
)]
[Parameter(
ParameterSetName = 'outputWithType'
)]
[ValidateSet('csv','xml','json')]
[String]
$Output,
[Parameter(
ParameterSetName = 'default'
)]
[Parameter(
ParameterSetName = 'outputWithType'
)]
[String]
$OutputPath = (Get-Location),
[Parameter(
ParameterSetName = 'default'
)]
[String]
$OutputFile = [string]::Empty
)
#Get a list of all the directories in the base path we're looking for.
$allFolders = Get-FolderList -FolderName $FolderName -FindExtension $FindExtension -OmitFolders $OmitFolders -BasePath $BasePath
$foundFiles = $null
#Create list to store folder objects found with size info.
[System.Collections.Generic.List[Object]]$fileList = @()
#Go through each folder in the base path.
ForEach ($file in $allFolders) {
#Clear out the variables used in the loop.
$fullPath = $null
$folderObject = $null
$fileSize = $null
$fileSizeInMB = $null
$fileSizeInGB = $null
$fileName = $null
#Store the full path to the folder and its name in separate variables
$fullPath = $file.FullName
$fileName = $file.BaseName
Write-Verbose "Working with [$fullPath]..."
#Get folder info / sizes
$fileSize = $file.Length
#We use the string format operator here to show only 2 decimals, and do some PS Math.
[double]$fileSizeInMB = "{0:N2}" -f ($fileSize / 1MB)
[double]$fileSizeInGB = "{0:N2}" -f ($fileSize / 1GB)
#Here we create a custom object that we'll add to the list
$folderObject = [PSCustomObject]@{
PSTypeName = 'PS.File.List.Result'
FileName = $fileName
SizeBytes = $fileSize
SizeMB = $fileSizeInMB
SizeGB = $fileSizeInGB
FullPath = $fullPath
}
#Add the object to the list
$fileList.Add($folderObject)
}
if ($AddTotal) {
$grandTotal = $null
if ($fileList.Count -gt 1) {
$fileList | ForEach-Object {
$grandTotal += $_.'Size(Bytes)'
}
[double]$totalFolderSizeInMB = "{0:N2}" -f ($grandTotal / 1MB)
[double]$totalFolderSizeInGB = "{0:N2}" -f ($grandTotal / 1GB)
$folderObject = [PSCustomObject]@{
FileName = "GrandTotal for [$fullPath]"
SizeBytes = $grandTotal
SizeMB = $totalFolderSizeInMB
SizeGB = $totalFolderSizeInGB
FullPath = 'N/A'
}
#Add the object to the list
$fileList.Add($folderObject)
}
}
if ($Output -or $OutputFile) {
if (!$OutputFile) {
$fileName = "{2}\{0:MMddyy_HHmm}.{1}" -f (Get-Date), $Output, $OutputPath
} else {
$fileName = $OutputFile
$Output = $fileName.Substring($fileName.LastIndexOf('.') + 1)
}
Write-Verbose "Attempting to export results to -> [$fileName]!"
try {
switch ($Output) {
'csv' {
$fileList | Sort-Object SizeBytes -Descending | Export-Csv -Path $fileName -NoTypeInformation -Force
}
'xml' {
$fileList | Sort-Object SizeBytes -Descending | Export-Clixml -Path $fileName
}
'json' {
$fileList | Sort-Object SizeBytes -Descending | ConvertTo-Json | Out-File -FilePath $fileName -Force
}
}
}
catch {
$errorMessage = $_.Exception.Message
Write-Error "Error exporting file to [$fileName] -> [$errorMessage]!"
}
}
#Return the object list with the objects selected in the order specified.
Return $fileList | Sort-Object SizeBytes -Descending
} #End function Get-FileReport