PowerShell 脚本将手动运行,但不会作为计划任务

时间:2021-04-21 17:52:57

标签: powershell windows-10 scheduled-tasks

以前有人问过这个问题,但没有一个答案适合我。我已经尝试了在这个论坛和其他地方提出的所有建议,但没有任何效果。

环境:Windows 10 工作站。这不是从服务器上传,而是从工作站上传,因为我必须每天手动更新文件。在我们启动并运行新系统之前,这也是一个临时解决方案。

问题:我有一个 Powershell 脚本,在手动启动时可以正常运行,但是,当我尝试通过任务计划程序启动它时,TS 显示上次运行结果“操作成功完成 (0x0)”,但文件没有没有上传。现在,当我手动运行脚本时,会在脚本运行时打开一个 Powershell 窗口,但屏幕上没有显示任何内容,然后当脚本运行完成时,窗口将关闭。当任务计划程序运行脚本时,此窗口不会出现 - 让我相信 TS 并没有真正运行脚本。

我尝试过的:

  • 更改运行任务的用户帐户的名称(我自己、SYSTEM、LOCAL SERVICE*)
  • 从 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 和 C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe 运行 Powershell
  • 向参数框添加-ExecutionPolicy Bypass
  • 将完整路径添加到“开始于”框中
  • 将其创建为基本任务和任务
  • 坐在曲速引擎上并照顾它们,尽管这是一个毫无用处和不体面的位置

我包含了我的任务的 XML 代码,以防有人看到我可能遗漏的东西。

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

我希望我提供了足够的信息,让别人知道我可能做错了什么。

*注意 - 本地系统给了我一个找不到目录错误

1 个答案:

答案 0 :(得分:0)

我想通了。任务计划程序不喜欢将要传输的文件放在不在本地计算机上的驱动器上。一旦我将文件复制到本地目录,将 PowerShell 脚本更改为指向该文件的副本,它就起作用了。