Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

  1. getting a result in the task scheduler so I may know if everything worked allright or if there were errors

  2. get the output of the script into a output.txt file (incremental file is possible?)

  3. run the script hidden

share|improve this question
    
You 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). –  Karan May 22 '13 at 2:12
    
ok but if I want to give the arguments in the Task Scheduler itself, is it possible? How to do it? –  FernandoSBS May 22 '13 at 13:16
add comment

1 Answer

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.

share|improve this answer
    
ok 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 Jun 20 '13 at 11:26
    
also which trigger should I use, I want it to run whenever I unlock the computer after screen saver or power on –  FernandoSBS Jun 20 '13 at 11:55
    
You 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. –  BigHomie Jun 20 '13 at 12:09
    
i've used the tee-object in the script. what else? I don't understand –  FernandoSBS Jun 20 '13 at 12:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.