Roberto Alves

Scripting and server based computing

.VBS to remap network drivers after file server migrations

Very useful on File Servers migrations, this script was created and used on login script of 1000+ users after migration from Novell file server to a new Windows file server. The script reduces the calls on service desk after migration, reading what mapped drivers each user has and creating a new mapped driver.

Basically, you need specify share names that you wanna modify and put this script to run on login script. After a file server migration, users run login script and remap automatically network drivers.

On Error Resume Next

Const iDebug = 1      ‘If different of 0, writes a log file
Const iNumPaths = 4   ‘Number of Paths to be modified

Dim name
Dim old_path(4), new_path(4), Path_name(4)
Dim Letter_Name(25,2)
Dim Letter_Remaps, iCount
Dim objPing, strUserName

‘====
‘Define Paths
‘====

old_path(1)=”\NWFSrc15Vol1users”
new_path(1)=”%USER%”
Path_name(1) = “Wsrcspfs03″

old_path(2)=”\NWFSrc15Vol1″
new_path(2)=”\172.20.200.30vol1″
Path_name(2) = “Wsrcspfs05″

old_path(3)=”\NWFSrc10Vol1users”
new_path(3)=”%USER%”
Path_name(3) = “Wsrcspfs03″

old_path(4)=”\NWFSrc10Vol1″
new_path(4)=”\172.20.200.29vol2″
Path_name(4) = “Wsrcspfs04″

‘====
‘Objects
‘====

Set objWMIService=GetObject(“winmgmts:\.rootcimv2″)
Set WshNetwork = WScript.CreateObject(“WScript.Network”)
Set WshShell = createobject(“WScript.Shell”)
Set objShell = CreateObject(“Shell.Application”)
Set colDisks = objWMIService.ExecQuery(“Select * from Win32_NetworkConnection”)
Set oFileSys=CreateObject(“Scripting.FileSystemObject”)
if iDebug then
Set oFile=oFileSys.CreateTextFile(“C:map_log.txt”)
end if

if iDebug then
oFile.writeline  “Process started on: ” & Now()
oFile.writeline  “# Drives befor process: ” & colDisks.count
end if

‘====
‘Execute Ping to force Name Resolution
‘====

Set objPing = GetObject(“winmgmts:{impersonationLevel=impersonate}”)._
ExecQuery(“select * from Win32_PingStatus where address = ‘servername’”)

For iPath = 1 to iNumPaths
For Each objDisk In colDisks
strMapeamento = objDisk.RemoteName
If instr(ucase(strMapeamento),ucase(old_path(iPath))) then
Letra = objDisk.LocalName

‘=== If new_path = “%USER%”, script will map user home directory
if new_path(iPath)=”%USER%” then
strUserName = WshNetwork.UserName
path = “\wsrcspfs03Users1″ & strUserName
if iDebug then oFile.writeline ” %USER% = ” & path & ” >> ” & oFileSys.FolderExists(path)
if not oFileSys.FolderExists(path) Then
path = “\wsrcspfs03Users2″ & strUserName
End If
Else

‘=== Else  will map the new_path variable
path = Replace(ucase(strMapeamento),ucase(old_path(iPath)),ucase(new_path(iPath)))
End If

‘=== Remove map
if iDebug then oFile.writeline “Drive: ” & Letra & ” >> Old Map: ” & strMapeamento & ” >> New Map: ” & path
WshShell.Run “NET USE ” & Letra & ” /DELETE”,0,True

‘=== Map!
WShNetwork.MapNetworkDrive Letra, path, True
If Err.Number <> 0 Then
if iDebug then ofile.wscript “An error occurred on create a mapped drive for: ” & path
Err.Clear
WshShell.Run “NET USE ” & Letra & ” ” & chr(34) & path & chr(34) & ” /PERSISTENT:YES”,0,True
End If

‘=== Store in an array names for letters
Letter_Remaps = Letter_Remaps + 1
Letter_Name(Letter_Remaps,1) = trim(Letra) & “”
Letter_Name(Letter_Remaps,2) = mid(path,instrrev(path,”") + 1,len(path)) & ” on ” & path_name(iPath)
End if
Next
Set colDisks = objWMIService.ExecQuery(“Select * from Win32_NetworkConnection”)

Next

For iCount = 1 to Letter_Remaps
objShell.NameSpace(Letter_Name(iCount,1)).Self.Name = Lcase(Letter_Name(iCount,2))
Next

if iDebug then
oFile.writeline  “# Drives after process: ” & colDisks.count
oFile.writeline “Process finished on: ” & Now()
oFile.Close
end if

Category: Scripts

Your email address will not be published. Required fields are marked *

*