Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running PowerShell Script on Task Scheduler

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>
like image 793
Walking Avatar asked Oct 19 '22 11:10

Walking


2 Answers

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

like image 168
James Morgan Avatar answered Oct 22 '22 11:10

James Morgan


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.

TaskSchedulerLastRunResult

and this is where you will find the last run result.

like image 25
Luke Avatar answered Oct 22 '22 11:10

Luke