none
ps1 script doesn't full run via Task Scheduler RRS feed

  • Question

  • Hi,

    I am writing a script so that the pdf file in a specific folder will be printed, the Adobe reader close automatically and the file in the folder being removed. The script is as below

    Start-Process –FilePath 'FolderPath\*.pdf' –Verb print -PassThru | %{sleep 10;$_} | kill
    Remove-Item –path 'FolderPath\*.pdf*' -recurse

    I saved it as a ps1 file, and created a cmd file as below and save it in the same location.

    @ECHO OFF
    SET ThisScriptsDirectory=%~dp0
    SET PowerShellScriptPath=%ThisScriptsDirectory%PrintfromFolder.ps1
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";
    EXIT

    So everything works fine when i directly run the cmd file, file printed, the Adobe reader closed automatically and the file in the folder being removed. But when i run it as a scheduled task (Action > start a program and open the cmd file), the file is not printed (check the printer and no print job is received), no Adobe is launched, but the file is removed. I tried to run the task with or without highest privileges or not, using SYSTEM account, no help at all ...

    Do you have an idea?

    Thanks!

    • Moved by Bill_Stewart Tuesday, December 11, 2018 8:43 PM Not a scripting question
    Monday, July 9, 2018 5:04 AM

All replies

  • You must use full path names and the task must be run under an account that has access to the files.

    You don't need a batch file to run PowerShell scripts.  You cannot use "RunAs" in a task.

    Task Scheduler to run powershell:

    Program:    PowerShell
    Arguments:  -Command "Start-Process –FilePath c:\FolderPath\*.pdf –Verb print; Remove-Item c:\FolderPath\*.pdf"
    The issue is not a scriptin issue. It is a Task Scheduler issue so further requests should be posted in the correct forum.


    \_(ツ)_/


    • Edited by jrv Monday, July 9, 2018 5:17 AM
    Monday, July 9, 2018 5:17 AM