Solved

Retrieve client's mapped drive

Posted on 2007-11-13
8
354 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
Comment Utility
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
Comment Utility
'// 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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:Erik Pitti
Comment Utility
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
Comment Utility
'// 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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many of us need to configure DHCP server(s) in their environment. We can do that simply via DHCP console on server or using MMC snap-in on each computer with Administrative Tools installed in a network. But what if we have to configure many DHCP ser…
Scenerio: You have a server running Server 2003 and have applied a retail pack of Terminal Server Licenses.  You want to change servers or your server has crashed and you need to reapply the Terminal Server Licenses. When you enter the 16-digit lic…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

10 Experts available now in Live!

Get 1:1 Help Now