rtmcmullen
asked on
Determine what drives are persistently mapped and read that info into log file
Is there a way to do this in vbscript maybe and run through a login script? I'm trying to determine what drives are mapped persistent as we are getting greater control on the environment. I re did the login script and want to disconnect all drives before mapping but want to see if there are any drives that are persistenly mapped so I can route accordingly.. Thanks
OK here are the key details:
http://www.winguides.com/registry/display.php/200/
and details of how to extract registry keys in VBS:
http://www.codeproject.com/books/1578701392.asp
http://www.winguides.com/registry/display.php/200/
and details of how to extract registry keys in VBS:
http://www.codeproject.com/books/1578701392.asp
Actually on the box I am on at the moment the entry as shown above doesn't appear to eb right. Try
HKEY_CURRENT_USER\Network
under which you get a key for each drive letter then entries for RemotePath
e.g. you can read the path for the X: drive mapping with
oShell.RegRead ("HKCU\Network\X\RemotePat h")
HKEY_CURRENT_USER\Network
under which you get a key for each drive letter then entries for RemotePath
e.g. you can read the path for the X: drive mapping with
oShell.RegRead ("HKCU\Network\X\RemotePat
ASKER
Ok, I have this which is reading it into excel, but it is reading the first drive 3 times:
' List persistent drive mappings
'Wscript.Echo oDrives.Count / 2, "Network Connections"
For ix = 0 To oDrives.Count - 2 Step 2
sDrive = oDrives(ix)
sUNCPath = oDrives(ix+1)
If sDrive = "" Then sDrive = "--"
ws.Cells(2,36).value = sDrive & " = " & sUNCPath
ws.Cells(2,37).value = sDrive & " = " & sUNCPath
ws.Cells(2,38).value = sDrive & " = " & sUNCPath
Next
If I do a query and echo, it will display drives in succession:
Dim oNet, oDrives, ix, sDrive, sUNCPath
Set oNet = CreateObject("Wscript.Netw ork")
Set oDrives = oNet.EnumNetworkDrives
Wscript.Echo oDrives.Count / 2, "Network Connections"
For ix = 0 To oDrives.Count - 2 Step 2
sDrive = oDrives(ix)
sUNCPath = oDrives(ix+1)
If sDrive = "" Then sDrive = "--"
Wscript.Echo "Drive: " & sDrive, "UNC Path: " & sUNCPath
Next
I guess my question now is, how would I go about getting all drives into the excel doc in succession?
' List persistent drive mappings
'Wscript.Echo oDrives.Count / 2, "Network Connections"
For ix = 0 To oDrives.Count - 2 Step 2
sDrive = oDrives(ix)
sUNCPath = oDrives(ix+1)
If sDrive = "" Then sDrive = "--"
ws.Cells(2,36).value = sDrive & " = " & sUNCPath
ws.Cells(2,37).value = sDrive & " = " & sUNCPath
ws.Cells(2,38).value = sDrive & " = " & sUNCPath
Next
If I do a query and echo, it will display drives in succession:
Dim oNet, oDrives, ix, sDrive, sUNCPath
Set oNet = CreateObject("Wscript.Netw
Set oDrives = oNet.EnumNetworkDrives
Wscript.Echo oDrives.Count / 2, "Network Connections"
For ix = 0 To oDrives.Count - 2 Step 2
sDrive = oDrives(ix)
sUNCPath = oDrives(ix+1)
If sDrive = "" Then sDrive = "--"
Wscript.Echo "Drive: " & sDrive, "UNC Path: " & sUNCPath
Next
I guess my question now is, how would I go about getting all drives into the excel doc in succession?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Steve, thanks, that works great, but your right, that list all drive mappings. To back up a little, I also want this to execute outside of a login script so I can do a audit to determine what drives are mapped persistently even for users not using login scripts. (non standard) I would want to run against a list of computers using a for loop to find persistent mappings. I don't want to blow away persistent drive mappings for users once I bring everyone under a login script, rogue users, etc.,
The only thing I can think of is to read the reg key you listed:
oShell.RegRead ("HKCU\Network\X\RemotePat h")
How would I go about conditioning that to read the letter and value in a script though?
The only thing I can think of is to read the reg key you listed:
oShell.RegRead ("HKCU\Network\X\RemotePat
How would I go about conditioning that to read the letter and value in a script though?
ASKER
If anyone is interested, I create the code below. Thanks for your help Steve.
'========================= ========== ========== ========== ========== ========== ========== =========
' NAME: PersitentDrive.vbs
'
' AUTHOR: rtmcmullen
' CREATED : 11/2/2006
' LAST MODIFIED: 11/2/2006
' VERSION: 1.0
'
' PURPOSE: To read both Registry subkey and value to determine "Persistent" Mapped drives ONLY.
' Outputs information
' SYNTAX: cscript //nologo persistentdrive.vbs
' SYNTAX to append output to log file: cscript //nologo persistentdrive.vbs >> "\\Server\Share\%COMPUTERN AME%-Peris tentMappin gs.log"
' Example of output created in text file:
' Date = 2-11-2006
' Domain\Username = AMERICAS\rtmcmulle
' MAC Address = 00:0B:DB:C6:F6:25
' OS = Microsoft Windows XP Professional
' Service Pack = Service Pack 2
' Persistent Drive Mapping = X:\\AMERICAS-WILM-APP01\AP PS
' Persistent Drive Mapping = Z:\\AMERICAS-WILM-APP01\AP PS
'
'========================= ========== ========== ========== ========== ========== ========== =========
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
'Explicitly Declare Variables
Dim WSHNetwork, oDrives, ix, sDrive, sUNCPath, count
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
'Get Date
WScript.Echo "Date = " & Day(Now) & "-" & Month(Now) & "-" & Year(Now)
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
'Set Objects
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set WSHNetwork = CreateObject("Wscript.Netw ork")
Set oDrives = WSHNetwork.EnumNetworkDriv es
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
' WMI for local computer
strComputer = "."
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S elect * from Win32_ComputerSystem",,48)
For Each objItem In colItems
WScript.Echo "Domain\Username = " & objItem.UserName
Next
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
' Get MAC Address
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S elect * from Win32_NetworkAdapterConfig uration where IPEnabled=TRUE",,48)
For Each objItem In colItems
WScript.Echo "MAC Address = " & objItem.MACAddress(0)
Next
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S elect * from Win32_OperatingSystem",,48 )
For Each objItem In colItems
WScript.Echo "OS = " & objItem.Caption
WScript.Echo "Service Pack = " & objItem.CSDVersion
Next
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
'Read SUBKEY (Registry KEY read using WMI query)
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject("winmgmts:{ impersonat ionLevel=i mpersonate }!\\" & _
strComputer & "\root\default:StdRegProv" )
strKeyPath = "Network"
oReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
' Wscript.Echo subkey
'Next
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
'Registry Value Read (Registry Value using shell)
Set WshShell = CreateObject ("WScript.Shell")
Path = "HKCU\Network\Z\Remotepath "
Value = WSHShell.RegRead (Path)
Wscript.Echo "Persistent Drive Mapping = " & (subkey & ":") & (Value)
'************************* ********** ********** ********** ********** ********** ********** ********** ********** **
Next
'=========================
' NAME: PersitentDrive.vbs
'
' AUTHOR: rtmcmullen
' CREATED : 11/2/2006
' LAST MODIFIED: 11/2/2006
' VERSION: 1.0
'
' PURPOSE: To read both Registry subkey and value to determine "Persistent" Mapped drives ONLY.
' Outputs information
' SYNTAX: cscript //nologo persistentdrive.vbs
' SYNTAX to append output to log file: cscript //nologo persistentdrive.vbs >> "\\Server\Share\%COMPUTERN
' Example of output created in text file:
' Date = 2-11-2006
' Domain\Username = AMERICAS\rtmcmulle
' MAC Address = 00:0B:DB:C6:F6:25
' OS = Microsoft Windows XP Professional
' Service Pack = Service Pack 2
' Persistent Drive Mapping = X:\\AMERICAS-WILM-APP01\AP
' Persistent Drive Mapping = Z:\\AMERICAS-WILM-APP01\AP
'
'=========================
'*************************
'Explicitly Declare Variables
Dim WSHNetwork, oDrives, ix, sDrive, sUNCPath, count
'*************************
'Get Date
WScript.Echo "Date = " & Day(Now) & "-" & Month(Now) & "-" & Year(Now)
'*************************
'Set Objects
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set WSHNetwork = CreateObject("Wscript.Netw
Set oDrives = WSHNetwork.EnumNetworkDriv
'*************************
' WMI for local computer
strComputer = "."
'*************************
' Get Computer System Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S
For Each objItem In colItems
WScript.Echo "Domain\Username = " & objItem.UserName
Next
'*************************
' Get MAC Address
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S
For Each objItem In colItems
WScript.Echo "MAC Address = " & objItem.MACAddress(0)
Next
'*************************
' Get OS Details
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("S
For Each objItem In colItems
WScript.Echo "OS = " & objItem.Caption
WScript.Echo "Service Pack = " & objItem.CSDVersion
Next
'*************************
'Read SUBKEY (Registry KEY read using WMI query)
Const HKEY_CURRENT_USER = &H80000001
strComputer = "."
Set oReg=GetObject("winmgmts:{
strComputer & "\root\default:StdRegProv"
strKeyPath = "Network"
oReg.EnumKey HKEY_CURRENT_USER, strKeyPath, arrSubKeys
For Each subkey In arrSubKeys
' Wscript.Echo subkey
'Next
'*************************
'*************************
'Registry Value Read (Registry Value using shell)
Set WshShell = CreateObject ("WScript.Shell")
Path = "HKCU\Network\Z\Remotepath
Value = WSHShell.RegRead (Path)
Wscript.Echo "Persistent Drive Mapping = " & (subkey & ":") & (Value)
'*************************
Next
Thanks for giving me the points, nice script.
Steve
Steve
Can't check to give you the key at the moment because I'm not on a windows machine (well windows mobile but that doesn't count)
No doubt someone else will post the key, otherwise will look back when on Windows later (or search for a share name you know is persistently mapped in regedit)
Steve