Solved

Retrieve client's mapped drive

Posted on 2007-11-13
8
356 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

785 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