简体   繁体   English

通过任务计划程序在Powershell中运行C#控制台应用程序

[英]C# Console Application run in Powershell viaTask Scheduler

I'm trying to get my console application to run via powersheell/task scheduler. 我正在尝试让我的控制台应用程序通过powersheell / task Scheduler运行。

If I open powershell and manually run .\\startReport.ps1 it works. 如果我打开powershell并手动运行。\\ startReport.ps1,它将起作用。 The console application doesn't open, just runs silently in the background. 控制台应用程序不会打开,只是在后台静默运行。

However, when I go to create a task via task scheduler, it fails... telling me it completed successfully. 但是,当我通过任务计划程序创建任务时,它失败了……告诉我它已成功完成。 But it didn't. 但事实并非如此。

Any ideas? 有任何想法吗?

  $r = Get-Date
  $fileName =  ".\Logs\" + $r.Date.Year + "-" + $r.Date.Month + "-"+ $r.Date.Day + ".txt"

  .\MyApp.exe > $fileName

Here is how I was able to perform this in my environment with minor modifications. 这是我在我的环境中进行少量修改即可执行的操作。 The code in my script is as below. 我的脚本中的代码如下。 Notice that I am using absolute paths instead of relative as PowerShell will launch with C:\\Windows\\System32 as default path and you may not have permission to create a Logs folder and a log file in there. 请注意,我使用的是绝对路径,而不是相对路径,因为PowerShell将以C:\\ Windows \\ System32作为默认路径启动,并且您可能没有权限在其中创建Logs文件夹和日志文件。

startReport.ps1 Script contents: startReport.ps1脚本内容:

$r = Get-Date
$fileName =  "C:\Data\Logs\" + $r.Date.Year + "-" + $r.Date.Month + "-"+ $r.Date.Day + ".txt"

& C:\Data\MyApp.exe > $fileName

Notice above that I am using & notation to execute an exe. 请注意,上面我在使用&表示法来执行exe。 You can also replace this with any command as well if you are using &. 如果使用&,也可以用任何命令替换它。 What you are doing is also correct. 你在做什么也是正确的。 I would just recommend using absolute path. 我只建议使用绝对路径。

Then in the Task Scheduler I have the below command: 然后在任务计划程序中,我具有以下命令:

PowerShell.exe -File "C:\Data\startReport.ps1"

Again I am using the absolute path to refer the ps1 file. 我再次使用绝对路径引用ps1文件。 You can provide PowerShell.exe for the "Program/Script" and -File "C:\\Data\\startReport.ps1" for the "Add Argument" section in the Action tab of the task scheduler. 您可以在任务计划程序的“操作”选项卡中为“程序/脚本”提供PowerShell.exe ,为“添加参数”部分提供-File "C:\\Data\\startReport.ps1"

Other Considerations 其他注意事项

  1. Within Task Scheduler try running with the highest privilege by clicking "Run with highest privileges" checkbox on the General tab of Create Task dialog. 在Task Scheduler中,尝试通过单击“创建任务”对话框的“常规”选项卡上的“以最高特权运行”复选框,以最高特权运行。 If you don't want that then ensure that Task scheduler has appropriate permissions to create a log file. 如果您不希望这样做,请确保任务计划程序具有创建日志文件的适当权限。
  2. Do ensure that Logs folder exists. 请确保“日志”文件夹存在。

How is your task scheduler configured? 您的任务计划程序如何配置? One of the possibilities is: 一种可能性是:

  1. In actions, select "Start a program". 在操作中,选择“启动程序”。 And program is 程序是

%SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe %SystemRoot%\\ system32 \\ WindowsPowerShell \\ v1.0 \\ powershell.exe

  1. Then, in "Add arguments" you write something like 然后,在“添加参数”中输入类似

cd d:\\folderWithTheScript; cd d:\\ folderWithTheScript; .\\TheScript.ps1 。\\ TheScript.ps1

Then you can use relative to "d:\\folderWithTheScript" pathes 然后,您可以使用相对于“ d:\\ folderWithTheScript”的路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM