Powershell script not running using scheduled task

May 5, 2018 at 03:21:14
Specs: Windows 10
Hi,
I have set up a scheduled task to run a powershell script Test.ps1. This script is meant to run three other powershell Scripts one by one in a powershell script e.g
'C:\Users\WP\Desktop\Scripts\1.ps1'
'C:\Users\WP\Desktop\Scripts\2.ps1'
'C:\Users\WP\Desktop\Scripts\3.ps1'

I have scheduled the task as under:

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add argruments: -noprofile -executionpolicy unrestricted -noninteractive -file "'C:\Users\WP\Desktop\Scripts\Test.ps1"

I am running it with highest privileges
The task runs without error, return 0. But the ps script wasn’t executed. It works fine when I run it manually. Any inputs?


See More: Powershell script not running using scheduled task

Reply ↓  Report •

#1
May 5, 2018 at 07:26:10
Hard to tell without output, but the posted script would only display
C:\Users\WP\Desktop\Scripts\1.ps1
C:\Users\WP\Desktop\Scripts\2.ps1
C:\Users\WP\Desktop\Scripts\3.ps1
and then exit. Also, you seem to have an errant single quote in your arguments line, but that probably isn't an issue if PowerShell's not throwing an error.

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#2
May 6, 2018 at 05:25:04
Hi ,

I have the requirement to run 1.ps1, 2.ps1 and 3.ps1 sequentially, hence I am using Test.ps1. Below is how the Test.ps1 script looks like:

$PSScriptroot = 'C:\Users\WP\Desktop\Scripts'

&"$PSScriptroot\1.ps1"
&"$PSScriptroot\2.ps1"
&"$PSScriptroot\3.ps1"


I have scheduled the task to run the Test.ps1 script .But it doesn't execute. The script works fine when executed manually. Please let me know if you need any other inputs.


Reply ↓  Report •

#3
May 6, 2018 at 11:13:17
Yeah, that's more what I'd expect to see in a script.

Well, first remove the errant single quote in -file "'C:\Users\WP\Desktop\Scripts\Test.ps1". Once you do that, add the line

Start-Transcript 'C:\Users\WP\Desktop\Scripts\scriptLog.txt'
to Test.ps1. That'll log the output, so we can actually see what's happening.

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •
Related Solutions


Ask Question