PowerShell FileSystemWatcher状态未启动
我有一个和这个问题完全相同的问题,不幸的是,似乎没有人知道这个问题的答案,这就是为什么我用基本相同的问题来尝试我的运气:) 这是我的代码:PowerShell FileSystemWatcher状态未启动,powershell,Powershell,我有一个和这个问题完全相同的问题,不幸的是,似乎没有人知道这个问题的答案,这就是为什么我用基本相同的问题来尝试我的运气:) 这是我的代码: # Source Pfad festlegen $Sourcepath = "C:\install" # Watcher objekt erstellen $watcher = [System.IO.FileSystemWatcher]::new() $watcher.IncludeSubdirectories = $true $watcher.Path
# 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
但事件从未启动状态:NotStarted
,如您在此处所见:
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 : {}
我在PowerShell.exe
,PowerShell.exe
作为管理员和PowerShell\u ISE
中尝试了此操作,但我的eventjob从未启动。为什么?我怎么开始呢
编辑:正如答案所示,启动不是问题。我认为这是个问题,因为我从未收到任何输出。看起来输出被丢弃了,除非是通过
写入主机完成的。看起来您看不到操作的结果,因为触发事件操作的输出被简单地丢弃了。我只是改变了动作,它对我有效。别忘了在目录中创建一个文件来触发事件
# 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"
我有:
Get-EventSubscriber
SubscriptionId : 4
SourceObject : System.IO.FileSystemWatcher
EventName : Created
SourceIdentifier : File
Action : System.Management.Automation.PSEventJob
HandlerDelegate :
SupportEvent : False
ForwardEvent : False
以及:
谢谢你的回答。看起来只有在创建/更改/删除第一个文件时,get状态才会被触发为“正在运行”。看起来只有在使用Write Host
时才能看到输出$event | fl*
不会返回,但Write Host($event | fl*)
会返回。奇怪的你知道为什么我不能在没有代码的情况下看到输出吗?写主机< /代码>?我通常认为动作脚本块不是在前台运行的。所以我从不使用写主机。触发的事件操作的输出被简单地丢弃。
$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 : {}