在Windows任务计划程序中运行Powershell脚本(Windows 7)

时间:2013-08-20 08:36:20

标签: powershell scheduled-tasks powershell-v2.0

您好我有一个脚本并且正在尝试在任务计划程序中启动它。

这给我带来了两个问题。

  1. 是脚本将激活一个需要运行和显示GUI的应用程序。那么如何让Task Scheduler在特定时间自动登录Windows。计算机永远不会/很少被关闭或重新启动。
  2. 2。 我还没有从Task Scheduler成功启动Powershell脚本。我试过以下:

    powershell.exe -noexit -executionpolicy bypass -command &'Y:\Run Test\StartPowershellScript.ps1'
    

     powershell.exe -noexit -ExecutionPolicy Bypass -File 'Y:\Run Test\StartPowershellScript.ps1'
    

    以上的变种。

    powershell.exe -noexit -ExecutionPolicy Bypass &'Y:\Run Test\StartPowershellScript.ps1'
    

    我尝试了一些变化,包括和不使用&“”&''&等等。

    现在我不确定它是否可以完成。但我希望我能在这里得到一些帮助。

    修改1。 我刚刚尝试了以下内容。

    powershell -noexit -ExecutionPolicy Bypass &'Y:\Run Test\StartPowershellScript.ps1'
    

    我在powershell中做了这个。

    set-executionpolicy -scope Process -executionPolicy Unrestricted -force
    set-executionpolicy -scope CurrentUser -executionPolicy Unrestricted -force
    set-executionpolicy -scope LocalMachine -executionPolicy Unrestricted -force
    

    但是,如何在重新启动计算机后确保此操作仍然存在?

    编辑2。 因为我似乎无法使计划任务登录或解锁计算机,是否有设置任务在特定时间运行登录计算机,以便计划任务可以在此时运行?

1 个答案:

答案 0 :(得分:2)

  1. 您无法使用计划任务登录GUI。如果您需要交互式桌面,请启用automatic logon

  2. 从参数列表中删除-NoExit,否则PowerShell将在脚本完成后继续运行。同时用双引号替换单引号。

  3. 执行策略是持久的,除非它被本地或组策略取代(在这种情况下Set-ExecutionPolicy应该抛出错误)或者您使用临时配置文件登录。

    如果这没有帮助,您需要提供更多详细信息。这项任务根本没有开始吗?你收到错误了吗?任务历史和事件日志说什么? Get-ExecutionPolicy的输出是什么?

相关问题