tags:

views:

75

answers:

1

I'm able to map the drive without problems on network shares without authentication. But I'm missing something once I try to authenticate with a username and password. Here is the current working example of the code with the error message I keep receiving.

#!/usr/bin/python
# Drive Map Script
import pywintypes
import win32com.client

# Disconnect previous mapped drives
testnetwork = win32com.client.Dispatch('Wscript.Network')
network_drives = testnetwork.EnumNetworkDrives()
for mapped_drive in [network_drives.Item(i)
                     for i in range(0, network_drives.Count() -1 , 2)
                     if network_drives.Item(i)]:
    testnetwork.RemoveNetworkDrive(mapped_drive, True, True)

# Mount the drives
drive_mapping = [
    ('z:', '\\\\192.168.1.100\\Some_Share', 'someuser', 'somepass')]

for drive_letter, network_path, user_name, user_pass in drive_mapping:
    try:
        testnetwork.MapNetworkDrive(drive_letter, network_path)
    except Exception, err:
        print err

And the error the code generates upon execution:

(-2147352567, 'Exception occurred.', (0, u'WSHNetwork.MapNetworkDrive', u'Logon failure: unknown user name or bad password.\r\n', None, 0, -2147023570), None)

A: 

You aren't passing user_name and user_pass to MapNetworkDrive. Try this instead:

testnetwork.MapNetworkDrive(drive_letter, network_path, True, user_name, user_pass)

Note: the True passed there is a flag that indicates whether the mapping information is stored in the current user's profile.

Idan K
That was it. I knew it was something simple. Thank you for the solution!
Dunwitch