我正在尝试在目录中创建或更改文件时注册事件。当我尝试注册事件时,状态仍为“未启动”且事件不起作用。
function checkFilestatus {
Param($k)
try {
[IO.File]::OpenRead($k).Close()
Copy-Item -Path "$k\" -Destination "\\ServerName\D$\Temp\MyFolder" -Recurse -Force
} catch {
Start-Sleep -Seconds 120
Copy-Item -Path "$k\" -Destination "\\ServerName\D$\Temp\MyFolder" -Recurse -Force
}
}
$filewatcher = New-Object System.IO.FileSystemWatcher
$filewatcher.Path = "D:\MyFolder\Folder2"
$filewatcher.IncludeSubdirectories = $true
$filewatcher.EnableRaisingEvents = $true
Register-ObjectEvent $filewatcher "Created" -SourceIdentifier FileCreated -Action { checkFilestatus($($EventArgs.FullPath)) }
Register-ObjectEvent $filewatcher "Changed" -SourceIdentifier FileChanged -Action { checkFilestatus($($EventArgs.FullPath)) }
以下是我运行脚本时的输出:
Id Name PSJobTypeName State HasMoreData -- ---- ------------- ----- ----------- 40 FileCreated NotStarted False 41 FileChanged NotStarted False