Decula
Decula

Reputation: 508

Python script could not access network drive in scheduled task but could run manually

I have a simple Python script to scan a network drive and copy file out. The script could copy file in IDE. However, if I add the script into task scheduler, it will show the network drive not accessible. Code snippet

os.system("net use z: /d /Y")
os.system("net use z: \\server\folder password /user:user_name /p:yes")
network_drive=r"\\server\folder"
if not os.path.exists(network_drive):
    print("Drive  "+ network_drive+" is not available")
else:
    #copy files

Manually run the code, it could copy. Ran in task scheduler, it will print info. I add network mapping lines from this link. Seems not working for me. How could I access network drive in my python code? In task scheduler, I already selected "Run whether user is logged on or not" and "Run with highest privileges".

EDIT: The real issue is net use doesn't mount the folder correctly. I check the output of os.system and get below error

System error 1219 has occurred Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.

Combine the solution from here , delete all mounts and restart the sever resolve issue for me. Please let me know if there is solution without restart server.

Upvotes: 1

Views: 2720

Answers (1)

thx1138v2
thx1138v2

Reputation: 576

There was a major overhaul of Scheduled Tasks security in Vista and later to prevent hackers from installing a scheduled task that could access network resources.

When a user is not logged on, task scheduler uses “Service-for-User” (S4U) authentication which denies the user access to any network functionality. This applies to both mapped drives and using UNC file specs.

"Run with Highest privileges" does not grant higher privileges to the specified user but runs under a completely separate security token for the system Administrator account that is created when Windows is installed.

https://technet.microsoft.com/en-us/library/cc722152(v=ws.11).aspx

https://technet.microsoft.com/en-us/library/cc732613(v=ws.10).aspx

https://technet.microsoft.com/en-us/library/ee844140(v=ws.10).aspx

The only way I got access to networked drives using either a mapped drive or a UNC spec was to leave the machine on and the user logged in.

Upvotes: 3

Related Questions