简体   繁体   中英

PowerShell FileSystemWatcher state NotStarted

I have the exact same problem like this question FileSystemWatcher state Not started unfortunately, no one seemed to know the answer to this, that's why I'm trying my luck with basically the same question :).

This is my code:

# Source Pfad festlegen
$Sourcepath = "C:\install" 

# Watcher objekt erstellen
$watcher = [System.IO.FileSystemWatcher]::new()
$watcher.IncludeSubdirectories = $true
$watcher.Path = $Sourcepath
$watcher.EnableRaisingEvents = $true

# Aktionen festlegen
$action = {
    $event | fl *
    $event.sourceeventargs | fl *
}

$erstellt = Register-ObjectEvent $watcher Created -Action $action -SourceIdentifier File

But the Event is never started State : NotStarted as you can see here:

PS C:\WINDOWS\system32> $erstellt | fl *


State         : NotStarted
Module        : __DynamicModule_dcdf5dd5-0032-42ad-902f-06e15f5cf3fa
StatusMessage :
HasMoreData   : False
Location      :
Command       :
                    $event | fl *
                    $event.sourceeventargs | fl *

JobStateInfo  : NotStarted
Finished      : System.Threading.ManualResetEvent
InstanceId    : 87072caa-32dc-45c9-b10b-7aa4f455a3aa
Id            : 1
Name          : File
ChildJobs     : {}
PSBeginTime   :
PSEndTime     :
PSJobTypeName :
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}
Information   : {}

I tried this in PowerShell.exe , PowerShell.exe as Admin, and PowerShell_ISE and my eventjob never got started. why? How can I start it?

EDIT: As the answer suggests, the starting is not a problem. I thought it was a Problem, because I've never received any output. looks like output is discarded, except it's done via Write-Host

It looks like you can't see the result of your action because the output from a triggered event action is simply discarded . I just change the action and it works for me. Don't forget to create a file the directory to have the event fired.

# Source Pfad festlegen
$Sourcepath = "C:\install" 

# Watcher objekt erstellen
$watcher = [System.IO.FileSystemWatcher]::new()
$watcher.IncludeSubdirectories = $true
$watcher.Path = $Sourcepath
$watcher.EnableRaisingEvents = $true

# Aktionen festlegen
$action = {
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -ForegroundColor red 
}

$erstellt = Register-ObjectEvent $watcher Created -Action $action -SourceIdentifier "File"

I've got :

Get-EventSubscriber 


SubscriptionId   : 4
SourceObject     : System.IO.FileSystemWatcher
EventName        : Created
SourceIdentifier : File
Action           : System.Management.Automation.PSEventJob
HandlerDelegate  : 
SupportEvent     : False
ForwardEvent     : False

and :

$erstellt |fl *


State         : Running
Module        : __DynamicModule_91fdc1aa-45b4-4c37-8c35-167cc41f5fa0
StatusMessage : 
HasMoreData   : True
Location      : 
Command       : 
                $name = $Event.SourceEventArgs.Name 
                $changeType = $Event.SourceEventArgs.ChangeType 
                $timeStamp = $Event.TimeGenerated 
                Write-Host "The file '$name' was $changeType at $timeStamp" -fore red 

JobStateInfo  : Running
Finished      : System.Threading.ManualResetEvent
InstanceId    : 15acf6ef-c89d-4ebb-8be3-2000ea5cfa43
Id            : 3
Name          : File
ChildJobs     : {}
PSBeginTime   : 13/02/2018 15:52:39
PSEndTime     : 
PSJobTypeName : 
Output        : {}
Error         : {}
Progress      : {}
Verbose       : {}
Debug         : {}
Warning       : {}
Information   : {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM