5

I want to watch a folder with powershell and I am a PS beginner.

That script works ONE time when I start the script.

But when I have to restart the script again because I changed some script code I get this error message:

Cannot subscribe to the specified event. A subscriber with the source identifier 'FileChanged' already exists.

I tried:

this at the top of the script:

Unregister-Event -SourceIdentifier FileChanged

does not work.

How do I correctly unregister the event so I can run my script as often I want and the previously registered event is disposed?

CODE

$folder = "C:\temp"

$Watcher = New-Object IO.FileSystemWatcher $folder -Property @{ 
    IncludeSubdirectories = $true
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
} 

$onChanged = Register-ObjectEvent $Watcher Changed -SourceIdentifier FileChanged -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
   #Move-Item $path -Destination $destination -Force -Verbose
}
Elisabeth
  • 20,496
  • 52
  • 200
  • 321
  • Can not reproduce this on PowerShell v5 or v2. Could you provide [mcve]? – user4003407 Oct 09 '15 at 09:18
  • That script was run on a server. When I run the script locally it works fine! – Elisabeth Oct 09 '15 at 11:08
  • Why would you unregister if you need to use it again? Once it resides in memory it should continue to work as long as the PowerShell session remains open I think. No need to rerun it. – Matt Oct 09 '15 at 12:10
  • but... I change the source code then I rerun it again change source code and rerun it again. Put that behavior in a loop with end 1000. – Elisabeth Oct 09 '15 at 13:57
  • Does `Get-EventSubscriber` return anything useful? – xXhRQ8sD2L7Z Oct 09 '15 at 19:13
  • yes always the same FireChanged event. As I said locally it works. I will have to reboot the server machine. Then we see... – Elisabeth Oct 09 '15 at 20:34
  • The Register-ObjectEvent $Watcher needs to be unique if you are not terminating the previous event. – Nick Eagle Oct 10 '15 at 15:55

1 Answers1

6

Ok, looking at what your trying to achieve... to answer your original question, you need to do the following to unregistered the event.

Get-EventSubscriber -SourceIdentifier "filechanged" | Unregister-Event

I have to ask why are you having to make 1000 adjustments to the code. If you are trying to register 1000 different events to be monitored it would make more sense to loop and increment a variable using the ++ modifier.

I have achieved this already if this is what your tying to accomplish and can share some code if you need it.

Nick Eagle
  • 1,118
  • 7
  • 7
  • you should NOT take my "1000" comment literally it was an ironic description ;-) – Elisabeth Oct 10 '15 at 17:10
  • 1
    Shouldn't say it then, but I can't see why you'd make amendments to the code constantly? – Nick Eagle Oct 10 '15 at 18:57
  • I'm having the same issue and Elisabeth, in my case the answer to your question is I'm still developing the code that is triggered by the event and I'd like to just run the script again but encounter 'subscriber already exists' and would like it to be smart about when I rerun it. – Kelly S. French Oct 28 '16 at 20:25
  • Thanks, piping the output of `Get-EventSubscriber` was the portion I was missing. – Robert Hartshorn Dec 12 '17 at 14:46