?
Solved

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

Posted on 2011-02-28
5
Medium Priority
?
1,570 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
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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…
Suggested Courses

864 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