Running PowerShell Script on Task Scheduler - powershell

I've looked at all PowerShell script on task scheduler answers in other questions and have not been able to find one that will solve this.
I have tried running the PowerShell Script straight through task scheduler using:
powershell.exe -file c:\path_to_your_script\script.ps1
As well as creating a batch to run the PowerShell script.
The PowerShell Script works perfectly find when its ran on its own as well as when ran through the batch manually.
It opens an excel, refreshes it, saves, and closes.
I'm unable to successfully schedule this through the task handler.
Any advice?
Thank you for your help
Edited:
#luke this is the export of the settings for task scheduler
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2015-05-11T12:40:51.6084684</Date>
<Author>----------</Author>
</RegistrationInfo>
<Triggers>
<TimeTrigger>
<Repetition>
<Interval>PT1H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2015-05-11T12:30:00</StartBoundary>
<Enabled>true</Enabled>
</TimeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>------\Administrator</UserId>
<LogonType>Password</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>true</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>true</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\cmd.exe</Command>
<Arguments>/q /c "C:\Users\---------\Documents\PowerBI\PowerShell\RunExcelRefreshingScriptPowerShell2.bat"</Arguments>
</Exec>
</Actions>
</Task>

I was getting the same issue where I created a scheduled task to run a PowerShell script I created. I found that if you add the path to the Powershell.exe (ie: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe) in the Program/script window and the path to your actual PowerShell script (ie: D:\Scripts\PowershellScript.ps1)(See Example Below) in the Arguments window it will work. You would also have to check the Server's PowerShell execution policy to make sure it's set to "unrestricted". You can also try adding it as an argument (-ExecutionPolicy Bypass).
I hope this will help someone.
Example:
Task to Run PowerShell Scripts

can you provide an export of the settings of the Task Schedule without the username or password.
I have many powershells running through Task Scheduler and not a single issue because I have my scheduler call the bat which starts the powershell.
and this is where you will find the last run result.

Related

Task Scheduler failed to start "Taskname" task for user "(NONE)". Additional Data: Error Value: 2147942487

I faced with this error first time. System: Windows server 2019 10.0.17763 N/A Build 17763
This is new server with last WSUS updates.
I've created Windows Scheduler Task that runs powershell script. This script does application start after reboot. Scheduler runs ps1 script as domain service account. Account has local administrator rights.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2022-08-23T11:14:12.8862128</Date>
<Author>admin\user</Author>
<URI>\Application AutoStart</URI>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>DOMAINUSERSID</UserId>
<LogonType>S4U</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell</Command>
<Arguments>-NoProfile -ExecutionPolicy Bypass -file "D:\Application\ApplicationAutoStart.ps1"</Arguments>
</Exec>
</Actions>
</Task>
Every time system starts this task fails with "Task Scheduler failed to start "My" task for user "(NONE)". Additional Data: Error Value: 2147942487."
But when I run this task manually it start without problems.
I tried to start cmd as domain service account and start PowerShell script and got no problems

PowerShell script will run manually but not as scheduled task

This question has been asked before, but none of the answers work for me. I've tried everything that has been suggested on both this forum and everywhere else and nothing has worked.
The Environment: Windows 10 workstation. This isn't being uploaded from a server, it's being uploaded from a workstation because I have to manually update the file daily. This is also a temporary solution until we get the new system up and running.
The Problem: I have a Powershell script that will run fine when launched manually, however, when I try to launch it through Task Scheduler, TS says the Last Run Result "The operation completed successfully (0x0)" but the file wasn't uploaded. Now, when I run the script manually, a Powershell window will open while the script runs but nothing shows on the screen, then when the script is finished running, the window closes. This window does not appear when the Task Scheduler runs the script - leading me to believe that TS is not really running the script.
What I've Tried:
Changing the name of the user account running the task (myself, SYSTEM, LOCAL SERVICE*)
Running Powershell from both C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
Adding -ExecutionPolicy Bypass to the arguments box
Adding the full path to the Start In box
Creating it as a Basic Task and just Task
Sitting on the warp engines and nursing them in spite of it being an unavailing and undignified position
I'm including the XML code for my task in case someone sees something in there that I may be missing.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-04-16T13:34:17.5511135</Date>
<Author>(redacted)</Author>
<URI>\Nightly Update Staff</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2021-04-16T17:05:00</StartBoundary>
<ExecutionTimeLimit>PT30M</ExecutionTimeLimit>
<Enabled>true</Enabled>
<ScheduleByWeek>
<DaysOfWeek>
<Monday />
<Tuesday />
<Wednesday />
<Thursday />
<Friday />
</DaysOfWeek>
<WeeksInterval>1</WeeksInterval>
</ScheduleByWeek>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-19</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
<Arguments>-ExecutionPolicy Bypass C:\StaffUpload.ps1</Arguments>
<WorkingDirectory>C:\Users\%username%\Desktop</WorkingDirectory>
</Exec>
</Actions>
</Task>
I hope I've provided enough information for someone to see what I may be doing wrong.
*NOTE - LOCAL SYSTEM gave me a Directory not found error
I figured it out. Task Scheduler doesn't like the file to be transferred to be on a drive that is not on the local machine. Once I copied the file to a local directory, changed the PowerShell script to point to that copy of the file, it worked.

Task scheduler task not executing ps1 scripts

I am trying to run a ps1 file in the Task scheduler.
The task should run every day in the evening and history shows that it has run,
but not really because I can see in SonarQube that it did not.
Every individual step works as intended and if I run the action and command everything is fine:
powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\sonarqube\run_sonar_scan.ps1
And I also tried using the full path to powershell
The task:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2020-07-27T13:10:30.3806911</Date>
<Author>xxx\yyy</Author>
<Description>Starts the SonarScanner bat files from run_sonar_scan.ps1</Description>
<URI>\DdDRetail\SonarScanner</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<StartBoundary>2020-07-27T21:00:00</StartBoundary>
<ExecutionTimeLimit>PT4H</ExecutionTimeLimit>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>false</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>true</WakeToRun>
<ExecutionTimeLimit>PT8H</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>3</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments>-NoProfile -ExecutionPolicy Bypass -File "C:\sonarqube\run_sonar_scan.ps1"</Arguments>
</Exec>
</Actions>
</Task>
The ps1 file:
$repo_file_path="C:\Users\xxx\source\repos"
Start-Process "cmd.exe" "/c $repo_file_path\run_articleservice_scan.bat"
The bat file
updates a local repo from github
updates Nuget packages
runs a SonarScanner
The user needed to be either an administrator or having permissions to Run whether the user is logged on or not. User was changed to Administrator, all is fine and the task is executed as expected.

Windows server 2008 task scheduler does not run task at start up

I have VPS with Windows Server 2008. I have configured Task Scheduler to run the *.exe (please, see below XML file I retrieved from c:\Windows\System32\Tasks\Chronos\). The Run on startup trigger is enabled (I suppose it is BootTrigger).
The task runs and works until the Windows reboots due to updates or other reasons. The task is not started after reboot. I must login to Windows and manually start Task Scheduler, select the task and press Run button). Why it does not launch the task on Windows startup?
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2014-09-06T16:47:00.9812</Date>
<Author>VMI28078\Administrator</Author>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<RunLevel>LeastPrivilege</RunLevel>
<UserId>VMI28078\chronos</UserId>
<LogonType>S4U</LogonType>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
<RestartOnFailure>
<Interval>PT1M</Interval>
<Count>999</Count>
</RestartOnFailure>
</Settings>
<Actions Context="Author">
<Exec>
<Command>D:\chronos\gate\ChronosGate.exe</Command>
<Arguments>rVPSgate.config</Arguments>
<WorkingDirectory>D:\chronos\gate</WorkingDirectory>
</Exec>
</Actions>
</Task>
The Web has many advices on using INSTSRV or SC but:
INSTSRV.EXE is NOT present in Windows Server 2008 R2
SC.EXE did not help since it does not allow to create services using *.bat files (I need to pass command line argument to my *.exe which could be accomplished using a *.bat file)
cron for windows - maybe yes, I did not try
The easiest way is to use http://nssm.cc which has GUI and worked like a charm for me.

powershell invisible executable

I created a powershell script that checks if an executable is running. If it is do nothing, if it is not running, run the executable. I do this because the app cannot run as a service.
jcon.ps1
$Prog = "C:\Program Files (x86)\JCON\Edifact\edifact.exe"
if (! (ps | ? {$_.path -eq $prog})) {& $prog}
This script works when I run it through windows explorer - Right click on jcon.ps1 and click run with powershell.
However, there is an issue when I run the script via a scheduled task.
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2012-09-06T10:35:07.1546685</Date>
<Author>BDPNT\jcon</Author>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Repetition>
<Interval>PT10M</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<Enabled>true</Enabled>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>BDPNT\jcon</UserId>
<LogonType>S4U</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>false</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>P3D</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"</Command>
<Arguments>-executionpolicy unrestricted -file "d:\script\jcon.ps1"</Arguments>
</Exec>
</Actions>
</Task>
If I run this code when "edifact.exe" is not running I see the executable is started but I cannot see the GUI. Also users report the application is not functioning properly. However the executable is running but invisible to me - no GUI. The executable is running as a process though. I can see that in the task manager.
The task is running as a local admin. Any ideas on why the GUI is not visible when I call the app through a scheduled task?
<Command>"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"</Command>
<Arguments>-executionpolicy unrestricted -file "d:\script\jcon.ps1"</Arguments>
I already tried the -windowstyle maximized parameter and the issue remained.
Thanks!
I have some powershell scheduled tasks which pop interactive windows. In the Task Scheduler UI, the setting is "Run only when user is logged on". In the XML, you need to change this setting:
<LogonType>S4U</LogonType>
to
<LogonType>InteractiveToken</LogonType>