【问题标题】:Executing PowerShell from Windows Task Scheduler从 Windows 任务计划程序执行 PowerShell
【发布时间】:2017-08-22 19:41:24
【问题描述】:

我正在尝试从 Windows 计划任务执行一个非常简单的 PowerShell 脚本。

我的 PowerShell 脚本是这样的:

$currentTime = (Get-Date).ToString('yyyy-MM-ddTHH.mm.ss')
$contentToDump = "JLS"
$path = "$home\Desktop\resutfile_" + $currentTime + ".txt"
Add-Content $path $contentToDump

所以它只是将字符串“JLS”写入我桌面上的一个文件,并将当前时间作为名称的一部分。

如果我使用 Windows PowerShell ISE 执行它,它工作正常。使用命令提示符执行时也可以正常工作。

但是当我连接到计划任务时它不起作用。该任务返回一个有效的("0x0")-result,但没有生成文件。

我的任务配置是这样的:

Action: Start a program
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command “&{c:\Users\jls\desktop\untitled2.ps1}”
Start in (optional): C:\users\jls\desktop

该任务设置为以“我”身份以最高权限运行,并且它在我作为管理员的机器上本地运行。

我错过了什么?

【问题讨论】:

  • 尝试在参数中使用文件参数而不是命令:-NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "c:\Users\jls\desktop\untitled2.ps1"
  • 为了调试,尽量放完整路径,比如 $path='C:\users\jls\desktop\resultfile_'+...
  • 并放入您的脚本行: add-content 'c:\users\public\homevalue.txt' $home 并在执行后检查它的内容

标签: powershell scheduled-tasks


【解决方案1】:

您需要将Add Arguments 中的-Command 替换为-File

Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File c:\Users\jls\desktop\untitled2.ps1

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多