简体   繁体   English

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

[英]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.环境:Windows 10 工作站。 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.问题:我有一个 Powershell 脚本,手动启动时可以正常运行,但是,当我尝试通过任务计划程序启动它时,TS 显示上次运行结果“操作成功完成(0x0)”但文件未上传. 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.现在,当我手动运行脚本时,Powershell window 将在脚本运行时打开,但屏幕上没有任何显示,然后当脚本完成运行时,window 将关闭。 This window does not appear when the Task Scheduler runs the script - leading me to believe that TS is not really running the script.当任务计划程序运行脚本时,这个 window 没有出现 - 让我相信 TS 并没有真正运行脚本。

What I've Tried:我试过的:

  • Changing the name of the user account running the task (myself, SYSTEM, LOCAL SERVICE*)更改运行任务的用户帐户的名称(我自己、SYSTEM、LOCAL SERVICE*)
  • Running Powershell from both C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe从 C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 和 C:\Windows\SysWOWshell.exe 运行 Powershell
  • Adding -ExecutionPolicy Bypass to the arguments box-ExecutionPolicy Bypass添加到 arguments 框
  • 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坐在曲速引擎上照顾它们,尽管它是一个无用和不体面的 position

I'm including the XML code for my task in case someone sees something in there that I may be missing.我为我的任务添加了 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>

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.将文件复制到本地目录后,将 PowerShell 脚本更改为指向该文件的副本,它就起作用了。

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

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