如何解决在 FileSystemWatcher (Powershell) 中调用函数和变量
我在 PS version: 4.0
上运行此脚本,但似乎无法从 -Action 参数外部访问函数或变量。
$action = { .....
access them here <--------
}
function GetData{
Write-host "this should show"
}
$somePath="D:\Temp"
$watcher=New-ObjectSystem.IO.FileSystemWatcher
$watcher.Path="D:\Temp"
$watcher.Filter="*.*"
$watcher.IncludeSubdirectories=$true
$watcher.EnableRaisingEvents=$true
$action={$path=$Event.sourceEventArgs.FullPath
$changeType=$Event.sourceEventArgs.ChangeType
$logline="$(Get-Date),$changeType,$path"
#$somePath returns nothing <-------
#GetData is not called at all <-------
GetData
Add-content"$($somePath)\log.txt"-value $logline
}
Register-ObjectEvent$watcher"Created"-Action $action
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。