PowerShell Script Not Running on Scheduled Task

13,355

Solution 1

Finally figured this one out:

The problem turned out to be the fact that I was attempting to manipulate something on a network drive. What I learned is that, even if you already have the drive mapped in Windows, you MUST un-map and re-map the drive as part of your script in order to manipulate files non-interactively through a PowerShell Script.

Here is what I added to my PowerShell script to get it to work:

$secpasswd = ConvertTo-SecureString "YourPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)

Remove-PSDrive –Name “Z”

New-PSDrive –Name “Z” –PSProvider FileSystem –Root “\\192.168.X.X\backup” –Persist -Credential $mycreds

There is something with credentials being cached that happens when you are logged on but is not used when you are running the same script via task manager.

Thanks a ton for all the answers I got to this question, it really helped me to finally figure this one out hopefully this will help someone else out there too.

Final note, this same method worked for batch files as well on the same server.

Solution 2

Can you try running with following:

powershell -noprofile -noexit -executionpolicy bypass -file C:\path\script.ps1

Also check that: get-executionpolicy is set to RemoteSigned

Also check "Run whether user is logged on or not.

Share:
13,355

Related videos on Youtube

Tyson Navarre
Author by

Tyson Navarre

Just another IT guy@tynavarre

Updated on September 18, 2022

Comments

  • Tyson Navarre
    Tyson Navarre over 1 year

    I have been attempting to set up a PowerShell script to run as part of a scheduled task on a Windows Server 2012 R2 server. The script will run if I execute it manually from the GUI but all but one of my attempts to run it from a task have failed.

    After some troubleshooting I found that if I set the task to "Run only when the user is logged on" and I execute the task, it runs just fine. It pops a powershell window, runs the script, and closes itself. It seems like I am on the right track and I may have narrowed it down as to what is the problem but I am at a loss as to where to go from here. Obviously, I would like the script to execute regardless of whether or not I am logged in.

    Just in case, here is the script I am running:

        Get-ChildItem C:\somefolder -Recurse| 
        Where-Object{$_.PsIsContainer}| Sort-Object CreationTime -desc| 
        Select-Object -Skip 3| Remove-Item -Force
    

    Where can I look from here? I have been able to run powershell scripts in this manner from server 2008 without problems but I just cannot seem to crack this one.

    EDIT: For the action of the task set to start a program and under Program/script I have "powershell" then ".\myscript.ps1" for an arguement then "c:\" for start in.

    • Zoredache
      Zoredache about 9 years
      Is it really ".\myscript"? Have you tried providing the fully qualified path the the script? You might also want to pass the ExecutionPolicy policy option.
    • Tyson Navarre
      Tyson Navarre about 9 years
      whoops nope, I mistyped that its ".\myscript.ps1" my bad. Will edit the original question to correct
    • Hyppy
      Hyppy about 9 years
      ExecutionPolicy is definitely your culprit
    • Tyson Navarre
      Tyson Navarre about 9 years
      Even if I have already set the executionpolicy to Unrestriced?
    • Tyson Navarre
      Tyson Navarre about 9 years
      also, Zoredache, I have tried a ton of different ways to express the path including fully qualified but nothing seems to work. Googled the living heck out of this and that seems to fix it for a lot of people
    • Tyson Navarre
      Tyson Navarre about 9 years
      Hyppy, if I have already set the execution policy previously, how can I alter the task or the script itself to get around it?
    • mortenya
      mortenya about 9 years
      if you just try to run the script as the user that your scheduled task runs as, does it work? For my Scheduled Tasks, I run as SYSTEM (since it's just doing local filesystem stuff) Start a program: powershell then Arguments: -ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden C:\Path\To\Script.ps1
    • mortenya
      mortenya about 9 years
      so, the script just sits in the root of the C:\ drive?
    • Lior Abel
      Lior Abel about 9 years
      Silly question really but just making sure... Did you set up credentials for running the task without the 'logged on user' option?
    • Tyson Navarre
      Tyson Navarre about 9 years
      not a bad question at all. I have tried using a local admin account on the server, and even running the task under the SYSTEM account and its the same result every time. When using the local admin account I am prompted to enter the account password every time i save the task.
    • Tyson Navarre
      Tyson Navarre about 9 years
      mortenya, I had it at the root of C but have since tried running it under a sub folder on the C drive and even on another partition and nothing has worked
  • Tyson Navarre
    Tyson Navarre about 9 years
    Thanks! if I have set the executionpolicy to Unrestricted, will it still work?
  • Ankh2054
    Ankh2054 about 9 years
    Try RemoteSigned first
  • Tyson Navarre
    Tyson Navarre about 9 years
    Update, tried running the script in the way you suggested and the task just runs forever, but nothing happens still. Before I would run the task, nothing would happen but the task would show as completed even though it did not actually appear to run. Weird stuff.
  • Ankh2054
    Ankh2054 about 9 years
    Try removing the noexit
  • Tyson Navarre
    Tyson Navarre about 9 years
    Took the no exit out and set the execution policy to remotesigned and got the same result. Man this makes me wanna bang my head on the wall.
  • Ankh2054
    Ankh2054 about 9 years
    Can you try running powershell as admin and then setting the execution policy again Or run get-execution policy just to check it is set to RemoteSigned
  • Ankh2054
    Ankh2054 about 9 years
    Then run the task as you had it
  • Ankh2054
    Ankh2054 about 9 years
    Could the script just be taking a while? Or does it usually finish quite quickly?
  • Tyson Navarre
    Tyson Navarre about 9 years
    just re-did the executionpolicy command then re-ran with the same result. Normally when I run the script manually it runs really fast.
  • Davidw
    Davidw about 9 years
    Certain folders at the root of the C: Drive, system32, for example, need elevated privileges to access. You would need to set the script to run with highest privileges in Task Scheduler.
  • Ankh2054
    Ankh2054 about 9 years
    Sorry just to check are you running set-executionpolicy remotesigned!
  • Ankh2054
    Ankh2054 about 9 years
    Oops set-executionpolicy remotesigned
  • Tyson Navarre
    Tyson Navarre about 9 years
    Ankh, you are right, that is the command I ran to set the policy.
  • Tyson Navarre
    Tyson Navarre about 9 years
    Davidw: I just created another folder at C called Scripts and put the script in there to see if that helped. The task has been set to run with highest privileges from the start. After attempting to run again I had the same result
  • Ankh2054
    Ankh2054 about 9 years
    Can you try David's suggestion. Go to the General Tab and select the checkbox of ” Run with highest privileges"
  • Ankh2054
    Ankh2054 about 9 years
    Anything in event logs that is useful as error?
  • Tyson Navarre
    Tyson Navarre about 9 years
    Yeah this is driving me crazy! Looked in the logs and nothing specifically mentioning powershell in there. The check-box for highest privileges has always been checked on this task but has had no affect. The only way I have gotten it to run is switching from running if logged off to run only if logged on
  • Davidw
    Davidw about 9 years
    You know, I've never had any luck running anything with "Run whether user is logged on or not" checked.
  • Davidw
    Davidw about 9 years
    This article seems to suggest that using that option means it runs completely in the background. You might try having it create a log file, and then check for that log file? social.technet.microsoft.com/Forums/windows/en-US/…
  • Ankh2054
    Ankh2054 about 9 years
    Hi Tyson, I have created your script on my server 2012 and it does run. The only observations I have are: The script says running, until I refresh, but when you look in history you can see it completed successfully.
  • Ankh2054
    Ankh2054 about 9 years
    What does it say in your history?
  • Tony Hinkle
    Tony Hinkle about 9 years
    For the user credentials you are entering, has that user logged into that server before? If that user account has not logged on, there won't be a profile and that might be causing it to hang. Just a wild guess here--I've seen some things trip up on that before, but haven't tested this scenario with it.
  • Ankh2054
    Ankh2054 about 9 years
    Try and make a very simple powershell script that actually outputs to file and see if that works.