?
Solved

Retrieve client's mapped drive

Posted on 2007-11-13
8
Medium Priority
?
359 Views
Last Modified: 2010-08-05
how to write vbscript to do if user is a member of xyz group then retrive the user's current mapped drives and store it on a share
0
Comment
Question by:rezzman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20278092
Try this.....

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path

const bytesToGb = 1073741824
strComputer = "." ' Local Computer

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'Grab the user name
UserString = WSHNetwork.UserName

'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDrives = objWMI.ExecQuery("select * from Win32_MappedLogicalDisk")

For Each GroupObj In UserObj.Groups
'Force upper case comparison of the group names, otherwise this is case sensitive.
    Select Case UCase(GroupObj.Name)
    'Check for group memberships and take needed action
        Case "XYZ"
for each objDrive in colDrives
      WScript.Echo "Device ID: " & objDrive.DeviceID & vbcrlf & _
                    "Volume Name: " & objDrive.VolumeName & vbcrlf & _
                 "Session ID: " & objDrive.SessionID & vbcrlf & _
                 "Size: " & round(objDrive.Size / bytesToGb,1) & " Gb" & vbcrlf & _
                     "Description: " & objDrive.Description
next
          End Select

next
0
 
LVL 14

Expert Comment

by:Erik Pitti
ID: 20278110
'// this is a quick and dirty sample script that will print out
'//the current mapped drives for a user who is a member
'//of the group named SOME_GROUP

Dim objNetwork, strDomain, strUser, strAdsPath

Set objNetwork = WScript.CreateObject("Wscript.Network")
strDomain = objNetwork.UserDomain
strUser = objNetwork.UserName
strAdsPath = strDomain & "/" & strUser

If IsMember("SOME_GROUP") Then
    CollectMappedDrives()
End If


'//Support functions below:

Sub CollectMappedDrives()
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set clDrives = WshNetwork.EnumNetworkDrives    
    For i = 0 to clDrives.Count -1
        WScript.Echo clDrives.Item(i)
    Next
End Sub
'// CollectMappedDrives taken from EnumNetworkDrives sample here:
'// http://devguru.com/technologies/wsh/17402.asp


Function IsMember(sGroup)
    Dim oDict, oUser, oGroup

    If IsEmpty(oDict) Then
        Set oDict = CreateObject("Scripting.Dictionary")
        oDict.CompareMode = vbTextCompare
        Set oUser = GetObject("WinNT://" & strAdsPath & ",user")
        For Each oGroup In oUser.Groups
            oDict.Add oGroup.Name, "-"
        Next
        Set oUser = Nothing
    End If
    IsMember = CBool(oDict.Exists(sGroup))
End Function

'// IsMember taken from the VBScriipt logon script sample here:
'// http://www.anonymoos.com/scripting/
0
 
LVL 14

Expert Comment

by:Erik Pitti
ID: 20278121
To save the script above's output to a share save it as EnumShares.vbs and use the following batch code to call it.

@ECHO OFF
CSCRIPT //NOLOGO EnumShares.vbs > \\server\share\%USERNAME%@%COMPUTERNAME%.TXT
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:rezzman
ID: 20280956
this great!

my bad.  i want to run the script via login script and place the out put to a file into a share drive.  wondering, is it possible to have a giant csv file created with user info such as user name, computer name, ip address & drive mapping?
0
 
LVL 14

Expert Comment

by:Erik Pitti
ID: 20282833
absolutely.

The following function (taken from:http://www.codeproject.com/vbscript/ipaddress.asp) will get the system's IP address.

--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--0--

Function GetIPAddress
' This function retrieves the IP Address from the registry
' It gets it from the CurrentControlSet, so even when using DHCP
' it returns the correct IP Address

   Dim key
   Dim cTempIPAddress
   dim cIPAddressKey

   Set oSh = CreateObject("WScript.Shell")

   cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
   cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"

   cNicServiceName=oSh.RegRead(cNICSearch)
   cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
   cTempIPAddress=oSh.RegRead (cIPAddresskey)
   GetIPAddress=cTempIPAddress
End Function
0
 
LVL 14

Accepted Solution

by:
Erik Pitti earned 1500 total points
ID: 20282889
'// this is a quick and dirty sample script that will print out
'//the current mapped drives for a user who is a member
'//of the group named SOME_GROUP

'//Complete script follows with CSV output

Dim objNetwork, strDomain, strUser, strAdsPath,strComputerName

Set objNetwork = WScript.CreateObject("Wscript.Network")
strDomain = objNetwork.UserDomain
strUser = objNetwork.UserName
strAdsPath = strDomain & "/" & strUser
strComputerName=objNetwork.ComputerName
strIpAddress=GetIPAddress()

If IsMember("SOME_GROUP") Then
    CollectMappedDrives()
End If


'//Support functions below:

Sub CollectMappedDrives()
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set clDrives = WshNetwork.EnumNetworkDrives    
    If clDrives.Count > 0 Then
        For i = 0 to clDrives.Count -1
            'WScript.Echo clDrives.Item(i)
            Wscript.Echo """" & strUser & """,""" & strComputerName & """,""" & strIPAddress & """,""" & clDrives.Item(i) & """"
        Next
    Else
        Wscript.Echo """" & strUser & """,""" & strComputerName & """,""" & strIPAddress & """,""" & """No Mapped Drives"""
    End If
End Sub
'// CollectMappedDrives taken from EnumNetworkDrives sample here:
'// http://devguru.com/technologies/wsh/17402.asp


Function IsMember(sGroup)
    Dim oDict, oUser, oGroup

    If IsEmpty(oDict) Then
        Set oDict = CreateObject("Scripting.Dictionary")
        oDict.CompareMode = vbTextCompare
        Set oUser = GetObject("WinNT://" & strAdsPath & ",user")
        For Each oGroup In oUser.Groups
            oDict.Add oGroup.Name, "-"
        Next
        Set oUser = Nothing
    End If
    IsMember = CBool(oDict.Exists(sGroup))
End Function

'// IsMember taken from the VBScriipt logon script sample here:
'// http://www.anonymoos.com/scripting/


'//The following function will get the system's IP address.
'//(taken from:http://www.codeproject.com/vbscript/ipaddress.asp)

Function GetIPAddress
' This function retrieves the IP Address from the registry
' It gets it from the CurrentControlSet, so even when using DHCP
' it returns the correct IP Address

   Dim key
   Dim cTempIPAddress
   dim cIPAddressKey

   Set oSh = CreateObject("WScript.Shell")

   cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
   cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"

   cNicServiceName=oSh.RegRead(cNICSearch)
   cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
   cTempIPAddress=oSh.RegRead (cIPAddresskey)
   GetIPAddress=cTempIPAddress
End Function
0
 

Author Comment

by:rezzman
ID: 20285518
Will csv file be locked while it's being used by one user?  What if two users output data being generated at the same time what will happen then?
0
 
LVL 14

Expert Comment

by:Erik Pitti
ID: 20291189
Yes, but the lock will be during the write operation only (we're talking milliseconds here).  There shouldn't be an issue unless you have two users who run that section of the login script at exactly the same instant.  I've not had issues when outputting data to csv files from login scripts and I've got 10,000 users across 200 sites.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've always wanted to allow a user to have a printer no matter where they login. The steps below will show you how to achieve just that. In this Article I'll show how to deploy printers automatically with group policy and then using security fil…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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