从任务计划程序启动时,Powershell脚本未完成

时间:2018-07-17 08:32:19

标签: azure-devops taskscheduler

我在这里有一个powershell脚本:

https://github.com/Azure/azure-devtestlab/blob/master/Artifacts/windows-vsts-download-and-run-script/DownloadVstsDropAndExecuteScript.ps1

我用来下载VSTS构建工件的

。当我在Powershell中运行脚本时,该脚本可以完美运行。当我像这样从命令提示符运行它时,它可以正常工作

powershell.exe -ExecutionPolicy Bypass C:\PathToScript\DownloadVstsDropAndExecuteScript.ps1 -parameters

但是,当我尝试将其作为计划任务运行时,脚本似乎无法完成下载(zip文件大小很小,当我尝试打开时,文件已损坏)。既可以使用用户登录模式,也可以不使用登录用户。

任务xml在这里:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2"     xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2018-07-17T08:39:31.2909587</Date>
    <Author>LOCALPC\Administrator</Author>
    <URI>\Download Nightly Artifacts</URI>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2018-07-17T04:44:44</StartBoundary>
      <ExecutionTimeLimit>PT2H</ExecutionTimeLimit>
      <Enabled>true</Enabled>
      <ScheduleByWeek>
        <DaysOfWeek>
          <Monday />
          <Tuesday />
          <Wednesday />
          <Thursday />
          <Friday />
          <Saturday />
        </DaysOfWeek>
        <WeeksInterval>1</WeeksInterval>
      </ScheduleByWeek>
    </CalendarTrigger>
   </Triggers>
  <Principals>
     <Principal id="Author">
       <UserId>S-1-5-21-1204101111-744349012-1702229681-300</UserId>
       <LogonType>InteractiveToken</LogonType>
      <RunLevel>HighestAvailable</RunLevel>
     </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>false</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>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>true</WakeToRun>
    <ExecutionTimeLimit>PT2H</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <Exec>
      <Command>powershell.exe</Command>
      <Arguments>C:\PathToScript\DownloadVstsDropAndExecuteScript.ps1 -parameters</Arguments>
    </Exec>
  </Actions>
</Task>

1 个答案:

答案 0 :(得分:0)

已替换

Invoke-RestMethod -Uri "$downloadUrl" -Headers $Headers -Method Get -Outfile $outfile | Out-Null

$wc = New-Object System.Net.WebClient
    foreach ($key in $Headers.Keys) {
        $wc.Headers.Add($key, $Headers[$key])
    }
    $wc.DownloadFile($downloadUrl, $outfile)

感谢@anheledir解决方案