简体   繁体   English

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

[英]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 不注册任何事件它打开一个命令窗口,就好像脚本正在运行一样,但 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.

相关问题 在Powershell中创建的计划任务未出现在Task Scheduler中 - Scheduled task created in Powershell doesn't appear in Task Scheduler 设置“无论用户是否登录都运行”时,PowerShell 不会作为计划任务运行 - PowerShell doesn't run as scheduled task when “Run whether user is logged on or not” is set IE不能从PowerShell脚本作为计划任务打开 - IE doesn't open from PowerShell script as a scheduled task Powershell Set-ADUser 不在计划任务中运行 - Powershell Set-ADUser doesn't run in scheduled task Powershell:将计划任务设置为在用户未登录时运行 - Powershell: Set a Scheduled Task to run when user isn't logged in PowerShell 计划任务:打开 PowerShell 和要运行的脚本但不执行脚本 - PowerShell scheduled task: opens PowerShell and script to be run but doesn't execute script 计划的PowerShell任务将不会写入文件 - Scheduled PowerShell task won't write to file 预定的Powershell任务无法与浏览器交互 - Scheduled Powershell task can't interact with browser powershell脚本可在run.exe中运行,但不能作为计划任务运行 - powershell script works in run.exe but doesn't work as scheduled task 使用任务计划程序计划的Powershell脚本无法运行…脚本运行正常 - Powershell script scheduled using Task scheduler doesn't run…script runs fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM