问题描述
我正在尝试从 Windows 计划任务执行一个非常简单的 PowerShell 脚本.
I am trying to execute a very simple PowerShell script from Windows Scheduled Tasks.
我的 PowerShell 脚本是这样的:
My PowerShell script is this:
$currentTime = (Get-Date).ToString('yyyy-MM-ddTHH.mm.ss')
$contentToDump = "JLS"
$path = "$home\Desktop\resutfile_" + $currentTime + ".txt"
Add-Content $path $contentToDump
所以它只是将字符串JLS"写入我桌面上的文件中,并将当前时间作为名称的一部分.
So it simply writes the string "JLS" to a file on my desktop with the current time as part of the name.
如果我使用 Windows PowerShell ISE 执行它,它工作正常.使用命令提示符执行时它也能正常工作.
If I execute it using Windows PowerShell ISE it works fine. It also works fine when executing using the command prompt.
但是当我连接到计划任务时它不起作用.任务返回一个有效的 ("0x0")
-result 但没有生成文件.
But when I hook up at Scheduled task it doesn't work. The task returns with a valid ("0x0")
-result but no file is generated.
我对任务的配置是这样的:
My configuration of the task is this:
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
该任务设置为以最高权限作为我"运行,并且它在我作为管理员的机器上本地运行.
The task is set to run as "me" with highest privileges and it is running locally on my machine where I am admin.
我错过了什么?
推荐答案
您需要将Add Arguments
中的-Command
替换为-File
代码>
You need to replace the -Command
with -File
in your Add Arguments
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File c:\Users\jls\desktop\untitled2.ps1
这篇关于从 Windows 任务计划程序执行 PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!