gpt4 book ai didi

PowerShell FileSystemWatcher 状态 NotStarted

转载 作者:行者123 更新时间:2023-12-02 23:54:27 26 4
gpt4 key购买 nike

我遇到了与这个问题完全相同的问题 FileSystemWatcher state Not started不幸的是,似乎没有人知道这个问题的答案,这就是为什么我用基本相同的问题来试试运气 :)。

这是我的代码:

# 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

但事件从未开始 State : 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.exePowerShell_ISE 中尝试过,但我的事件作业从未启动。为什么?我该如何启动它?

编辑: 正如答案所暗示的那样,开始不是问题。我认为这是一个问题,因为我从未收到任何输出。看起来输出被丢弃了,除了它是通过 Write-Host

完成的

最佳答案

看起来您看不到操作的结果,因为触发事件操作的输出被简单地丢弃。我只是改变了 Action ,它对我有用。不要忘记在目录中创建一个文件以触发事件。

# 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

和:

$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 : {}

关于PowerShell FileSystemWatcher 状态 NotStarted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48767481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com