计划任务时,PowerShell FileSystemWatcher不起作用

时间:2016-04-25 12:26:49

标签: powershell batch-file scheduled-tasks windows-server-2008-r2 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}

只要在文件夹中创建.sql文件,脚本就会启动.bat文件 planUpdate.bat 。 .bat文件执行特定SQL服务器上的.sql文件的内容(数据库在.sql文件中声明),然后将文件移动到子文件夹“Old”:

for %%f in (.\*.sql) do sqlcmd -S <server> -i %%f  & move "%%f" .\Old

当我通过右键单击并选择“使用PowerShell运行”启动PowerShell脚本时,它可以完美运行。

我需要在从cmd执行脚本之前使其工作,然后才能作为计划任务。但是当我从cmd启动它时,它不起作用:

powershell -noexit -file C:\ScheduledTasks\PlanUpdates\StartWatching.ps1

它打开一个命令窗口,就像脚本正在运行一样,但FileSystemWatcher没有注册任何事件 It opens a command-window as if the script is running, but FileSystemWatcher doesn't register any events

我希望你可以提供帮助,因为我一整天都试图解决这个问题。

这是任务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>

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。

我不知道为什么会有效,但请尝试更改

powershell -noexit -file C:\ScheduledTasks\PlanUpdates\StartWatching.ps1

powershell -noexit -file ". C:\ScheduledTasks\PlanUpdates\StartWatching.ps1"