Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the below script. Basically, it loops through files in a specific directory, prints it, and then moves it to another directory.

I have a .bat file that executes this script and does exactly as needed when executed manually.

However, when I setup a Task Scheduler to run the .bat file, it runs the script exactly as needed except doing the actual printing. It sets the default printer, moves the files, and then at the end, sets the printer to another default.

I have set the user account to be a network administrator, it has local administrator rights on the client computer. In the schedule, I have highest permission enabled.

Any ideas on what to check are greatly appreciated!!

Python
# Import libraries
import os
import time
import shutil
import win32print

# Insert the directory path in here
path = r'\\tsl-files\Share\_PostingJournal\Queue'
auditpath = r'\\tsl-files\Share\_PostingJournal\Log'
# Extracting all the contents in the directory corresponding to path
l_files = os.listdir(path)

# Iterating over all the files
for file in l_files:

# Instantiating the path of the file
	file_path = f'{path}\\{file}'
	win32print.SetDefaultPrinterW("LANIER MP 4055")

	# Checking whether the given file is a directory or not
	if os.path.isfile(file_path):
		try:
			# Printing the file pertaining to file_path
			os.startfile(file_path, 'print')
			print(f'Printing {file}')
			

			# Sleeping the program for 5 seconds so as to account the
	# steady processing of the print operation.
			time.sleep(5)
			shutil.move(f'{path}\\{file}', f'{auditpath}\\{file}')
		except:
			# Catching if any error occurs and alerting the user
			print(f'ALERT: {file} could not be printed! Please check\
			the associated softwares, or the file type.')
	else:
		print(f'ALERT: {file} is not a file, so can not be printed!')
win32print.SetDefaultPrinterW("Generic / Text Only")		
print('Task finished!')


What I have tried:

Since the file location specified in the script is a network location, I duplicated my script and set up a new task scheduler to execute directly from a folder on the C Drive. It does the same exact thing. I was thinking that maybe since the bat file is being opened on the network location that maybe it was trying to start the pdf adobe reader from that location instead? But attempting locally as mentioned debunked that.

I'm not sure what else to test. Any ideas are greatly appreciated!!

Python 3.12.0
Windows 10 Pro
Posted
Updated 6-Oct-23 10:17am
v3

I can't remember if the NetworkService (there's no such thing as "Network Admin" account) has access to printers, but I don't think it does.

Create a normal user account for the purpose of running your task, giving it permissions to the file system as required and that account will have access to the printers. Setup the task to use this account instead. Just remember to turn off "password expires" on the account.

NetworkService is a machine account with minimal permissions and no password, so you don't want to use it unless you absolutely need to.
 
Share this answer
 
Comments
Cody O'Meara 5-Oct-23 19:08pm    
Hello! Thank you for some troubleshooting steps. I should of clarified. This is a normal user account in the domain, it is just associated with our Administrators group which has Full control over the network location, as well as printers, at least it should.

This might help. I just removed the "print" operation from my method (os.startfile(file_path)) so it should just open the file in adobe. Same thing, work fine when running the .bat file manually - opened in adobe as expected, but doesn't with the Task Schedule. Odd...
Dave Kreskowiak 5-Oct-23 19:11pm    
Jobs when run under the task schedular are run under a different desktop, which any logged in users cannot see, unless you tell it to interact with the logged in user desktop. The problem with checking that box is your job will no longer work if someone isn't logged into the machine at the time the job runs.

Has the printer been setup in the user account you created?

Cody O'Meara 6-Oct-23 16:09pm    
Only one user is logged into the computer and is on almost up 100% of the time. This computer is used only for automated processes but first time using python. I may just end up making my own service that runs in the background instead of using Task Scheduler. See below link with screenshot of potentially helpful info, maybe something will stick out that you will notice. Thanks again for helping!!

https://gyazo.com/c9978a98860c18e762fca9303cc4bd34
Cody O'Meara 10-Oct-23 10:36am    
Hey Dave,
I finally understood what you were saying and resolved this. Switching to "Run only when user is logged on" fixed it. Thanks!
Having "Run whether the user is logged on or not" was the problem. Switching to "Run only when user is logged on" fixed the issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900