Powershell Scheduled Task RRS feed

  • Question

  • Hi,

    I've been having some trouble with a Powershell scheduled task that is beginning to drive me mental. Hoping someone can pinpoint my issue.

    The setup:

    - Powershell script scheduled task is pushed out with Group Policy.

    - References a script file via UNC path.

    - Execution Policy was remote signed, set to unrestricted for troubleshooting.

    - Running as a domain admin service account (I'm logged in with my own admin credentials).

    The script runs fine from the cmd prompt under my login using "powershell -command "& \\servername\script.ps1 > C:\log.txt"

    When I attempt to run the scheduled task with the service account, it just stays at "Running" (0x41301) unless I manually stop it. No text file is written at all so it's not even getting to the script.

    I've set UNCasIntranet = 1 on a server to test, and have added \\servername to the trusted sites list with no luck.

    I have a feeling that these two previous settings are user based settings though, so they're only applying to my log on account, but the only way I've gotten the trusted sites list to be picked up by the service account is if I log into the server with the service account credentials, which completely ruins the purpose of using a service account in the first place.

    I've successfully gotten this working in a dev/test environment, with the only noticable difference between the two environments being that UAC is turned ON in the working environment and off in the other.

    Any ideas would be greatly appreciated.



    Monday, July 16, 2012 12:20 AM

All replies

  • You should post the exact command from the scheduled task that you are executing to get some more help.  You might also want to try running that exact command outside of the scheduled task (launch directly from a PowerShell instance) and share the output you get in that window.

    As far as the execution policy is concerned, you can confirm what the local machine policy is by running:

    Get-ExecutionPolicy -List

    You will need to be sure that one of the higher order scopes are not overriding that policy.  It sounds like this isn't the issue though based on the fact that the job states "Running" because it would typically stop immediately if the execution of scripts was disabled in some way.

    Monday, July 16, 2012 6:22 AM
  • As mentioned in my first post, the exact command I'm running in the scheduled task is: powershell.exe -command "& \\servername\script.ps1 > C:\log.txt"

    Command runs fine from a prompt logged in as any administrator.

    I've confirmed unrestricted is the only execution policy set via Get-ExecutionPolicy -List.

    I've also confirmed that if I log in with the service account credentials, I can then log out, log back in as any admin and run the task successfully.

    Presuming this is because of some Group Policy (either UNCasIntranet or Trusted Sites) that won't come into effect until the user has a local profile.

    Any ideas?

    Monday, July 16, 2012 6:40 AM
  • Is the service account (i.e. the one whose credentials are used by the scheduled task) able to access the UNC to the script?

    By the way, the UNC \\servername\script.ps1 seems to be missing a path component, i.e. \\servername\share\script.ps1.

    Al Dunbar

    Monday, July 16, 2012 2:01 PM
  • As mentioned above, if I log in as the service account then log out, the script will run as a scheduled task. It will not run unless the service account has logged in to the machine beforehand.

    The path to the script is actually \\servername\scripts\scriptname.ps1.

    I know what the issue is, I just don't understand why I need to have the service account login first.

    Tuesday, July 17, 2012 12:39 AM