Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Gathering mapped drive information remotely

Posted on 2011-10-05
3
Medium Priority
?
460 Views
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")
strComputer=compid
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
	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
					objOutputFile.WriteLine subkey & ":" & vbTab & dapath
					'wscript.echo subkey & ":" & vbTab & dapath
				Next
			End If
		End If
	End If
Next
objOutputFile.WriteLine ""
objoutputFile.WriteLine "Current Logged In User: " & suser
objOutputFile.WriteLine "Ran on:" & Now
objOutputFile.WriteLine "________________________________________"
objOutputFile.WriteLine ""
objOutputFile.WriteLine ""


objoutputFile.Close

Open in new window

0
Comment
Question by:blkstrim
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 36921682
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.

Regards,

Rob.
0
 

Author Comment

by:blkstrim
ID: 36928480
Rob,
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.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 36928496
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.

Rob.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

564 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