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

标签 powershell scheduled-tasks powershell-2.0

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

这给我带来了两个问题。

  1. 是该脚本将激活一个需要 GUI 运行并显示的应用程序。那么如何让任务计划程序在特定时间自动登录到Windows。计算机永远不会/很少被关闭或重新启动。

2. 我尚未从任务计划程序成功启动 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. 您无法使用计划任务登录 GUI。启用automatic logon如果您需要交互式桌面。

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

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

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

关于powershell - 在 Windows 任务计划程序中运行 Powershell 脚本(Windows 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18330523/

相关文章:

powershell - 如何通过自动化运行 PowerShell 脚本而不会遇到主机问题

ssl - 使用要求 SSL 设置为 "Require"的 PowerShell 创建 IIS7 Web 应用程序

电源外壳。无法从 {} 获取变量

c# - 如何显示计划任务的属性?

macos - 如何从 PowerShell 脚本打开另一个 PowerShell 控制台

batch-file - 批处理手动运行,但不在计划任务中

windows - 如何让我的程序在 Windows 7 启动时以不同的权限运行?

powershell - 如何修改我的 powershell 脚本以搜索机器上的所有 jpg/jpeg

scripting - PowerShell 脚本第一次失败但第二次有效

powershell - 通过管道将 0..10 连接到高级函数