none
Powershell script from task scheduler partially working

    Question

  • Hi all,

    I wrote a powershell script to print out all files in a specific folder:

    cd c:\Script

    Dir -name c:\Invoices\*.* > list.txt

    ForEach ($DocId in Get-Content "C:\Script\list.txt")  
    {
    cd "C:\Program Files\SumatraPDF\"
    .\SumatraPDF.exe -print-to-default "c:\Invoices\$DocId"
    }



    sleep 30
    ri c:\Invoices\*.*
    ri c:\Script\list.txt

    This work perfectly from console every time.

    When I schedule it from task scheduler is executing the last part and completely ignoring the part that is calling the program to print.

    This is driving me crazy any suggestion why is skipping that part? I have search a lot and people speak about variables and environment but nothing seems to apply to my case.

    The task with the following parameters:

    User:System

    Action:Program/Script

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

    Add Arguemts:-File "C:\Script\PrintInvoices.ps1"


    Thanks for any suggestion.

    Federico

    Wednesday, August 3, 2016 2:42 PM

Answers

  • Windows 10 is not an option as this something for my Work and cliente are all Windows 7.

    Anyway I fixed. I made it work with a simple stupid bat using sumatra and using that in Task Scheduler instead of powershell script now is working like a charm.

    Thanks everyone for the help

    Federico

    Friday, August 5, 2016 12:04 PM

All replies

  • Hello Federico,

    just some ideas to debug this:

    • add some debugging stuff to your script and log it, e. g. is your list.txt created and filled correctly, if pdf printer has some output capture it
    • try to run your task not as system but as your user (try with logged in user and without)
    • try starting your script using powershell.exe -file "C:\Script\PrintInvoices.ps1" and see how it behaves

    Olaf

    Wednesday, August 3, 2016 3:06 PM
  • Start by removing all f the potential issues that can happen under the scheduler.  Use all explicit paths.

    $files=Get-ChildItem c:\Invoices\*.pdf 
    ForEach ($file in $files) {
    	& "C:\Program Files\SumatraPDF\SumatraPDF.exe -print-to-default c:\Invoices\$($file.FullName)"
    }
    Remove-Item c:\Invoices\*.pdf
    

    Be sure vendor of program supports silent execution.

    Be sure the task is running under an account with correct permissions.


    \_(ツ)_/

    Wednesday, August 3, 2016 3:27 PM
    Moderator
  • Hi All,

    I have tried to do most of the staff you have suggested above:

    • add some debugging stuff to your script and log it, e. g. is your list.txt created and filled correctly, if pdf printer has some output capture it

    The script at the beginning was not re moving the files so that I can be sure indeed that list.txt was populating correctly and it was.  I have tried to redirect the pdf printer output but is not generating nothing at all. This at list make me see the it was executing the command as I was trying to redirect to a file and the file was created.

    • try to run your task not as system but as your user (try with logged in user and without)

    I was running the script before with a service account with admin rights no difference this is why I change to SYSTEM but no luck and the same with the logged in and wtihout

    • try starting your script using powershell.exe -file "C:\Script\PrintInvoices.ps1" and see how it behaves

    Do you mean from console? It worked fine in case

    Jrv I tried using a similar command with double quote using explicit path but it is just throwing error about command not recgnise for string in the double quote that is why I spited it. 

    I was using Acroreader32 before and I was having the same exactly problem that is why I changed to sumatrapdf it looked easier but no luck.  I am starting thinking that is the scheduler the issue.

    Wednesday, August 3, 2016 6:47 PM
  • You must verify with the vendor that the program can run without an interactive session.  If it can't thenyu  will not be able to make this work as you are trying,

    You can try yo use Start with -verb "print"

    Start-Process d:\test\newtest.pdf -Verb print -NoNewWindow


    \_(ツ)_/

    Wednesday, August 3, 2016 7:23 PM
    Moderator
  • Hi JVR,

    I was using this at the very beginning:

    $FilesToPrint = Get-ChildItem “C:\Invoices” -Recurse -Include *.pdf

    foreach($File in $FilesToPrint)
    {
    Start-Process -FilePath $File.FullName -Verb Print  | %{ sleep 10;$_ } | kill

    }

    same problem from Task scheduler did not print from console it did.

    I am probably doing something wrong with the scheduler cannot understand what.....

    Wednesday, August 3, 2016 8:35 PM
  • Why?

    Start-Process -FilePath $File.FullName -Verb Print  | %{ sleep 10;$_ } | kill

    This is all you need:

    Start-Process -FilePath $File.FullName -Verb Print 

    Post the XML from the scheduler.


    \_(ツ)_/

    Wednesday, August 3, 2016 8:41 PM
    Moderator
  • Hi !

    In that case, i would try to the Microsoft Print to PDF driver, to see if it is Sumatra pdf printer driver that is not working properly. , ?

    Wednesday, August 3, 2016 9:08 PM
  • Microsoft Print to PDF is not for printing PDF files.  It is fro printing Office documents in PDF format.

    No program can correctly print a PDF that does not register as a print enabled object.


    \_(ツ)_/

    Wednesday, August 3, 2016 9:21 PM
    Moderator
  • Hi JRV !

    For me Microsoft Print to PDF is working very fine.

    I have many PDF books ( downloaded at Gutenberg Project ) that i open with Edge on Win 10, and then print perfectly formatted with Microsoft PDF printer . Since i have install Win 10, i have flush all others party  PDF reader  of my computer :)

    Wednesday, August 3, 2016 10:05 PM
  • So you use Edge to print to PDF?


    \_(ツ)_/

    Wednesday, August 3, 2016 10:15 PM
    Moderator
  • Microsoft Print to PDF is used to create a PDF from a document.  It is not used to print PDFs.

    If you have Adobe installed then the default print program for: "Start-Process file.pdf -verb print" will be the Adobe program.


    \_(ツ)_/

    Wednesday, August 3, 2016 10:18 PM
    Moderator
  • Hi Jrv !

    Yes, Edge works in conjunction with Microsoft Print to PDF, and these 2 tools, I can do anything, open a PDF read a PDF and print it.
    I can't edit a PDF.
    But it is really new from Microsoft:

    read this article very interresting

    These are the best ways to print-to-PDF in Windows 10

    Wednesday, August 3, 2016 10:27 PM
  • This is the registry key that lists all drivers that can print:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers

    Look for PDF printers.

    Yes - The current version of MS Print2PDF can print PDF documents.  It didn't used to be able to do that.  Office 2016 seems to have upgraded many things.


    \_(ツ)_/

    Wednesday, August 3, 2016 10:30 PM
    Moderator
  • Which PDF readers do you use on your computer ? Adobe, Sumatra, Foxit, others ?
    Wednesday, August 3, 2016 10:38 PM
  • here what i have in my registry: the Microsoft PDF printer

    Wednesday, August 3, 2016 10:42 PM
  • here what i have in my registry: the Microsoft PDF printer

    Yup.  Mine has about 4.   I never thought to check the newest MS2PDF.  That is good to know as I tended to install adobe for that.  Now I know in later Windows that the MS2PDF is available for printing.


    \_(ツ)_/

    Wednesday, August 3, 2016 11:12 PM
    Moderator
  • Hi all,

    so I have changed it to this: 

    d c:\Script

    $FilesToPrint = Get-ChildItem “C:\Invoices” -Recurse -Include *.pdf
    foreach($File in $FilesToPrint)
    {
    Start-Process -FilePath $File.FullName -Verb Print > c:\Script\Output.txt
    }

    sleep 30
    kill -name Acrobat
    ri c:\Invoices\*.*

    Still same behaviour work from console not from scheduler.

    Attached is the XML as you asked JVR.

    <?xml version="1.0" encoding="UTF-16"?>
    <Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
      <RegistrationInfo>
        <Date>2016-08-03T10:25:42.8760444</Date>
        <Author>WESTFIELD_UK\svc_tap</Author>
      </RegistrationInfo>
      <Triggers>
        <TimeTrigger>
          <Repetition>
            <Interval>PT3M</Interval>
            <StopAtDurationEnd>false</StopAtDurationEnd>
          </Repetition>
          <StartBoundary>2016-08-03T10:09:35.8233313</StartBoundary>
          <Enabled>true</Enabled>
        </TimeTrigger>
      </Triggers>
      <Principals>
        <Principal id="Author">
          <UserId>S-1-5-18</UserId>
          <RunLevel>HighestAvailable</RunLevel>
        </Principal>
      </Principals>
      <Settings>
        <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
        <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
        <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
        <AllowHardTerminate>true</AllowHardTerminate>
        <StartWhenAvailable>false</StartWhenAvailable>
        <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
        <IdleSettings>
          <StopOnIdleEnd>true</StopOnIdleEnd>
          <RestartOnIdle>false</RestartOnIdle>
        </IdleSettings>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <Enabled>true</Enabled>
        <Hidden>false</Hidden>
        <RunOnlyIfIdle>false</RunOnlyIfIdle>
        <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
        <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
        <WakeToRun>false</WakeToRun>
        <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
        <Priority>7</Priority>
      </Settings>
      <Actions Context="Author">
        <Exec>
          <Command>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe</Command>
          <Arguments>-File "C:\Script\PrintInvoices.ps1"</Arguments>
        </Exec>
      </Actions>
    </Task>




    Thursday, August 4, 2016 9:01 AM
  • What is this supposed to do?

    sleep 30
    kill -name Acrobat

    What is this supposed to do?

     > c:\Script\Output.txt

    You should set the startup directory to "c:\script"


    \_(ツ)_/

    Thursday, August 4, 2016 9:35 AM
    Moderator
  • Here is how I test my tasks:

     <Actions Context="Author">
        <Exec>
          <Command>powershell</Command>
          <Arguments>-NoExit -File TestPrint.ps1</Arguments>
          <WorkingDirectory>d:\scripts</WorkingDirectory>
        </Exec>
      </Actions>
    
    

    Note the "-NoExit".  This leaves PowerShell visible so I can see any errors and also I can analyze the error stack.

    I always set a working directory no matter what the scrip will do.  I also use explicit paths inside the script.


    \_(ツ)_/

    Thursday, August 4, 2016 9:45 AM
    Moderator
  • Hi Jvr,

    that was to see if I could catch any error but I do not think it would work.

    Sleep 30 I put 30 seconds wait because the process was a bit slow and if I have multiple files when it gets to this:

    ri c:\Invoices\*.*

    It was telling me that the last file in the print queue was use by another process and  could not deleted.

    Using this command:

    Start-Process -FilePath $File.FullName -Verb Print 

    it open acrobat reader and so the kill is to end the process. Is the -NoNewWindow parameter you quote above to avoid this?

    Tried to set up start folder to "c:\script" no difference.

    Federico

    Thursday, August 4, 2016 9:50 AM
  • Hi,

    where am I suppose to see the error stack. Event viewer?

    using the parameter above the task never end now,

    Federico

    Thursday, August 4, 2016 10:07 AM
  • The issue is that no error is occurring.  AcroReader is not compatible under the Task Scheduler.  It cannot be run blind.  I went through the info at Adobe.

    To automatically print to Adobe you must have the full copy a Acrobat and the jobber extension.  The free version is purposely crippled.

    You can write a print routine that uses the Microsoft PDF printer.

    I ran the code.  It loops through all calls to adobe with no error but adobe won't actually run.

    If you look in task manager you will likely see hundreds of copies of Adobe.

    Get-Process Acro*


    \_(ツ)_/

    Thursday, August 4, 2016 11:15 AM
    Moderator
  • HI,

    ok so is an adobat problem that will not run through the scheduler? what do you mean by blind? Because if you mean no intervetion if i run the script from cmd it works fine and that look blind to me in case.

    I am kind of confuse. 

    Cannot see hundred of acrobat anyway but I was seeing this beahaviour when I was using sumatrapdf.

    Regards

    Federico

    Thursday, August 4, 2016 12:25 PM
  • By "blind" I mean no GUI.  When Adobe loads it cannot run with no Window.  This is by design for the free reader.  Contact Adobe for how to obtain one of the products they have that can do batch printing.


    \_(ツ)_/

    Thursday, August 4, 2016 12:33 PM
    Moderator
  • Hi,

    I cannot find microsoft PDF printer on windows 7 and does not seem to be available for download.

    I will have to find a different way to do it.

    Thursday, August 4, 2016 1:10 PM
    • Edited by YLed Thursday, August 4, 2016 2:45 PM
    Thursday, August 4, 2016 2:43 PM
  • Hi,

    already saw it the title is quite misleading it speaks about converting not printing to a physical printer.

    Windows 10 is the only one with Microsoft Printer PDF. They speak in the article about a free converter called dopdf which does not allow you to print.

    I am wondering why sumatrapdf does not work either. Does not look like it needs a GUI.

    Federico

    Thursday, August 4, 2016 7:14 PM
  • Hi,

    already saw it the title is quite misleading it speaks about converting not printing to a physical printer.

    Windows 10 is the only one with Microsoft Printer PDF. They speak in the article about a free converter called dopdf which does not allow you to print.

    I am wondering why sumatrapdf does not work either. Does not look like it needs a GUI.

    Federico


    Contact the vendor.

    \_(ツ)_/

    Thursday, August 4, 2016 7:38 PM
    Moderator
  • @federico

    If you’re using Windows 10, you’re in luck, because they finally included a print to PDF feature natively into the operating system. So you can just choose File -> Print from any application, and then print to the “Microsoft Print to PDF” option as your printer

    so why not you go to Windows 10 ?  is great, many things new, Edge, Pdf Printer, Groove music, and many mores features, it was FREE for one year, so maybe if the date is not expired you could install it for free ! :)


    • Edited by YLed Thursday, August 4, 2016 7:56 PM
    Thursday, August 4, 2016 7:50 PM
  • so why not you go to Windows 10 ?  is great, many things new, Edge, Pdf Printer, Groove music, and many mores features, it was FREE for one year, so maybe if the date is not expired you could install it for free ! :)
    Ended July 29th. There are still methods to upgrade for free (dunno why you'd want to, I'm not impressed), but the actual promotion itself has already ended.

    Friday, August 5, 2016 11:56 AM
    Moderator
  • Windows 10 is not an option as this something for my Work and cliente are all Windows 7.

    Anyway I fixed. I made it work with a simple stupid bat using sumatra and using that in Task Scheduler instead of powershell script now is working like a charm.

    Thanks everyone for the help

    Federico

    Friday, August 5, 2016 12:04 PM
  • Hi Federico,

    you are saying that it got fixed by putting the powershell script in bat file?

    and could you please tell me what is sumatra

    Thursday, July 12, 2018 8:57 AM