[英]PowerShell script running as different user in Windows Task Scheduler
[英]Windows Task Scheduler Powershell Script complete and restart
我創建了一個使用FileSystemWatcher的腳本並創建了一個作業。 將文件添加到文件夾后
一旦完成,就結束工作。
我要做的是使用Windows Task Scheduler來檢查作業完成后是否可以立即再次運行。 這樣,如果一個人放下一個文件夾,那么另一個人也可以這樣做。
另外,如果還有另一種方式可以進行這項工作,我很樂意使用它。
我試圖安排任務運行,但最小增量為1分鍾
我希望filesystemwatcher一直運行。 腳本完成后,我希望它再次啟動。
當事件(文件創建)發生時,您不必開始工作,您可以直接移動文件並使用action
參數執行您想做的任何事情。
另外,您不必重新啟動事件,除非您手動注銷它,否則它將一直運行,因此您根本不必使用任務計划程序。
這是代碼
# set the folder you want to watch
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "$home/Desktop/Folder"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
# start job when event is detected
$action = {
Start-Job -ScriptBlock {
# Step 1&2 (You can move the file and rename it with one step)
Move-Item $Event.SourceEventArgs.FullPath -Destination $newPath
} | Wait-Job
}
# Register and start the FileSystemWatcher
Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier FileCreated -Action $action
注意我在代碼中使用了Start-Job
; 但是,您可以忽略它,並將代碼准確地放在$action
。
此代碼將注冊新的FileSystemWatcher,並將繼續監視文件夾中是否有新創建的文件,然后將其移動到新位置。
停止觀察者
Unregister-Event -SourceIdentifier FileCreated
在此處查找有關使用PowerShell跟蹤文件夾更改的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.