问题
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