Fixing (0x1) Error When Executing PowerShell Scripts Using Task Scheduler - powershell

I'm trying to run a PowerShell script that runs hourly.
When I run the script outside Task Scheduller it goes right, but when I try to run using task scheduller, it shows me an (0x1) error.
Here is how my task is configured:
Action: Start a Program
Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Argument (Optional): powershell.exe -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File \\C:\xampp\java_monitor.ps1
Start in (Optional): Empty
Does anyone knows what should I change or add to fix execution?

Related

Powershell window does not hide even after adding -WindowsStyle hidden while running task from task scheduler

I have created a task in task scheduler.
Its basically a powershell script which has to run in interactive mode.
Task is running under SYSTEM account.
In Actions tab, under Program/Script I have added path of the ServiceUI.exe e.g, C:**\ServiceUI.exe
Under Add arguments option, I have added C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -Windowstyle hidden -NoProfile -Executionpolicy bypass -file "C:**\PS1.ps1"
When I run the task, powershell window prompt shows for a fraction of a second.
Could someone please suggest a way to hide it?
I was able to fix the problem by using vb script.
WScript.Quit CreateObject("WScript.Shell").Run("powershell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File " & PSscriptPath, 0, true)
In Actions tab, under Program/Script I had path of the ServiceUI.exe e.g, C:**\ServiceUI.exe
Added wscript.exe "vbs script path" in the arguments option.
Powershell prompt doesn't come up, and task works perfectly in interactive mode under SYSTEM account using these options.

Windows scheduler not executing task

The below command-line will successfully execute a powershell command invisibly; saving the output to a text file:
C:\Windows\System32\wscript.exe C:\temp\invisible.vbs C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -WindowStyle Hidden -command ( (Get-StartApps -Name 'RDP (Tools)').AppID > c:\Temp\AppB.txt )
invisible.vbs:
CreateObject("Wscript.Shell").Run "" & WScript.Arguments(0) & "", 0, False
However, if I create a Windows scheduler task where the Action is: EXE = C:\Windows\System32\wscript.exe and...
ARGUMENTS = C:\temp\invisible.vbs C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -WindowStyle Hidden -command ( (Get-StartApps -Name 'RDP (Tools)').AppID > c:\Temp\AppB.txt )
...the text file doesn't get created when I run the scheduler task; even though it says operation completed successfully (0x0).
How can I create a Windows scheduler task that does the same thing as from a command line?
HINT: When I run the scheduled task, a powershell.exe process is spawned by Task Scheduler. However, it doesn't appear to be doing anything. Something is causing the PowerShell process to not run as expected. Unfortunately, I can't tell what's happening.
HINT2: When I completely eliminate the VBScript; where Task Schedule executes only PowerShell; and, the respective command, it works fine. Unfortunately, I don't know vbscript well enough to know why this isn't working.
MKNANET,
FWIW: I had no problem running w/o the VBS as follows:
Task Scheduler--
Command: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
Arguments: -noprofile -WindowStyle Hidden -command (Get-StartApps -Name "*Office*").AppID >> G:\Test\AppB.txt
Note: as I didn't have any 'RDP (Tools)' items in my app list I changed it to look for Office items.
HTH

How do I schedule this .ps1 file to run daily?

I have a .ps1 file that works when I run it in PowerShell (it registers a filesystemwatcher to watch for changes in an excel file), but I can't get it to work when I schedule it in task scheduler.
Is this because my inputs are off or do I need to export this .ps1 into something else?
My inputs-
Program = PowerShell.exe
Add arguments = -ExecutionPolicy Bypass \blah blah blah\testing file.ps1
Start in is empty
Try this:
Action: Start a program
Program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments: -ExecutionPolicy Bypass -file "\blah blah blah\testing file.ps1"

How to execute PowerShell script in MoveIT central?

I have a bat file include command em32\WindowsPowerShell\v1.0\powershell.EXE -NoLogo -NoProfile -Command c:\temp\GL_Format_Update.ps1. Then used command line App in MoveIT central to execute bat file. The script can't produce the output file as expected. Command can be run in CMD window successfully. It seems like MoveIT service owner can't execute PowerShell script.
I had a similar issue and found that simply putting the entire command into the CommandLineApp_AppPath was throwing an error. By breaking it up into the path to powershell and the arguments to powershell, I was able to successfully call and execute my script. My script also took 3 parameters.
Create a task with a process. Select the built-in script "Command Line App". Set the parameters as follows:
CommandLineApp_AppPath = C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.EXE
CommandLineApp_AppParms = -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "E:\PowerShell\CreateManifest.ps1 -Folder \\mdcvmsfms11u\DataTransfer\BFClientGateway\Test\Download\2129\PPfAandDP -ManifestName MS_CONTROL_ -OutputType FULL"

Get bat file to contiune when lauching PowerShell

I have a bat file that is launching a powerShell script. I would like for the bat file to keep moving after it launches the script and not wait for the powerShell script to complete. Every time right now when i launch the powerShell script the bat files waits till the powerShell script finishes before it moves on. Here is how I'm calling my powerShell script:
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "&'C:\Users\sharph\Desktop\test.ps1'"
SS64 'start' help page
You'll want to start it with the start command, like this;
start "" "PowerShell"
This will start a program without waiting for it to close, although that behavior can be re-added with the /w or /wait option. The blank "" is in place of the title, not always needed but generally a safe thing to add.
Perhaps this will work?
start "" "PowerShell" -NoProfile -ExecutionPolicy Bypass -Command "^& 'C:\Users\sharph\Desktop\test.ps1'"
of course, the & had to be delimited to ^&.