Solved

Retrieve client's mapped drive

Posted on 2007-11-13
8
355 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
  • 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
 

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
setup share and NTFS permissions. 12 69
shadow copies 7 69
2003 File Server upgrade 11 50
Way to setup network drive share permanently mapped to server 3 47
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now