我正在尝试注册在目录中创建或更改文件时的事件。当我尝试注册事件时,状态保持“未开始”并且事件不起作用。
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
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)