-
Notifications
You must be signed in to change notification settings - Fork 4
/
Get-ZeroSize.ps1
101 lines (92 loc) · 3.95 KB
/
Get-ZeroSize.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
#requires -module CIMCmdlets
Function Get-ZeroLengthFiles {
[CmdletBinding()]
[alias('gzf', 'zombie')]
Param(
[Parameter(Position = 0, HelpMessage = 'Specify a path to search.')]
[ValidateScript( { Test-Path -Path $_ })]
[String]$Path = '.',
[Switch]$Recurse
)
Begin {
Write-Verbose "[$((Get-Date).TimeOfDay) BEGIN ] Starting $($MyInvocation.MyCommand)"
#select a subset of properties which speeds things up
$get = 'Name', 'CreationDate', 'LastModified', 'FileSize'
$cimParams = @{
Classname = 'CIM_DATAFILE'
Property = $get
ErrorAction = 'Stop'
Filter = ''
}
} #begin
Process {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using specified path $Path"
#test if folder is using a link or reparse point
if ( (Get-Item -Path $Path).Target) {
$target = (Get-Item -Path $Path).Target
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] A reparse point was detected pointing towards $target"
#re-define $path to use the target
$Path = $Target
}
#convert the path to a file system path
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Converting $Path"
$cPath = Convert-Path $Path
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Converted to $cPath"
#trim off any trailing \ if cPath is other than a drive root like C:\
if ($cpath.Length -gt 3 -AND $cpath -match '\\$') {
$cpath = $cpath -replace '\\$', ''
}
#parse out the drive
$drive = $cpath.Substring(0, 2)
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using Drive $drive"
#get the folder path from the first \
$folder = $cpath.Substring($cpath.IndexOf('\')).replace('\', '\\')
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Using folder $folder (escaped)"
if ($folder -match '\w+' -AND $PSBoundParameters.ContainsKey('Recurse')) {
#create the filter to use the wildcard for recursing
$filter = "Drive='$drive' AND Path LIKE '$folder\\%' AND FileSize=0"
}
elseif ($folder -match '\w+') {
#create an exact path pattern
$filter = "Drive='$drive' AND Path='$folder\\' AND FileSize=0"
}
else {
#create a root drive filter for a path like C:\
$filter = "Drive='$drive' AND Path LIKE '\\%' AND FileSize=0"
}
#add the filter to the parameter hashtable
$cimParams.filter = $filter
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Looking for zero length files with filter $filter"
#initialize a counter to keep track of the number of files found
$i = 0
Try {
Write-Host "Searching for zero length files in $cpath. This might take a few minutes..." -ForegroundColor magenta
#find files matching the query and create a custom object for each
Get-CimInstance @cimParams | ForEach-Object {
#increment the counter
$i++
#create a custom object
[PSCustomObject]@{
PSTypeName = 'cimZeroLengthFile'
Path = $_.Name
Size = $_.FileSize
Created = $_.CreationDate
LastModified = $_.LastModified
}
}
}
Catch {
Write-Warning "Failed to run query. $($_.exception.message)"
}
if ($i -eq 0) {
#display a message if no files were found
Write-Host "No zero length files were found in $cpath." -ForegroundColor yellow
}
else {
Write-Verbose "[$((Get-Date).TimeOfDay) PROCESS] Found $i matching files"
}
} #process
End {
Write-Verbose "[$((Get-Date).TimeOfDay) END ] Ending $($MyInvocation.MyCommand)"
} #end
}