Solved

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

Posted on 2011-02-28
5
1,544 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 166 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 167 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 167 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

760 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

21 Experts available now in Live!

Get 1:1 Help Now