Kenny
Kenny

Reputation: 151

New-Object System.IO.FileSystemWatcher multiple servers

I've got a script based on New-Object System.IO.FileSystemWatcher that watches a folder on my home file server for new files, and runs an external app, but now i'm looking to expand on the usage of New-Object System.IO.FileSystemWatcher, to monitor a set of multiple servers (input from a .csv).

I've got the below code working as far as detecting the events, but it generates an alert for a new file multiple times when new files are detected. Any idea how I could get it just generate one alert? I'm thinking it's how my loop is structured?

Any help appreciated!

$Servers = import-csv "C:\Scripts\Servers.csv"

while($true) {

ForEach ($Item in $Servers) { 

# Unregister-Event $changed.Id -EA 0
$Server = $($Item.Server)
write-host "Checking \\$Server\c$\Scripts now"

#$folder = "c\Scripts"
$filter = "*.html"

$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "\\$Server\c$\Scripts\"
$watcher.Filter = "*.html"
$watcher.IncludeSubdirectories = $False
$watcher.EnableRaisingEvents = $true

        $created = Register-ObjectEvent $watcher "Created" -Action {
        write-host "A new file has been created on $Server $($eventArgs.FullPath) -ForegroundColor Green

    }

} #ForEach

write-host "Monitoring for new files. Sleeping for 5 seconds"
Start-Sleep -s 5

} #While

Here is the single server version of my script, basically, i want to do the same thing, except running against a bunch of servers:

$SleepTimer = 15
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "\\FILESERVER\NEWSTUFF\"
$watcher.Filter = "*.html"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$changeType, $path"
write-host "$LogLine created"

**RUN EXTERNAL PROGRAM HERE**

add-content -Value $LogLine -path "\\Fileserver\Log.txt"

}    

### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
$created = Register-ObjectEvent $watcher "Created" -Action $action

while ($true) {
write-warning "no new files detected. Sleeping for $SleepTimer seconds ..."
start-sleep -s $SleepTimer
}

Upvotes: 1

Views: 1102

Answers (1)

ClumsyPuffin
ClumsyPuffin

Reputation: 4069

I think each time the while loop is executing , new file system watcher is created which is generating the multiple alert .which is not happening in your single server version of script

can you check this :

$Servers = import-csv "C:\Scripts\Servers.csv"

ForEach ($Item in $Servers) { 
    # Unregister-Event $changed.Id -EA 0
    $Server = $($Item.Server)
    write-host "Checking \\$Server\c$\Scripts now"

    #$folder = "c\Scripts"
    $filter = "*.html"

    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "\\$Server\c$\Scripts\"
    $watcher.Filter = "*.html"
    $watcher.IncludeSubdirectories = $False
    $watcher.EnableRaisingEvents = $true

    $created = Register-ObjectEvent $watcher "Created" -Action {
        write-host "A new file has been created on $Server $($eventArgs.FullPath)" -ForegroundColor Green
    }
} 


while ($true) {
    write-host "Monitoring for new files. Sleeping for 5 seconds"
    Start-Sleep -s 5
} #While

Upvotes: 1

Related Questions