Solved

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

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
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…

726 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