Executing PowerShell from Windows Task Scheduler

孤街浪徒 提交于 2019-12-11 07:18:35

问题


I am trying to execute a very simple PowerShell script from Windows Scheduled Tasks.

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

So it simply writes the string "JLS" to a file on my desktop with the current time as part of the name.

If I execute it using Windows PowerShell ISE it works fine. It also works fine when executing using the command prompt.

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.

What am I missing?


回答1:


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


来源:https://stackoverflow.com/questions/43091466/executing-powershell-from-windows-task-scheduler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!