Last active
February 5, 2019 15:49
-
-
Save royashbrook/bf8419a82dbc6de5c4a7b160256d446b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# references: | |
# https://community.spiceworks.com/topic/380201-using-powershell-to-check-for-new-files-in-a-directory | |
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/register-objectevent | |
# https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher | |
# you can change $p to whatever path you want, you can also add file filters, and add more events | |
# i am monitoring a folder for all create/delete events and color coding output | |
$p = $pwd.path | |
$w = New-Object System.IO.FileSystemWatcher $p | |
$w.IncludeSubdirectories = $true | |
function a($e,$c){($e.TimeGenerated,$e.SourceEventArgs.ChangeType,$e.SourceEventArgs.FullPath) -join "," | Write-Host -f $c} | |
Register-ObjectEvent $w -EventName created -Action { a $event "green" } | |
Register-ObjectEvent $w -EventName deleted -Action { a $event "red" } | |
# if you'd like to simulate create/change/rename/delete, here's a simple loop to do so. | |
# just make sure you are in the same path as $p above. | |
while ($true){ | |
dir *.* > .\a.txt | |
dir *.txt >> .\a.txt | |
ren a.txt b.txt | |
del b.txt | |
sleep 5 | |
} | |
# to remove all of the watchers, run below. This is assuming you don't have any other | |
# event subscriptions you want to keep live. If so, you probably already know what to do. =) | |
Get-EventSubscriber -Force | Unregister-Event -Force |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#watch for a single file creation event | |
function w1($p){ | |
$a = { | |
$msg = ($Event.TimeGenerated,$Event.SourceEventArgs.ChangeType,$Event.SourceEventArgs.FullPath) -join "," | |
$m = @{ | |
To = "email to text for your provider" | |
From = "[email protected]" | |
Smtpserver = "yoursmtpserver" | |
Port = 25 | |
} | |
Send-MailMessage @m -Subject "w1 alert - $(get-date)" -Body $msg | |
Unregister-Event -SubscriptionId $EventSubscriber.SubscriptionId | |
"Email Sent. Halted Monitoring" | write-host -f cyan | |
} | |
$w = New-Object System.IO.FileSystemWatcher $p | |
$w.IncludeSubdirectories = $true | |
Register-ObjectEvent $w -EventName created -Action $a | |
} | |
w1 "\\server\share" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$p = "path to folder" | |
$w = New-Object System.IO.FileSystemWatcher $p | |
$w.IncludeSubdirectories = $true | |
function a($e,$c){ | |
$msg = ($e.TimeGenerated,$e.SourceEventArgs.ChangeType,$e.SourceEventArgs.FullPath) -join "," | |
$msg | Write-Host -f $c | |
} | |
Register-ObjectEvent $w -EventName created -Action { a $event "green" } | |
Register-ObjectEvent $w -EventName deleted -Action { a $event "red" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment