Solved

Retrieve client's mapped drive

Posted on 2007-11-13
8
358 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 500 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

717 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