Skip navigation

During a recent migration from Windows 2003 to 2008, and subsequent updating of various scheduled scripts, I came across an issue with my scripts not functioning when executed through the task scheduler. The problem, as it turns out, is how Windows 2008 task scheduler handles mapped drives. I don’t know exactly what was changed between 2003 and 2008 in this respect, but the fact is 2008 requires any scripts that need mapped drive access, self mount the desired path.

Here’s an example of the code I added to my Python scripts to get them to be able to run as a scheduled task using mapped drives.

#this module is included in the pywin32 package
import win32wnet
from win32netcon import RESOURCETYPE_DISK as DISK

#drive you want to map to
local_dir = "Z:"          
drive_path = "\\\\network-server\\shared-folder-of-interest"

#mount the mapped drive
win32wnet.WNetAddConnection2(DISK,local_dir,drive_path)

#insert code here to do whatever using mapped drive

#unmount the mapped drive (if you don't do this, the mapped drive will be persistent
#causing this script to hang next time it is scheduled to run again
win32wnet.WNetCancelConnection2(local_dir, 0, 0)

2 Comments

  1. Your post is the #2 link on Google for “task scheduler mapped drive” I had this same problem and luckily I was trying to run a Python script as well, I used your code to fix it, thank you very much!

  2. Hi, I got it to running without mounting the mapped drive and giving the dns path (drive path in your script.)


Leave a comment