-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWatcherScript.ps1
64 lines (49 loc) · 1.37 KB
/
FileWatcherScript.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
$username = [System.Environment]::UserName
$downloads = "C:\Users\$username\Downloads\"
$Path = $downloads
$FileFilter = '*.exe'
$IncludeSubfolders = $true
$AttributeFilter = [IO.NotifyFilters]::FileName
try
{
$watcher = New-Object -TypeName System.IO.FileSystemWatcher -Property @{
Path = $Path
Filter = $FileFilter
IncludeSubdirectories = $IncludeSubfolders
NotifyFilter = $AttributeFilter
}
$action = {
$details = $event.SourceEventArgs
$Name = $details.Name
$FullPath = $details.FullPath
$OldFullPath = $details.OldFullPath
$OldName = $details.OldName
$ChangeType = $details.ChangeType
$text = "{0} was {1}" -f $Name, $ChangeType
Write-Host ""
Write-Host $text -ForegroundColor DarkYellow
switch ($ChangeType)
{
'Created' { "$Name" | Out-File -FilePath .\blacklist.txt -append -Encoding UTF8}
}
}
$handlers = . {
Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action
}
$watcher.EnableRaisingEvents = $true
Write-Host "Watching for changes to $Path"
do
{
Wait-Event -Timeout 1
} while ($true)
}
finally
{
$watcher.EnableRaisingEvents = $false
$handlers | ForEach-Object {
Unregister-Event -SourceIdentifier $_.Name
}
$handlers | Remove-Job
$watcher.Dispose()
Write-Warning "Event Handler disabled, monitoring ends."
}