Tuesday 25 September 2012

Getting a list of persistently mapped drives

There are many scripts on the Internet I found that allow you to enumerate which drives a user has currently mapped. What I needed was a list of drives mapped persistently (thus manually), which would exclude drives user has just mapped temporarily as well as drives mapped by other scripts. Here's the VBSCRIPT I came up with:


' List Persistently Mapped Network Drives
Option explicit

const HKEY_CURRENT_USER = &H80000001

Dim strComputer
Dim objReg
Dim objShell
Dim arrSubKeys
Dim i
Dim strRemotePath

strComputer = "."

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
Set objShell = Wscript.CreateObject("WScript.Shell")

objReg.EnumKey HKEY_CURRENT_USER, "Network", arrSubKeys

If IsArray(arrSubKeys) Then
    For i=0 to UBound(arrSubKeys)
        objReg.GetStringValue HKEY_CURRENT_USER, "Network\" & arrSubKeys(i), "RemotePath", strRemotePath
        objShell.Run ("%comspec% /K echo " & arrSubKeys(i) & ": is mounted to " & strRemotePath &_
            ">> \\server\share\%USERNAME%_on_%COMPUTERNAME%.txt"),0
    Next
Else
    objShell.Run ("%comspec% /K echo No persistent mappings found>> \\server\share\%USERNAME%_on_%COMPUTERNAME%.txt"),0
End If


Specify the script above as the Logoff script using a GPO and after a few days/weeks you will have a full list of who has mapped what and where, so that then a decision can be made as to how to migrate from manual drive mappings to automated ones.

No comments:

Post a Comment