1
votes

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
Try using the File param instead of Command in your Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "c:\Users\jls\desktop\untitled2.ps1"James C.
For debug, try to put full path, like $path='C:\users\jls\desktop\resultfile_'+...Mihail Kuznesov
And put to your script line: add-content 'c:\users\public\homevalue.txt' $home and check it content after executionMihail Kuznesov

1 Answers

4
votes

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