Forum Discussion

Ganesh12's avatar
Ganesh12
Copper Contributor
Oct 03, 2019

"FileSystemWatcher "not able to monitor new files coming in sub directory for network share drive

Hi,
I was testing the monitor files using  New-Object System.IO.FileSystemWatcher
 
Even I enabled the property the below property,its not working
$watcher.IncludeSubdirectories = $true
 
 
I am testing on Windows server 12 R2 on network share drive
 

1 Reply

  • Kevin_Morgan's avatar
    Kevin_Morgan
    Iron Contributor

    Ganesh12 

     

    Can you try the below script ?

    $FileSystemWatcher = New-Object System.IO.FileSystemWatcher
    $FileSystemWatcher.Path = "C:\RootFolder\TestFolder"
    $FileSystemWatcher.IncludeSubdirectories = $true
    $FileSystemWatcher.Filter = "*"
    $FileSystemWatcher.NotifyFilter = 383
    
    #Register watcher to watch created changes alone.
    Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action {
    $Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath,
    $Event.SourceEventArgs.ChangeType,
    $Event.TimeGenerated
    $WriteHostParams = @{
    ForegroundColor = 'Green'
    BackgroundColor = 'Black'
    Object = $Object
    }
    Write-Host @WriteHostParams
    }


    Other supported EventName(s)
    Changed,Renamed,Deleted and Error

     

    Refer this post for more info : https://mcpmag.com/Articles/2015/09/24/Changes-to-a-Folder-Using-PowerShell.aspx?Page=1

Resources