在Windows 10中以编程方式运行python map驱动器

时间:2017-06-06 18:04:27

标签: python windows

我试图在Windows 10上映射远程路径几个小时,但我不能让它工作。起初我尝试使用WNetAddConnection2,但无论我使用哪种凭据或标记,当我键入net use时,映射的驱动器始终处于状态not available

手动我可以毫无问题地映射驱动器,当我以编程方式映射驱动器时,我只会遇到问题。

import win32wnet
import win32netcon

nr = win32wnet.NETRESOURCE()
nr.dwScope = win32netcon.RESOURCE_GLOBALNET
nr.dwType = win32netcon.RESOURCETYPE_DISK
nr.dwUsage = win32netcon.RESOURCEUSAGE_CONNECTABLE
nr.lpLocalName = 'Z:'
nr.lpRemoteName = '\\\\192.168.178.46\\Test'

win32wnet.WNetAddConnection2(nr, None, None, 25)

25是interactiveprompt的标志集。我没有收到任何错误,当我输入net use时列出了驱动器,但状态始终为not available,并且驱动器在工作站下不可见。

之后我尝试了NetUseAdd

import win32net
win32net.NetUseAdd(None, 3, {'remote': r'\\192.168.178.46\Test',
    'local': 'Z:', 'username': 'Admin', 'password': '123',
    'status': 0, 'flags': 1, 'asg_type': 0})

它成功运行,但net use没有列出任何内容,工作站下也没有可见的映射驱动器。

没有子进程的解决方案会很好。有人可以帮忙吗?

编辑:现在我理解为什么它不起作用。该应用程序在管理员环境中运行,我当前以非管理员身份登录。此行为在此处得到了解释:https://superuser.com/questions/495370/why-isnt-a-mapped-drive-available-under-an-elevated-cmd-prompt-but-is-under-a-r

是否可以以管理员身份运行应用程序,但以WNetAddConnection2方式作为当前用户?

编辑2 :按照eryksun的说明,我想出了这个:

import ctypes
from win32security import TOKEN_IMPERSONATE, TOKEN_ALL_ACCESS
from win32process import GetWindowThreadProcessId
from win32api import OpenProcess
from win32security import OpenProcessToken
from win32security import ImpersonateLoggedOnUser
from win32security import RevertToSelf

user32 = ctypes.WinDLL('user32', use_last_error=True);

user32.GetShellWindow.restype = ctypes.c_void_p
handle = user32.GetShellWindow()

threadId, processId = GetWindowThreadProcessId(handle)

handle_op = OpenProcess(TOKEN_ALL_ACCESS, True, processId)

handle_opt = OpenProcessToken(handle_op, TOKEN_IMPERSONATE)
ImpersonateLoggedOnUser(handle_opt) # throws access denied error

enter image description here

    import ctypes
    from win32process import GetWindowThreadProcessId
    from win32api import OpenProcess
    from win32security import OpenProcessToken, ImpersonateLoggedOnUser, RevertToSelf, TOKEN_QUERY, TOKEN_DUPLICATE

    from win32con import PROCESS_QUERY_INFORMATION

    user32 = ctypes.WinDLL('user32', use_last_error=True);

    user32.GetShellWindow.restype = ctypes.c_void_p
    handle = user32.GetShellWindow()

    threadId, processId = GetWindowThreadProcessId(handle)

    handle_op = OpenProcess(PROCESS_QUERY_INFORMATION, False, processId)

    handle_opt = OpenProcessToken(handle_op, TOKEN_QUERY | TOKEN_DUPLICATE)
    ImpersonateLoggedOnUser(handle_opt)


    try:
        nr = win32wnet.NETRESOURCE()
        nr.dwScope = win32netcon.RESOURCE_GLOBALNET
        nr.dwType = DISK
        nr.dwUsage = win32netcon.RESOURCEUSAGE_CONNECTABLE
        nr.lpLocalName = 'Z:'
        nr.lpRemoteName = '\\\\192.168.178.46\\Test'

        win32wnet.WNetAddConnection3(None, nr, None, None, 25)
    except:
        print("Unexpected error...")

    RevertToSelf()

1 个答案:

答案 0 :(得分:0)

win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, drive,
    networkPath, None, user, password)

云端硬盘是您要将网络驱动器映射到的本地驱动器,例如X:\

对于网络路径,在路径的开头添加\\,例如\\\\networkpath02如果您可以在资源管理器中使用\\networkpath02访问路径。