我想通过.bat文件运行.ps1文件

时间:2016-10-17 10:11:12

标签: powershell scheduled-tasks

我正在尝试通过任务计划程序运行此PowerShell代码,因为我想运行此代码24 * 7。任务计划程序未运行.ps1文件。所以我想从Task Scheduler运行一个.bat文件,它会调用这个PowerShell文件。

以下是我的PowerShell代码(文件名是Watch.ps1):

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "D:\MigrationWorkflow\UAT"
$watcher.EnableRaisingEvents = $true

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
$action = { $path = $Event.SourceEventArgs.FullPath
            $changeType = $Event.SourceEventArgs.ChangeType
            $logline = "$(Get-Date), $changeType, $path"
            Invoke-Expression 'MASysMgr.exe -w MigrationUAT.smw -f "D:\MigrationWorkflow\UAT\ParameterUat.smp" -l WorkflowResults.log -showoutput' }

### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
$created = Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 0}

1 个答案:

答案 0 :(得分:0)

任务计划程序可以正常运行PowerShell脚本,但您需要将powershell.exe作为要执行的程序,并-File "C:\path\to\watch.ps1"作为程序的参数。

在您的情况下,您还应该添加参数-NoExit,以便PowerShell不会终止,并从脚本中删除while循环(或至少将睡眠时间设置为几百毫秒,以便系统可以在此期间做其他事情。)