forked from jdhitsolutions/PSScriptTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-lastModified.ps1
More file actions
70 lines (60 loc) · 2.5 KB
/
get-lastModified.ps1
File metadata and controls
70 lines (60 loc) · 2.5 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
Function Get-LastModifiedFile {
[cmdletbinding()]
[alias("glm")]
[OutputType("System.IO.FileInfo")]
Param(
[Parameter(Position = 0, HelpMessage = "Specify a file filter like *.ps1.")]
[ValidateNotNullOrEmpty()]
[string]$Filter = "*",
[Parameter(Position = 1, HelpMessage = "Specify the folder to search.")]
[ValidateScript({
#this will write a custom error message if validation fails
If ((Test-Path -path $_ -PathType Container) -AND ((Get-Item -path $_).psprovider.name -eq 'Filesystem')) {
return $True
}
else {
Throw "The specified Path value $_ is not a valid folder or not a file system location."
return $False
}
})]
[string]$Path = ".",
[Parameter(HelpMessage = "Specify the search interval based on the last write time.")]
[ValidateSet("Hours", "Minutes", "Days", "Months", "Years")]
[string]$Interval = "Hours",
[Parameter(HelpMessage = "Specify the number of intervals.")]
[alias("ic")]
[ValidateScript({$_ -ge 1})]
[int32]$IntervalCount = 24,
[Parameter(HelpMessage = "Recurse from the specified path.")]
[switch]$Recurse
)
Write-Verbose "Starting $($myinvocation.mycommand)"
$msg ="Searching {0} for {1} files modified in the last {2} {3}." -f (Convert-Path $Path),$filter,$IntervalCount,$Interval
Write-Verbose $msg
switch ($Interval) {
"minutes" { $last = (Get-Date).AddMinutes(-$IntervalCount) }
"hours" { $last = (Get-Date).AddHours(-$IntervalCount) }
"days" { $last = (Get-Date).AddDays(-$IntervalCount) }
"months" { $last = (Get-Date).AddMonths(-$IntervalCount) }
"years" { $last = (Get-Date).AddYears(-$IntervalCount) }
}
Write-Verbose "Cutoff date is $Last"
#remove bound parameters that don't belong to Get-ChildItem
"IntervalCount", "Interval" | ForEach-Object {
if ($PSBoundParameters.ContainsKey($_)) {
[void]$PSBoundParameters.Remove($_)
}
}
#add -File to PSBoundParameters
$PSBoundParameters.Add("file", $True)
if ($recurse) {
Write-Verbose "Recursing..."
}
else {
Write-Verbose "Searching..."
}
#get the files and filter on the LastWriteTime using the Where() method for
#better performance
(Get-ChildItem @PSBoundParameters).Where({$_.LastWriteTime -ge $last})
Write-Verbose "Ending $($myinvocation.mycommand)"
}