Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exports All Users Mapped Drives on One Machine

Posted on 2016-08-07
7
Medium Priority
?
324 Views
Last Modified: 2016-08-24
Hello,

I want to Export the Users Mapped Drives on One Machine. i found the below Script but it doesn't work

I need the txt file with the Computer name and inside it each user with his mapped Drive.

'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" , "\\02-maroon-02\Package Sources\Applications\MapDrive Logs\", OverwriteExisting 

Open in new window

0
Comment
Question by:fadyaz
7 Comments
 
LVL 1

Expert Comment

by:P. Sisk
ID: 41746261
How does this look?

' List Mapped Network Drives


On Error Resume Next

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * from Win32_MappedLogicalDisk")

For Each objItem in colItems
Wscript.Echo "Compressed: " & objItem.Compressed
Wscript.Echo "Description: " & objItem.Description
Wscript.Echo "Device ID: " & objItem.DeviceID
Wscript.Echo "File System: " & objItem.FileSystem
Wscript.Echo "Free Space: " & objItem.FreeSpace
Wscript.Echo "Maximum Component Length: " & objItem.MaximumComponentLength
Wscript.Echo "Name: " & objItem.Name
Wscript.Echo "Provider Name: " & objItem.ProviderName
Wscript.Echo "Session ID: " & objItem.SessionID
Wscript.Echo "Size: " & objItem.Size
Wscript.Echo "Supports Disk Quotas: " & objItem.SupportsDiskQuotas
Wscript.Echo "Supports File-Based Compression: " & _
objItem.SupportsFileBasedCompression
Wscript.Echo "Volume Name: " & objItem.VolumeName
Wscript.Echo "Volume Serial Number: " & objItem.VolumeSerialNumber
Wscript.Echo
Next
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 41746280
Here's a small BAT file as a starting point showing how you can gather info on mapped drives and write to a CSV file.

@echo off
setlocal
set ListFile=EE28961982.csv
(
  for /f "skip=2 tokens=1-4 delims=," %%A in ('wmic netuse get localname^,remotename^,username /Format:csv') do (
    echo %%A,%username%,%%B,%%C,%%D
  )
) > "%ListFile%"

Open in new window

~bp
0
 
LVL 12

Expert Comment

by:Benjamin Voglar
ID: 41746755
You can try it with powershell:

gwmi win32_mappedlogicaldisk | select SystemName,Name,ProviderName | export-csv c:\it\mapped.csv

Open in new window

0
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
LVL 2

Author Comment

by:fadyaz
ID: 41746793
Thanks Bill Prew. but your script will give me all users the logged in the machine and their maooed drive even if they logged off ? or only the online users ?

Thanks
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 41747043
My script will report on the mapped drives of the currently sctive user that ran the script.

~bp
0
 
LVL 2

Author Comment

by:fadyaz
ID: 41747054
Thanks for your reply. Can we make it for all users?
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 41747506
Okay, give this a try, see what it gives you.

@echo off
setlocal EnableDelayedExpansion

rem Define output file for CSV format data
set ListFile=EE28961982.csv

rem Send all output to the CSV file
(

  rem Write header line to CSV
  echo User,Drive,Path

  rem Get all accounts on this machine, and associated SIDs
  for /f "skip=2 tokens=2-3 delims=," %%A in ('wmic useraccount get name^,sid /Format:csv') do (

    rem Look in the registry for any Network node for this SID
    for /f "tokens=*" %%E in ('reg query "HKEY_USERS\%%B\Network" 2^>nul') do (

      rem Look for the path this mapped drive references
      for /f "tokens=2*" %%F in ('reg query "%%E" /V "RemotePath" 2^>nul ^|find /I "REG_"') do (

        rem Write user,drive,path to CSV
        set Drive=%%E
        echo %%A,!Drive:~-1!:,%%G

      )

    )

  )

) > "%ListFile%"

Open in new window

~bp
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

886 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