Task Scheduler - how to run powershell script with multiple parameters?
I don't know if you forgot the - in your question but it's -ExecutionPolicy
. If that script isn't signed then it won't execute that script also, you can use bypass
as the executionpolicy to rule that out.
I doubt you can run scripts and commands one after another in the command line. Add the exit $LASTEXITCODE
to the script and run it with the -File C:\script.ps1
.
If by run hidden you mean no console window, I usually call my scripts like
powershell.exe -noprofile -executionpolicy bypass -file C:\script.ps1
and never see a window.
To get the output of your script into a file, pipe your commands into Tee-Object
"Hello World" | Tee-Object C:\output.file
You can name the file anything you like, implement logic in your script so the file name increments, or - my personal favorite - use YYYYMMddhhmmss in the file name.
Update
Also, taken from Microsoft's Technet Page on Powershell:
Script blocks must be enclosed in braces ({}). You can specify a script block only when running PowerShell.exe in Windows PowerShell. The results of the script are returned to the parent shell as deserialized XML objects, not live objects.
If the value of Command is a string, Command must be the last parameter in the command , because any characters typed after the command are interpreted as the command arguments.
Related videos on Youtube
FernandoSBS
Updated on September 18, 2022Comments
-
FernandoSBS over 1 year
I am using powershell.exe as the Program and "
-Command "& C:\Windows\logon.ps1; exit $LASTEXITCODE"; ExecutionPolicy RemoteSigned
" as the Parameter, but it doesn't seems to be working.Some things I would like:
getting a result in the task scheduler so I may know if everything worked allright or if there were errors
get the output of the script into a output.txt file (incremental file is possible?)
run the script hidden
-
Karan almost 11 yearsYou can use a batch file to do your work (using PowerShell), check the exit code/errorlevel, redirect output etc. You can of course run it hidden. If a batch file is not acceptable you can directly run the PS script hidden (1, 2).
-
FernandoSBS almost 11 yearsok but if I want to give the arguments in the Task Scheduler itself, is it possible? How to do it?
-
FernandoSBS almost 11 yearsok most of the stuff worked, but I am still not getting the result in the text file. Could you give me more hints on that? The script is running automated in loggon but is not outputting it to a file. I'm running it with:
-file "C:\Eternita\Arruma_Maps.ps1" -executionpolicy bypass -noexit
Is it necessary that the output file exists so that it output or the script creates the file if it doesn't exists? -
FernandoSBS almost 11 yearsalso which trigger should I use, I want it to run whenever I unlock the computer after screen saver or power on
-
MDMoore313 almost 11 yearsYou have to pipe the output to a cmdlet that will send the result to the file. Paste my example above and that should get you started. As far as which trigger to use that is a separate question and should be asked in a separate SE question.
-
FernandoSBS almost 11 yearsi've used the tee-object in the script. what else? I don't understand