PowerShell FileSystemWatcher不支持';t在执行计划任务时运行

PowerShell FileSystemWatcher不支持';t在执行计划任务时运行,powershell,batch-file,scheduled-tasks,windows-server-2008-r2,filesystemwatcher,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 =

我有一个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 server上执行.sql文件的内容(数据库在.sql文件中声明),然后将该文件移动到子文件夹“Old”:

它会打开一个命令窗口,就像脚本正在运行一样,但FileSystemWatcher不会注册任何事件

我希望你能帮忙,因为我一整天都在努力解决这个问题

以下是任务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>

2016-04-25T12:57:08.5588603
CRB\IUSR
真的
CBB\IUSR
密码
高可用性
队列
假的
真的
真的
真的
假的
真的
假的
真的
真的
假的
假的
假的
PT0
7.
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-Noexit-文件C:\ScheduledTasks\PlanUpdates\StartWatch.ps1

我遇到了同样的问题

我不知道为什么会这样,但试着改变一下

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


您需要给出任务定义。导出它并在此处提供XML或提供等效的
schtasks.exe
命令。我添加了任务XML。@KHH您好您找到过解决方案吗?
<?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>
powershell -noexit -file C:\ScheduledTasks\PlanUpdates\StartWatching.ps1
powershell -noexit -file ". C:\ScheduledTasks\PlanUpdates\StartWatching.ps1"