tuckcorp
asked on
script to get mapped drives
Hi, I want to write a script that will show all the mapped drive a user has. This is on a windows xp enviroment. I know that I can use net use or this script. The problem is If I run through psexec it will check my admin id drives and not the logged on user drive mappings. Any ideas
strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDrives = objWMI.ExecQuery("select * from Win32_MappedLogicalDisk")
WScript.Echo "Mapped Drives:"
for each objDrive in colDrives
WScript.Echo " Device ID: " & objDrive.DeviceID
WScript.Echo " Volume Name: " & objDrive.VolumeName
WScript.Echo " Session ID: " & objDrive.SessionID
WScript.Echo " Size: " & objDrive.Size
WScript.Echo
next
strComputer = "."
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDrives = objWMI.ExecQuery("select * from Win32_MappedLogicalDisk")
WScript.Echo "Mapped Drives:"
for each objDrive in colDrives
WScript.Echo " Device ID: " & objDrive.DeviceID
WScript.Echo " Volume Name: " & objDrive.VolumeName
WScript.Echo " Session ID: " & objDrive.SessionID
WScript.Echo " Size: " & objDrive.Size
WScript.Echo
next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you like PsExec Try to use PsInfo with -d
http://www.microsoft.com/technet/sysinternals/utilities/psinfo.mspx
http://www.microsoft.com/technet/sysinternals/utilities/psinfo.mspx
List mapped drives on remote machine, 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=imper sonate}!\\ " & strComputer & "\root\cimv2")
'Go and get the currently logged on user by checking the owner of the Explorer.exe process.
Set colProc = objWmiService.ExecQuery("S elect 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_U SERS, "", 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
'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=imper
'Go and get the currently logged on user by checking the owner of the Explorer.exe process.
Set colProc = objWmiService.ExecQuery("S
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_U
For Each strKey In arrRegKeys
If UCase(strKey) = ".DEFAULT" Or UCase(Right(strKey, 8)) = "_CLASSES" Then
Else
Set objSID = objWbem.Get("Win32_SID.SID
'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
wscript.echo subkey & ":" & vbTab & dapath
Next
End If
End If
End If
Next
My example off a script work, it list mapped drive for current user.
I have tested to login on a workstation and using “net use” to map some drives on server.
Then from another workstation use “psexec” to copy and execute this script remotely.
It then show the mapped drives I did for this user.
I have tested to login on a workstation and using “net use” to map some drives on server.
Then from another workstation use “psexec” to copy and execute this script remotely.
It then show the mapped drives I did for this user.
http://www.microsoft.com/technet/scriptcenter/resources/qanda/jul06/hey0718.mspx
Here you have more scripts.
http://www.microsoft.com/technet/scriptcenter/resources/qanda/all.mspx