mbt masai
 
Welcome !
         

                                
After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.

 List mapped drives on remote machine

Author Message
ginolard

  • Total Posts : 1347
  • Scores: 23
  • Reward points : 0
  • Joined: 8/11/2005
  • Status: offline
List mapped drives on remote machine Thursday, December 01, 2005 10:50 PM (permalink)
0
Improved version of the code that gets the currently logged on user.

 'Define variables, constants and objects
 
 strComputer="REMOTE MACHINE HERE"
 Const HKEY_USERS = &H80000003
 Set objWbem = GetObject("winmgmts:")
 Set objRegistry = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")
 Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
 'Go and get the currently logged on user by checking the owner of the Explorer.exe process.  
 
 Set colProc = objWmiService.ExecQuery("Select Name from Win32_Process" & " Where Name='explorer.exe' and SessionID=0")
 
 If colProc.Count > 0 Then
     For Each oProcess In colProc
         oProcess.GetOwner sUser, sDomain
     Next
 End If
 
 'Loop through the HKEY_USERS hive until (ignoring the .DEFAULT and _CLASSES trees) until we find the tree that 
 'corresponds to the currently logged on user.
 lngRtn = objRegistry.EnumKey(HKEY_USERS, "", arrRegKeys)    
     
 For Each strKey In arrRegKeys
     If UCase(strKey) = ".DEFAULT" Or UCase(Right(strKey, 8)) = "_CLASSES" Then
     Else
 
         Set objSID = objWbem.Get("Win32_SID.SID='" & strKey & "'")
 
 'If the account name of the current sid we're checking matches the accountname we're looking for Then
 'enumerate the Network subtree
         If objSID.accountname = sUser Then 
             regpath2enumerate = strkey & "\Network" 'strkey is the SID
             objRegistry.enumkey hkey_users, regpath2enumerate, arrkeynames
                 
 'If the array has elements, go and get the drives info from the registry
             If Not (IsEmpty(arrkeynames)) Then
                 For Each subkey In arrkeynames
                     regpath = strkey & "\Network\" & subkey
                     regentry = "RemotePath"
                     objRegistry.getstringvalue hkey_users, regpath, regentry, dapath
                     wscript.echo subkey & ":" & vbTab & dapath
                 Next
             End If
         End If
     End If
 Next
 


<message edited by ginolard on Tuesday, May 30, 2006 9:18 PM>
#1
    gurner78

    • Total Posts : 8
    • Scores: 0
    • Reward points : 0
    • Joined: 10/17/2006
    • Status: offline
    RE: List mapped drives on remote machine Tuesday, October 17, 2006 12:42 AM (permalink)
    0
    Or, borrowing from your script, i need to know who and what mapped drives have been created on workstations by the users in a very disjointed domain, to try and control via a GPO/KIX/VBS type logon script to query AD.  so I came up with this variation of your script, to put in a central NETLOGON Share as a VBS file to upload the mapped drive details for a user to a share on a NAS box

    and marked in Red my additions

    **********************************************************
    'Define variables, constants and objects

    'define text file and username

    Const ForAppending = 8
    Const OverwriteExisting = TRUE

    dim WSHNetwork, UserString
    set WSHNetwork = CreateObject("WScript.Network")
    UserString = WSHNetwork.UserName

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile _
        ("" & UserString & ".txt", ForAppending, True)


    ' rest

    strComputer="localhost"
    Const HKEY_USERS = &H80000003
    Set objWbem = GetObject("winmgmts:")
    Set objRegistry = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")
    Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    'Go and get the currently logged on user by checking the owner of the Explorer.exe process. 

    Set colProc = objWmiService.ExecQuery("Select Name from Win32_Process" & " Where Name='explorer.exe' and SessionID=0")

    If colProc.Count > 0 Then
       For Each oProcess In colProc
           oProcess.GetOwner sUser, sDomain
       Next
    End If

    'Loop through the HKEY_USERS hive until (ignoring the .DEFAULT and _CLASSES trees) until we find the tree that
    'corresponds to the currently logged on user.
    lngRtn = objRegistry.EnumKey(HKEY_USERS, "", arrRegKeys)   
      
    For Each strKey In arrRegKeys
       If UCase(strKey) = ".DEFAULT" Or UCase(Right(strKey, 8)) = "_CLASSES" Then
       Else

           Set objSID = objWbem.Get("Win32_SID.SID='" & strKey & "'")

    'If the account name of the current sid we're checking matches the accountname we're looking for Then
    'enumerate the Network subtree
           If objSID.accountname = sUser Then
               regpath2enumerate = strkey & "\Network" 'strkey is the SID
               objRegistry.enumkey hkey_users, regpath2enumerate, arrkeynames
                  
    'If the array has elements, go and get the drives info from the registry
               If Not (IsEmpty(arrkeynames)) Then
                   For Each subkey In arrkeynames
                       regpath = strkey & "\Network\" & subkey
                       regentry = "RemotePath"
                       objRegistry.getstringvalue hkey_users, regpath, regentry, dapath
                       objTextFile.WriteLine subkey & ":" & vbTab & dapath
               Next
                    objTextFile.Close
               End If
           End If
       End If
    Next

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "" & UserString & ".txt" , "\\nas1\Map\", OverwriteExisting


    ************************************************
    Cheers
    #2
      ginolard

      • Total Posts : 1347
      • Scores: 23
      • Reward points : 0
      • Joined: 8/11/2005
      • Status: offline
      RE: List mapped drives on remote machine Tuesday, October 17, 2006 7:22 PM (permalink)
      0
      If you're going to be running this script as part of a logon script then you really don't need to do it this way.  You can use WMI to get the Mapped drive information by querying Win32_LogicalDisk Where DriveType=4

      The only drawback of that method (and the reason I created the above code) is that it won't work when run against a REMOTE machine.
      Author of ManagePC - http://managepc.net

      #3
        vehemeni

        • Total Posts : 1
        • Scores: 0
        • Reward points : 0
        • Joined: 12/1/2006
        • Status: offline
        RE: List mapped drives on remote machine Friday, December 01, 2006 2:31 AM (permalink)
        0

        Hey,
        Your script is awesome!

        But it only lists the drives that were mapped by the logged in user on his own, how about drives that were mapped by a logon script?
        Can you help with this?
        Thanks,
        e.Mirandda
        #4
          ginolard

          • Total Posts : 1347
          • Scores: 23
          • Reward points : 0
          • Joined: 8/11/2005
          • Status: offline
          RE: List mapped drives on remote machine Friday, December 01, 2006 3:15 AM (permalink)
          0
          Unfortunately I've never found a way to do this remotely.  It's very frustrating because WMI almost promises to do it with Win32_LogicalDisk or Win32_NetworkConnection but doesn't return all the info when run against remote machines.
          Author of ManagePC - http://managepc.net

          #5
            ginolard

            • Total Posts : 1347
            • Scores: 23
            • Reward points : 0
            • Joined: 8/11/2005
            • Status: offline
            RE: List mapped drives on remote machine Monday, December 04, 2006 1:25 AM (permalink)
            0
            Well, I guess I spoke too soon.

            It's not pretty but it's fast and it works.

             strComputer = "xxxx" ' Enter a remote machine name here
             strTempOutFile="netuse.txt" 'Enter a filename to use here
             strtempFullPath="\\" & strComputer & "\c$\" & strTempOutFile ' Use a different path if you wish
             WScript.Echo strTempFullPath
             Set objFSo=CreateObject("Scripting.FileSystemObject")
             
             Set objWMIService = GetObject _
                 ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
             errReturn = objWMIService.Create _
                 ("cmd.exe /c net.exe use > c:\" & strTempOutFile, Null, Null, intProcessID) ' Make sure the path here coincides with  strtempFullPath
             
             'Make sure the file is created and has data in it.
             FileExists=False
             Do Until FileExists=True
             If objFSo.FileExists(strtempFullPath) Then
                 Set tempfile=objFSo.GetFile(strtempFullPath)
                     If tempfile.Size > 0 Then
                         FileExists=True
                         Exit Do
                     End If
             Else
                 WScript.Sleep 1000
             End If
             Loop
             
             InputFile=objFSo.OpenTextFile(strtempFullPath,1).ReadAll
             
             Arrayfile=Split(InputFile,vbcrlf)
             For i = 0 To UBound(Arrayfile)
                 If InStr(Arrayfile(i),"\\") > 1 Then
                     WScript.Echo Trim(Mid(Arrayfile(i),14,36))
                     
                 End If
             Next
             
             objFSo.DeleteFile(strtempFullPath)
             
            <message edited by ginolard on Monday, December 04, 2006 1:51 AM>
            Author of ManagePC - http://managepc.net

            #6
              kremlin

              • Total Posts : 1
              • Scores: 0
              • Reward points : 0
              • Joined: 5/21/2007
              • Status: offline
              RE: List mapped drives on remote machine Tuesday, May 22, 2007 8:04 PM (permalink)
              0
              Hello,
               
              This is my first reply in forum, so i could make some mistakes.
              I found this script very useful for collecting data from
              logged in computers, so i made some modifications that i could use it in logonscript and collect information
              into remote server shared folder.
              My users couldnt save temp file into c:\ directly so i had to redirect this file into user temp folder.
              After data is collected this file is copied into \\server\log\folder\ and log file in local pc is deleted.
              Script is tested with group policy and it works.
               
                Set WshShell = WScript.CreateObject("WScript.Shell")
               Set WshSysEnv = WshShell.Environment("PROCESS")
               tempfolder = WshSysEnv("TEMP")
               Set objNet = CreateObject("WScript.NetWork")
               strComputer = objNet.ComputerName 
               Set WshNetwork = WScript.CreateObject("WScript.Network")
               strUserName = WshNetwork.UserName
               strTempOutFile= objNet.ComputerName & "_" & strUserName & ".txt" 
               strtempFullPath=tempfolder & "\" & strTempOutFile 
               targetserver="\\server_name_here\log\map_share\"
               'WScript.Echo strTempFullPath
               Set objFSo=CreateObject("Scripting.FileSystemObject")
               Set objWMIService = GetObject _
                  ("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
               errReturn = objWMIService.Create _
                  ("cmd.exe /c net.exe use > " & strtempFullPath, Null, Null, intProcessID) 
               FileExists=False
               Do Until FileExists=True
               If objFSo.FileExists(strtempFullPath) Then
                  Set tempfile=objFSo.GetFile(strtempFullPath)
                      If tempfile.Size > 0 Then
                          FileExists=True
                          Exit Do
                      End If
               Else
                  WScript.Sleep 1000
               End If
               Loop
               InputFile=objFSo.OpenTextFile(strtempFullPath,1).ReadAll
               Arrayfile=Split(InputFile,vbcrlf)
               For i = 0 To UBound(Arrayfile)
                  If InStr(Arrayfile(i),"\\") > 1 Then
                      'WScript.Echo Trim(Mid(Arrayfile(i),14,36))
                      
                  End If
               Next
               Const OverwriteExisting = True
               objFSO.CopyFile strtempFullPath , targetserver , OverwriteExisting
               objFSo.DeleteFile(strtempFullPath)
               
               

              #7
                alfonsopilato

                • Total Posts : 2
                • Scores: 0
                • Reward points : 0
                • Joined: 2/1/2009
                • Status: offline
                RE: List mapped drives on remote machine Sunday, February 01, 2009 3:39 PM (permalink)
                0
                Hi,

                I would like to point out that running "net use" on a remote target machine does not provide the list of drives that are mapped by the user that is logged onto that remote pc.

                This is because  the command (net use) is being run under your own ID that's used to connect to the target machine.

                Therefore, when done in this fashion the command lists what YOU have mapped, if anything. Most of the time it will resturn something like: "There are no entries in the list".

                Here's what I recommend, and note there are two separate techniques, based on the type of target machine's O/S.

                WIN2K
                1) First,  use wmi to return the drives of the remote machine
                Here's the function to do it
                Function getMappedDriveLettersForWin2k(ws) 
                 'returns dictionary
                 Dim d,wmiquery 
                 set d = CreateObject("Scripting.Dictionary") 
                 Set getMappedDriveLettersForWin2k = d 
                 wmiquery = "Select caption From Win32_LogicalDisk Where DriveType = 4"
                 Set colItems = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & ws& "\root\cimv2" ).ExecQuery (wmiquery)   
                 For Each objItem In colItems                 
                      d.Add Replace (objItem.caption,":",""),True
                 Next
                End function
                 
                 
                then look up the drive resource using registry
                HKEY_USERS\S-1-5-21-..\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints\<driveletter>



                 
                Note if you're dealing with w2k remote pc, best use "reg query" command line since other methods may fail.


                Then look up the binary value "_UB" for each subkey



                Decipher binary to text using :
                 For i = 1 to Len(z) step 2
                hexval = Mid(z,i,2)
                        If ("&H" & hexval) > 0 Then strString = strString & Chr("&H" & hexval)
                Next    



                 


                WINXP:


                far more straight forward:


                enmerate subkeys to


                HKEY_USERS\S-1-5-2...\Network
                All the info is found in plain text .

                Hope this helps!
                <message edited by alfonsopilato on Friday, February 06, 2009 1:48 AM>
                #8
                  ginolard

                  • Total Posts : 1347
                  • Scores: 23
                  • Reward points : 0
                  • Joined: 8/11/2005
                  • Status: offline
                  Re: RE: List mapped drives on remote machine Monday, August 02, 2010 3:05 AM (permalink)
                  0
                  Yes, enumerating the Network subkey under the user's SID in HKU is how I do it.  The only drawback is that will only find network drives mapped by THAT user.  Drives mapped by, say, a login script that runs under a different account will not be shown.

                  I have never found a way to get those drives.
                  Author of ManagePC - http://managepc.net

                  #9
                    alfonsopilato

                    • Total Posts : 2
                    • Scores: 0
                    • Reward points : 0
                    • Joined: 2/1/2009
                    • Status: offline
                    Re: RE: List mapped drives on remote machine Monday, August 02, 2010 5:08 AM (permalink)
                    0
                    I've been working on this since I last wrote and have learnt a few more things.

                    To my understanding, a logon script may use elevated privileges when running, however it doesn't perform drive mapping under a different name. My understanding is that there is no drive mapping that's machine wide, it's all profile limited, and if it's done by a logon script, then it's done for the user's profile who is running that logon script.

                    Drives that have been mapped persistently can be listed using the registry method.. they are in the registry so that the system may remap them on next logon.. hence the term "persistent".

                    Most logon scripts where I work will not map the drives persistently, and this is done purposefully.

                    I am very much in the same boat as you, in that I need to also list those drive mapping that are not persistent on a given machine for a given user.
                    #10
                      TomRiddle

                      • Total Posts : 608
                      • Scores: 12
                      • Reward points : 0
                      • Joined: 2/7/2008
                      • Location: Australia
                      • Status: offline
                      Re: RE: List mapped drives on remote machine Monday, August 02, 2010 2:40 PM (permalink)
                      0
                      I use a script that is passed the remote computer name, it works out who the logged on user is, get's that user's sid, enumerate the registry to get the mapped drives.

                      You used to be able to run "net use" remotely but that no longer works due to some security patch. So as far as I can tell the only way is work it out (get all mapped drives) is maybe add into the logon script a bit of code that saves what drives that it mapped to a registry key or text file that you can remotely access.

                      If I was going to progress this I would develop it in powershell, i.e http://www.powershellcommunity.org/Forums/tabid/54/aff/1/aft/4450/afv/topic/Default.aspx
                      -join([int[]][char[]]'Ut|jwXmjqq%Wzqjx'|%{[char]($_-5)})
                      #11
                        ginolard

                        • Total Posts : 1347
                        • Scores: 23
                        • Reward points : 0
                        • Joined: 8/11/2005
                        • Status: offline
                        Re: RE: List mapped drives on remote machine Thursday, August 05, 2010 12:25 AM (permalink)
                        0
                        Even Powershell can't get the drives mapped by a login script.  I wish I knew where NET USE gets its information from!
                        Author of ManagePC - http://managepc.net

                        #12
                          robvip2442

                          • Total Posts : 3
                          • Scores: 0
                          • Reward points : 0
                          • Joined: 3/16/2010
                          • Status: offline
                          Re: RE: List mapped drives on remote machine Wednesday, February 01, 2012 3:34 AM (permalink)
                          0
                          I was wondering if I can take the code I have put together below with help from you guys, and also make this work for a Windows 7 machine. This works perfectly against an XP machine when I runas Admin.
                           
                           
                           
                          'Define variables, constants and objects
                          
                          Dim strComputer
                          
                          ' Input the Machine Name
                          strComputer=InputBox("Enter the Machine Name:")
                          MsgBox("You typed " & strComputer)
                           
                          ' strComputer="Machine Name"
                           Const HKEY_USERS = &H80000003
                           Set objWbem = GetObject("winmgmts:")
                           Set objRegistry = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")
                           Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                           
                           'Go and get the currently logged on user by checking the owner of the Explorer.exe process.  
                           
                           Set colProc = objWmiService.ExecQuery("Select Name from Win32_Process" & " Where Name='explorer.exe' and SessionID=0")
                           
                           If colProc.Count > 0 Then
                           For Each oProcess In colProc
                           oProcess.GetOwner sUser, sDomain
                           Next
                           End If
                           
                           'Loop through the HKEY_USERS hive until (ignoring the .DEFAULT and _CLASSES trees) until we find the tree that 
                           'corresponds to the currently logged on user.
                           lngRtn = objRegistry.EnumKey(HKEY_USERS, "", arrRegKeys)    
                           
                           For Each strKey In arrRegKeys
                           If UCase(strKey) = ".DEFAULT" Or UCase(Right(strKey, 8)) = "_CLASSES" Then
                           Else
                           
                           Set objSID = objWbem.Get("Win32_SID.SID='" & strKey & "'")
                           
                           'If the account name of the current sid we're checking matches the accountname we're looking for Then
                           'enumerate the Network subtree
                           If objSID.accountname = sUser Then 
                           regpath2enumerate = strkey & "\Network" 'strkey is the SID
                           objRegistry.enumkey hkey_users, regpath2enumerate, arrkeynames
                           
                           'If the array has elements, go and get the drives info from the registry
                           If Not (IsEmpty(arrkeynames)) Then
                           For Each subkey In arrkeynames
                           regpath = strkey & "\Network\" & subkey
                           regentry = "RemotePath"
                           objRegistry.getstringvalue hkey_users, regpath, regentry, dapath
                           wscript.echo subkey & ":" & vbTab & dapath
                           Next
                           End If
                           End If
                           End If
                           Next 

                          #13

                            Online Bookmarks Sharing: Share/Bookmark

                            Jump to:

                            Current active users

                            There are 0 members and 1 guests.

                            Icon Legend and Permission

                            • New Messages
                            • No New Messages
                            • Hot Topic w/ New Messages
                            • Hot Topic w/o New Messages
                            • Locked w/ New Messages
                            • Locked w/o New Messages
                            • Read Message
                            • Post New Thread
                            • Reply to message
                            • Post New Poll
                            • Submit Vote
                            • Post reward post
                            • Delete my own posts
                            • Delete my own threads
                            • Rate post

                            2000-2012 ASPPlayground.NET Forum Version 3.8
                            mbt shoes www.wileywilson.com