Automation

 View Only
Expand all | Collapse all

Powershell scripts not running in scheduled tasks

  • 1.  Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 11:24 AM

    I am having problems running vmware powershell scripts through scheduled tasks, then run fine when I run then manually. Below is the script below I use in scheduled tasks:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ ” & “E:\scripts\DataStoreSpaceCheck1.ps1″

    When I copy and paste this in a normal windows command line and then run it I get the following error:

    Windows powershell console file ""C:\program" extension is not psc1. Windows powershell console file extension must be psc1. The filename, directory name, or volume label syntax is incorrect.

    Can anyone please advise what might be wrong within scheduled tasks



  • 2.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 11:33 AM

    Did you check Alan's excellent article Running a PowerCLI Scheduled task



  • 3.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 11:45 AM

    yes I did and I copied and paste the script, but still get the same error message as mentioned in the previous post



  • 4.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 12:19 PM

    Can you try something along these lines

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -file "D:\Userdata\Scripts\MyScript.ps1" -noconsole -noninteractive
    



  • 5.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 01:10 PM

    I copied the script into a command console first and now it runs fine, does not bring up the error. But when I setup a schedule task, and copy the text into the task and then run it in schedule task, it gives no output, its like it does not run, even though the status is saying running which stays running, but nothing eventually happens.

    The only thing I can think which maybe causing the issue might when i ran the script in the command line or when i run the script manually it prompts me with a security warning, and then I have to choose if I do not want to run, or run once, or suspend. I choose R to run once, and then the script runs.

    Would this security warning cause the script not to run automatically, would I need to put something in the script to overcome this security warning.

    Any advice thanks



  • 6.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 01:26 PM

    You could do the following as a debugging step.

    Add this to the top of your script

    Start-Transcript "C:\scheduled_task.log"
    Get-ExecutionPolicy
    

    And at the end of your script add

    Stop-Transcript
    



  • 7.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 01:40 PM

    I put the above commands in the script before and after. I put it in the scheduletask run command. WHen I run the script I get the error message could not start. Is there something I am doing wrong



  • 8.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:19 PM

    Is there a way to bypass or stop the security warning, as that might be stopping the powershell script from running in scheduled tasks.

    My execution policy is set to unrestricted, but still the security warning appears



  • 9.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:22 PM

    the script you are running , is it on a local drive or a mapped drive /UNC?

    your batch file should look like this:

    reg import "c:\wherever\policy_posh.reg"
    reg import "c:\wherever\policy_ie.reg"
    %systemroot%\system32\windowspowershell\v1.0\powershell.exe  -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -Command ". 'D:\Userdata\Scripts\MyScript.ps1'"  -NoLogo -NoProfile -Noninteractive 
    



  • 10.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:25 PM

    Its on the local drive.

    Thanks



  • 11.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:27 PM

    You shouldn't be getting a run once prompt then...

    What does Get-ExecutionPolicy say when you run in console as the same user as the sched task?



  • 12.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:30 PM

    Its on my local drive and i get the run once command, do not understand why



  • 13.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:28 PM

    Which security warnings do you mean ?

    Can you run the command from the CMD prompt ?



  • 14.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:32 PM

    The security warning is the one that gives you the option not to run or run once or suspend.

    I get this message when I run the script in windows command line as well.



  • 15.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:35 PM

    What OS are you running this on? Are you running CMD or POWERSHELL with the UAC admin tokeN (aka. 'run as...')?



  • 16.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:39 PM

    I am running this on windows 2003 server sp2. I have tried the script on cmd and in powershell and the VI toolkiit as well. Sorry I did not know what you mean by UAC admin token.



  • 17.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:41 PM

    Nevermind, UAC does not exist in 2003. What does Get-ExecutionPolicy return? let me check my server.



  • 18.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:45 PM

    Do you get the prompt when you just run powershell.exe (w/o script parameter)?

    try

    powershell.exe

    and

    powershell.exe -noprofile

    any difference?



  • 19.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 02, 2009 04:21 PM

    Put this in a batch file and schedule a task for a batch file:

    %systemroot%\system32\windowspowershell\v1.0\powershell.exe  -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -Command ". 'D:\Userdata\Scripts\MyScript.ps1'"  -NoLogo -NoProfile -Noninteractive 
    

    You might also need to add this to the beginning of your script, which would replace your '-PSConsoleFile' parameter...

    $VMCore = Get-PSSnapin VMware.VimAutomation.Core -EA 0
    if ( -not $VMCore ) { Add-PSSnapin VMware.VimAutomation.Core };
    



  • 20.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 06:18 AM

    try c:\Progra~1 instead of c:\program files



  • 21.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 09:17 AM

    I used the below script, but still I get the same security warning:

    systemroot%\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -Command ". 'D:\Userdata\Scripts\MyScript.ps1'" -NoLogo -NoProfile -Noninteractive

    Also I ran powershell and powershell -noprofile from the windows command line and there was no difference.

    If i run get-executionpolicy, I get unrestrcited.

    I assuming its the security warning which is not allowing the script to run under scheduled tasks.

    Any advice please. Thanks



  • 22.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 09:25 AM

    Powershell.exe > Set-ExecutionPolicy RemoteSigned



  • 23.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 09:25 AM

    Would it be possible to show us (text or screenshot) the complete security message you are getting ?

    Stupid question perhaps, but are you running the scheduled task with the same account as the one used for the interactive runs of the script ?



  • 24.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 09:36 AM

    I have attached a screenshot of the security warning message i get.

    I have tried remotesigned, but this does not allow me to run the script altogther



  • 25.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 01:57 PM

    For some reason powershell sees E: drive as a remote drive. Can you copy the script to C: and run it again?



  • 26.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 02:35 PM

    looks like you downloaded the script file from other computer/internet/email etc.

    I got similar issue a while ago, and i copy pasted all script text and created a new .ps1 file and pasted there. Thereafter internet script warning disappeared.



  • 27.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 03, 2009 03:55 PM

    That's crazy. But wait I remember reading about this. There is some file property, could be alternate data stream, that actually flags whether the file is a local, Intranet or Internet file....



  • 28.  RE: Powershell scripts not running in scheduled tasks



  • 29.  RE: Powershell scripts not running in scheduled tasks

    Posted Dec 04, 2009 12:20 PM

    cool