Solved

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

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
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 …
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…

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