1

The Environment: Windows 10 workstation. 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. 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. This window does not appear when the Task Scheduler runs the script - leading me to believe that TS is not really running the script.

What I've Tried:

  • Changing the name of the user account running the task (myself, SYSTEM, LOCAL SERVICE*)
  • Running Powershell from both C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe
  • Adding -ExecutionPolicy Bypass to the arguments box
  • 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

I'm including the XML code for my task in case someone sees something in there that I may be missing.

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

What I may be doing wrong?

*NOTE - LOCAL SYSTEM gave me a Directory not found error

7
  • 2
    You said you've tried everything. Have you added a transcript to your script and logged what its doing? The problem is invariably in the script or the permissions to run or do what its doing. you souhld post the script not the task xml Commented Apr 21, 2021 at 17:59
  • No, I said I've tried everything that has been suggested. None of the suggestions included a log, but they did ask for XML. Perhaps you can tell me how to add a transcript to my script? Commented Apr 21, 2021 at 18:21
  • You haven't told us what the script does. Also, it's normal to not see the PowerShell window come up when it runs. It may run under your credentials, but not under your login session. Commented Apr 21, 2021 at 18:28
  • You may call Start-Transcript at the beginning of your script to log its actions. Commented Apr 21, 2021 at 18:33
  • Note that pseudo-accounts like Local System don't have a C:\Users\%username%\ folder, so make sure your WorkingDirectory takes that into account. Make sure that the file paths for the stuff you're uploading all work properly as well in the context of whatever user is supposed to run the script. Your task looks fine otherwise and it is running - can you add the powershell script to your question instead of the task xml? Commented Apr 21, 2021 at 18:46

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar problem. I had a task that ran a PowerShell script to copy a website folder zip and the website's database to an archive on a network share. The Task status would never come out of running, and it would not copy the files.

When I ran the script manually, as the user (via run as another user), the script worked perfectly.

When I created my task, I gave it a meaningful name and copied the parameters for the Action from another similar task. Notice I said "copied."

The text in the Edit Action > Program/script, Add arguments, and Start in boxes looked correct, but the script would not run.

I also tried copying my parameters out to Notepad++, deleting the task, creating a new task, and copying the parameters in again. The new task would not come out of running and would not copy the files either.

The solution for my issue was to NOT copy the path into the Edit Action > Program/script box. I really went manual and clicked on the Browse button, navigated to the PowerShell location on my server, and selected powershell.exe. I did copy in the Add arguments value. I also copied the newly selected path from the Program/script box to the Start in box. The offending part seemed to be only the value in the Program/script box.

When I ran the task, after manually selected the path via the Browse button, it worked as desired. Evidently, something unseen was causing the path to powershell.exe not to resolve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.