2

I've successfully created a FileSystemWatcher (C# object). I run the code below in a powershell session.

# Filters
$filter = "somefile.txt"
$flagfolder = "C:\path\to\some\folder"

# Instantiate Watcher 
$Watcher = New-Object IO.FileSystemWatcher $flagfolder, $filter -Property @{ 
    IncludeSubdirectories = $false
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}

# EVENT: $filter is created
$onCreated = Register-ObjectEvent $Watcher Created -SourceIdentifier FileCreated -Action {
   $path = $Event.SourceEventArgs.FullPath
   $name = $Event.SourceEventArgs.Name
   $changeType = $Event.SourceEventArgs.ChangeType
   $timeStamp = $Event.TimeGenerated
   Write-Host "The file '$name' was $changeType at $timeStamp"
   Write-Host $path
   someglobalfunction $param
}

After running the code above, if I do a Get-Job it reports the FileWatcher:

Id     Name            PSJobTypeName   State         HasMoreData     Location  
--     ----            -------------   -----         -----------     --------  
74     FileCreated                     NotStarted    False  

But, if I open a new powershell session and do a Get-Job it reports nothing....

I need this to fire whenever anybody or anything creates the file $pathfolder\somefile.txt... but currently it only works when the session that defines $watcher creates the file.

Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
  • Jobs don't work like you think they do. Try the code [in this answer](https://stackoverflow.com/a/45313203/562459). – Mike Sherrill 'Cat Recall' Jul 25 '17 at 21:32
  • "The Get-Job cmdlet gets objects that represent the background jobs that were **started in the current session**." Emphasis added. [Source](https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.core/get-job) Also, you assign `$flag` a value, but never use `$flag`. – Mike Sherrill 'Cat Recall' Jul 26 '17 at 15:18
  • @MikeSherrill'CatRecall' sorry, I meant $filter. So I take it this is the wrong way to watch a file? – Kellen Stuart Jul 26 '17 at 16:02

1 Answers1

1

You need two things to make this work.

  1. New-Object System.IO.FileSystemWatcher
  2. Register-ObjectEvent

I think this is the minimum code you need to make FileSystemWatcher work.

Set up the FileSystemWatcher object

$w = New-Object System.IO.FileSystemWatcher
$w.Path = $PWD

$PWD (current working directory) is an automatic variable.

Subscribe to the "Created" event (for simplicity)

Register-ObjectEvent -InputObject $w -EventName Created -SourceIdentifier "File.Created" `
    -Action {Write-Host -Object "A file was created" -ForegroundColor Green -BackgroundColor Black}

Now, if you create a file in the current working directory, PowerShell will write "A file was created" to the screen. It doesn't matter how that file gets created--in the current session, in a different PowerShell session, through File Explorer, using cmd.exe--doesn't matter.

This answer has more elaborate code. But the simplified code here is based directly on it.

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185