[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB script to map a network drive with logged in user name

Posted on 2011-02-28
5
Medium Priority
?
1,560 Views
Last Modified: 2012-05-11
Hi,

I have windows 2008 file server with shared users folders. I want to run a VB script in GPO so that for each user it should select appropriate user folder from file server and the mapped network drive should display the name of the user. for eg [X:John]

Any help is appriciated.

thanks
0
Comment
Question by:vneedhelp
[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 Comments
 
LVL 3

Expert Comment

by:mnation1
ID: 35003950
I'm sure there's a way in VB, but it should also work with a DOS batch file.

Copy what's below into a text file and rename with a .bat file extension.

@echo off
net use x: \\[servername]\%username%

This is assuming you have a folder named the same as the username that is shared on the file server.

good luck!
0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 664 total points
ID: 35004084
Hi, this can do it in VBScript.

Regards,

Rob.
Set objNetwork = CreateObject("WScript.Network")
strDrive = "X:"
strRemotePath = "\\server\users\" & objNetwork.Username
strNewName = objNetwork.Username & "'s Home Drive"

objNetwork.MapNetworkDrive strDrive, strRemotePath, True

' Rename the Mapped Drive
Set objShellApp = CreateObject("Shell.Application")
objShellApp.NameSpace(strDrive).Self.Name = strNewName

Open in new window

0
 
LVL 14

Accepted Solution

by:
Psy053 earned 668 total points
ID: 35004091
Try the following:
Set objNetwork = CreateObject("WScript.Network") 


strDriveLetter = "Z:" 
strRemotePath = "\\Server\Path" 
strUserName = objNetwork.Username

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath & "\" & strUserName, True 


' If you don't want the mapping labelled, remove this and the following lines
' The following will label the drive, so in the list it will appear as: <user>'s Home Drive

Set objShell = CreateObject("Shell.Application")
strNewName = strUserName & "'s Home Drive"
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

Open in new window

0
 
LVL 14

Expert Comment

by:Psy053
ID: 35004095
Argh, I should have typed it quicker.
0
 
LVL 3

Assisted Solution

by:sam0x01
sam0x01 earned 668 total points
ID: 35016483
The code below works a little differently. I will query the users HomeDirectory property from the logged on user in the domain, and map the drive to that property.

Regards

Shane
 
Option Explicit
' This script gets the users Home Directory from their NTDomain account and maps the HomeDrive to it
' after deleting the old one. This is for overrriding when the parent folder is mapped to

Const NTDomain = "domain.realm"
Const HomeDrive = "driveletter:"

Dim UserName, HomeDirectory, objWSHNetwork

'If user is not logged into NTDomain Then exit gracefully
If (LCase(GetLoggedOnUserDNSDomain()) <> NTDomain) Then 
'	WScript.Echo LCase(GetLoggedOnUserDNSDomain()) & " <> " & NTDomain
	WScript.Quit(0)
End If

' Get Current Username
UserName = GetLoggedOnUserName()

'Get  Users Home Directory
HomeDirectory = GetUserHomeDirectory(UserName)

' Used for disconnecting and reconnecting U Drive
Set objWSHNetwork = WScript.CreateObject("WScript.Network") 

' Remove the previously mapped drive
objWSHNetwork.RemoveNetworkDrive HomeDrive, True, True

'Map non-persistent drive
objWSHNetwork.MapNetworkDrive HomeDrive, HomeDirectory, False

Function  GetLoggedOnUserName()
	Dim objWSHNetwork

	Set objWSHNetwork = WScript.CreateObject("WScript.Network")
	' Returns currently logged on user
	GetLoggedOnUserName = objWSHNetwork.UserName
End Function

Function GetUserHomeDirectory(UserName)
	Dim objUser
	
	Set objUser = GetObject("WinNT://" & NTDomain& "/" & UserName & ",user")
	' Returns currently Home Directory path for passed UserName
	GetUserHomeDirectory = objUser.HomeDirectory
End Function

Function  GetLoggedOnUserDNSDomain()
	Dim objWSHShell
	Set objWSHShell = WScript.CreateObject("WScript.Shell")
	' Returns USERDNSDOMAIN environment variable
	GetLoggedOnUserDNSDomain = objWSHShell.ExpandEnvironmentStrings("%USERDNSDOMAIN%")
End Function

Open in new window

0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…

656 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