5

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

Kevin Fegan
  • 4,997
FernandoSBS
  • 2,023

1 Answers1

2

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.

MDMoore313
  • 6,336