我正在尝试编写一个监视文件夹的PowerShell脚本,以便每当将新的JSON文件添加到该文件夹或其任何子文件夹时,都会运行mongoimport命令以在MongoDB中导入该JSON文件。我编写了以下脚本,但它似乎没有完成这项工作。我觉得Invoke-Command
部分存在问题。
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\git\DataChain\UploadStation\to"
$watcher.Filter = "*.json"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
Invoke-Command -FilePath "C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe" -ArgumentList (db myDB collection jsonFiles type json file $path)
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {Start-Sleep 5}
答案 0 :(得分:0)
而不是Invoke-Command
使用call operator(&
)和splat参数:
$params = '--db', 'myDB',
'--collection', 'jsonFiles',
'--type', 'json',
'--file', $Event.SourceEventArgs.FullPath
& "C:\Program Files\MongoDB\Server\3.4\bin\mongoimport.exe" @params