Link to home
Start Free TrialLog in
Avatar of blkstrim
blkstrimFlag for United States of America

asked on

Gathering mapped drive information remotely

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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of blkstrim

ASKER

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