Gathering mapped drive information remotely

Posted on 2011-10-05
Last Modified: 2012-05-12
I have the script below that I am attempting to use to gather mapped drive information for the currently logged in user remotely. The script works fine on XP machines but will not work with Windows 7. What am I missing?

I am not a vbscript writer by any stretch of the imagination. I have compiled it by taking snippets from code that I have found online and modifying it.

Any help would be greatly appreciated.
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.OpenTextFile("C:\scripts\output.txt",8,True)

'Define variables, constants and objects

compid = InputBox("Enter Computer Name","Gather Mapped Drives for Current Logged on User")
objOutputFile.WriteLine "Computer Name: " & strComputer
objOutputFile.WriteLine "----------------------------------------"

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
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

		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
					objOutputFile.WriteLine subkey & ":" & vbTab & dapath
					'wscript.echo subkey & ":" & vbTab & dapath
			End If
		End If
	End If
objOutputFile.WriteLine ""
objoutputFile.WriteLine "Current Logged In User: " & suser
objOutputFile.WriteLine "Ran on:" & Now
objOutputFile.WriteLine "________________________________________"
objOutputFile.WriteLine ""
objOutputFile.WriteLine ""


Open in new window

Question by:blkstrim
    LVL 65

    Accepted Solution

    For this to work, the drive MUST be a persistent mapping.  That's the only time it puts it in the Network key.  I have tested it and it worked, although I changed SessionID to 2 because I had RDP'd to the Windows 7 machine.



    Author Comment

    Thanks for the help. That lead me in the right direction to complete the script to work with both my xp and windows 7 machines. I modified the script to first check for the OS on the machine and then use the correct session id for that OS. Works perfectly for what I need to gather.
    LVL 65

    Expert Comment

    No worries.  Are you saying that it's always session ID 2, even when someone is logged in at the console?  That seems odd....but you never know...

    Thanks for the grade.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now