简体   繁体   English

在任务计划程序上运行PowerShell脚本

[英]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. 我在其他问题中查看了有关任务调度程序答案的所有PowerShell脚本,但未能找到解决此问题的脚本。

I have tried running the PowerShell Script straight through task scheduler using: 我尝试使用以下命令直接通过任务计划程序运行PowerShell脚本:

       powershell.exe -file c:\path_to_your_script\script.ps1

As well as creating a batch to run the PowerShell script. 以及创建批处理以运行PowerShell脚本。

The PowerShell Script works perfectly find when its ran on its own as well as when ran through the batch manually. PowerShell脚本可以完美地查找它自己运行以及手动运行批处理时的情况。

It opens an excel, refreshes it, saves, and closes. 它打开一个excel,刷新,保存和关闭。

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 @luke这是任务调度程序设置的导出

<?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>

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. 我有很多通过任务计划程序运行的功能,而不是一个问题,因为我的调度程序调用了启动powershell的bat。

TaskSchedulerLastRunResult

and this is where you will find the last run result. 这是您可以找到最后一次运行结果的地方。

I was getting the same issue where I created a scheduled task to run a PowerShell script I created. 我遇到了同样的问题,我创建了一个计划任务来运行我创建的PowerShell脚本。 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. 我发现如果你在程序/脚本窗口中添加Powershell.exe的路径(即:%SystemRoot%\\ system32 \\ WindowsPowerShell \\ v1.0 \\ powershell.exe)以及实际PowerShell脚本的路径(即:D) :\\ Scripts \\ PowershellScript.ps1)(参见下面的示例)它将在Arguments窗口中工作。 You would also have to check the Server's PowerShell execution policy to make sure it's set to "unrestricted". 您还必须检查服务器的PowerShell执行策略,以确保它设置为“不受限制”。 You can also try adding it as an argument (-ExecutionPolicy Bypass). 您也可以尝试将其添加为参数(-ExecutionPolicy Bypass)。 I hope this will help someone. 我希望这会对某人有所帮助。 Example: Task to Run PowerShell Scripts 示例: 运行PowerShell脚本的任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM