none
PowerShell script not running as Scheduled Task Server 2008 RRS feed

  • Question

  • I have a PowerShell script I wrote that does some basic cleanup deleting hourly backups older than [x] days. The script works, and can be run locally, or remotely via a PowerShell console. However, I have a Scheduled Task on Windows Server 2008 that reports the task completes successfully yet if I run the script manually after the task executes it finds and deletes backups (meaning the scheduled task isn't actually working). The task runs as user account, 'ServiceAccount'. I can open a command prompt impersonating 'ServiceAccount' and run "powershell -nologo -noninteractive -commadn "&{G:\Backups\DeleteOldBackups.ps1}" and the command executes successfully so I dont think it's a permission issue; (I've tried using an Admin account just to see if it made a difference, nope).

    Get-ExecutionPolicy = Unrestricted

    • Actions: Start a program
    • Program/script: powershell
    • Add Arguments: -nologo -noninteractive -command “&{G:\Backups\DeleteOldBackups.ps1}”
    • Start In: C:\Windows\System32\WindowsPowerShell\v1.0\

     

    Wednesday, February 9, 2011 4:34 PM

Answers

  • I ran into the same issue the other day and if you read very closely the powershell /? text for command you need to do the following

    -command & ("{ <command to run> }")

     

    I was running another person's batch file and it did show succes but work not work until I put it in this format.

    Hope that works,


    Allan
    • Marked as answer by David.Mathis Monday, February 14, 2011 10:54 PM
    Monday, February 14, 2011 10:33 PM

All replies

  • Server 2008 32 or 64-bit?

    See also: http://dmitrysotnikov.wordpress.com/2011/02/03/how-to-schedule-a-powershell-script/

    -File only comes with PowerShell v2 though, which is not included by default with Server 2008, but you can load it though.

    Saturday, February 12, 2011 3:06 AM
    Moderator
  • Sorry, I didn't specify this is Server 2008 R2, which only comes in x64 (I believe).

    Your link appears to have it scheduled exactly as I do... I could try and specify the ExecPolicy in the task parameters, but I really don't think that is the issue because it's set to 'unrestricted' on the machine... 

    Monday, February 14, 2011 6:55 PM
  • I ran into the same issue the other day and if you read very closely the powershell /? text for command you need to do the following

    -command & ("{ <command to run> }")

     

    I was running another person's batch file and it did show succes but work not work until I put it in this format.

    Hope that works,


    Allan
    • Marked as answer by David.Mathis Monday, February 14, 2011 10:54 PM
    Monday, February 14, 2011 10:33 PM
  • Thanks dude, I think that worked! That's crazy... I have several servers (some '03 and some '08) that all don't have any problems with running powershell tasks without using those parens, this was the first for me! Anyway, thanks again!
    Monday, February 14, 2011 10:55 PM
  • Hi Guys, If you don't have any error messages and don't know what the problem is - why PowerShell scripts don't want to start from a Scheduled Task do the following steps to get the answer:

    1. Run CMD as a user who has been set for Scheduled Task to execute the PowerShell script 
    2. Browse to the folder where the PowerShell script is located
    3. Execute the PowerShell script (remove all statements that block the error notifications if any exists inside of the script like $ErrorActionPreference= 'silentlycontinue')

    You should be able to see all error notifications.

    In case of one of my script it was:

    "Unable to find type [System.ServiceProcess.ServiceController]. Make sure that the assembly that contains this type is loaded."

    And in this case I have to add additional line at the begining of the script to load the missing assembly:

    Add-Type -AssemblyName "System.ServiceProcess"

    And next errors:

    Exception calling "GetServices" with "1" argument(s): "Cannot open Service Control Manager on computer ''. This operation might require other privileges."

    select : The property cannot be processed because the property "Database Name" already exists



    Friday, September 7, 2018 10:09 AM
  • Thank you so much, this here really solved my issue, the script worked Ok on the command line but would not run on the scheduler but after doing you did above then it solved my issue.my example is like this below on the argument 

    -command &("{ D:\test\scripts_folder\test.ps1 parameter1 parameter2 'script did run OK'}")

    Thank you

    Wednesday, April 3, 2019 7:38 PM