[英]PowerShell FileSystemWatcher doesn't function when Scheduled Task
I have a PowerShell script StartWatching.ps1 which monitors a folder using FileSystemWatcher:我有一个 PowerShell 脚本StartWatching.ps1 ,它使用 FileSystemWatcher 监视文件夹:
### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\ScheduledTasks\PlanUpdates"
$watcher.Filter = "*.sql"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = {
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), Processed, $path"
Add-content "C:\ScheduledTasks\PlanUpdates\log.txt" -value $logline
cmd.exe /c 'C:\ScheduledTasks\PlanUpdates\planUpdate.bat'
}
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY
$created = Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 1}
The script launches a .bat-file planUpdate.bat whenever an .sql-file is created in the folder.每当在文件夹中创建 .sql 文件时,脚本都会启动 .bat 文件planUpdate.bat 。 The .bat-file executes the contents of the .sql-file on a specific SQL server (database is stated in .sql-file), and then moves the file to sub-folder "Old": .bat 文件在特定 SQL 服务器上执行 .sql 文件的内容(数据库在 .sql 文件中说明),然后将文件移动到子文件夹“旧”:
for %%f in (.\*.sql) do sqlcmd -S <server> -i %%f & move "%%f" .\Old
The PowerShell script works flawlessly when I launch it by right-clicking and selecting "Run with PowerShell".当我通过右键单击并选择“使用 PowerShell 运行”启动 PowerShell 脚本时,它可以完美运行。
I need to make the script work when executed from cmd before it can work as a Scheduled Task.我需要让脚本在从 cmd 执行时工作,然后才能作为计划任务工作。 But it doesn't work when I launch it from cmd with:但是当我从 cmd 启动它时它不起作用:
powershell -noexit -file C:\ScheduledTasks\PlanUpdates\StartWatching.ps1
It opens a command-window as if the script is running, but FileSystemWatcher doesn't register any events它打开一个命令窗口,就好像脚本正在运行一样,但 FileSystemWatcher 不注册任何事件
I hope you can help as I have been trying to solve this all day.我希望你能提供帮助,因为我一整天都在努力解决这个问题。
Here's the task xml:这是任务xml:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2016-04-25T12:57:08.5588603</Date>
<Author>CRB\IUSR</Author>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>CBB\IUSR</UserId>
<LogonType>Password</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>Queue</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>-Noexit -File C:\ScheduledTasks\PlanUpdates\StartWatching.ps1</Arguments>
</Exec>
</Actions>
</Task>
I came across the same problem.我遇到了同样的问题。
I don't know why this works, but try changing我不知道为什么会这样,但尝试改变
powershell -noexit -file C:\ScheduledTasks\PlanUpdates\StartWatching.ps1
to到
powershell -noexit -file ". C:\ScheduledTasks\PlanUpdates\StartWatching.ps1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.