4
votes

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>
2
Problems with running a script as a scheduled task are almost always related to either access rights for the account that the task is running under or profile differences for that account. - EBGreen
In the Task Scheduler, what is the value of "Last Run Result" for this task? - Samuel Prout
@EBGreen the local administrator account is being used. Thank you - Walking
@SamuelProut are you asking about the correlation id? Can't seem to find the "Last Run Result" under the history tab. Thank you - Walking
Sounds like a DCOM Identity issue. Have you tried this answer? - briantist

2 Answers

2
votes

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

1
votes

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.