Link to home
Start Free TrialLog in
Avatar of pospichalales
pospichalales

asked on

VBScript for mapping and unmapping network drives

Is this VBScript working and is it good solution for login script which will map network drives?
I need to remove old drives K: to R: and map new drives M: to R: and assign a name for these mappings.
Is it OK?

Option Explicit
Dim oNetwork, oShell, objFSO, prikaz, Drive1, Drive2, Drive3, Drive4, Drive5, Drive6, Drive7
Set objFSO = CreateObject("Scripting.FileSystemObject")

Drive1 = "K:"
If (objFSO.DriveExists(Drive1) = True) Then
objNetwork.RemoveNetworkDrive Drive1,True,True
Else
End If

Drive2 = "K:"
If (objFSO.DriveExists(Drive2) = True) Then
objNetwork.RemoveNetworkDrive Drive2,True,True
Else
End If

Drive3 = "M:"
Set oNetwork = CreateObject("WScript.Network")
If (objFSO.DriveExists(Drive3) = True) Then
objNetwork.RemoveNetworkDrive Drive3,True,True
Else
End If
objNetwork.MapNetworkDrive Drive3, "\\ZSSERVER\public"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(Drive3).Self.Name = "Verejné"

Drive4 = "N:"
Set oNetwork = CreateObject("WScript.Network")
If (objFSO.DriveExists(Drive4) = True) Then
objNetwork.RemoveNetworkDrive Drive4,True,True
Else
End If
objNetwork.MapNetworkDrive Drive4, "\\ZSSERVER\chranene"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(Drive4).Self.Name = "Chránené"

Drive5 = "O:"
Set oNetwork = CreateObject("WScript.Network")
If (objFSO.DriveExists(Drive5) = True) Then
objNetwork.RemoveNetworkDrive Drive5,True,True
Else
End If
objNetwork.MapNetworkDrive Drive5, "\\ZSSERVER\fotografie"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(Drive5).Self.Name = "Fotografie"

Drive6 = "P:"
Set oNetwork = CreateObject("WScript.Network")
If (objFSO.DriveExists(Drive6) = True) Then
objNetwork.RemoveNetworkDrive Drive6,True,True
Else
End If
objNetwork.MapNetworkDrive Drive6, "\\ZSSERVER\zaci"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(Drive6).Self.Name = "Žáci"

Drive7 = "R:"
Set oNetwork = CreateObject("WScript.Network")
If (objFSO.DriveExists(Drive7) = True) Then
objNetwork.RemoveNetworkDrive Drive7,True,True
Else
End If 
objNetwork.MapNetworkDrive Drive7, "\\ZSSERVER\aplikace"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(Drive7).Self.Name = "Aplikace"

WScript.Quit

Open in new window

Avatar of pospichalales
pospichalales

ASKER

There are some bugs - objNetwork should be oNetwork
and second K: should be L:
ASKER CERTIFIED SOLUTION
Avatar of jlindler
jlindler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Don
Don
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Group policy preferences are best, but I cannot use them because our school have computers with Windows 2000, XP and 7. Windows 2000 does not support it and Windows XP requires installing Client Side Extensions.
With regards to your VBS script, here's a slightly easier way to code that I would say.  Just to share a different approach for your consideration.

~bp

Option Explicit
Dim oNetwork, oShell, oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oNetwork = CreateObject("WScript.Network")
Set oShell = CreateObject("Shell.Application")

DoMapping "K:", "", ""
DoMapping "M:","\\ZSSERVER\public","Verejné"
DoMapping "N:","\\ZSSERVER\chranene","Chránené"
DoMapping "O:","\\ZSSERVER\fotografie","Fotografie"
DoMapping "P:","\\ZSSERVER\zaci","Žáci"
DoMapping "R:","\\ZSSERVER\aplikace","Aplikace"

Set oFSO = Nothing
Set oNetwork = Nothing
Set oShell = Nothing

WScript.Quit

Sub DoMapping(sDrive, sShare, sName)
   If oFSO.DriveExists(sDrive) Then
      oNetwork.RemoveNetworkDrive sDrive, True, True
   End If
   If sShare <> "" then
      oNetwork.MapNetworkDrive sDrive, sShare
      oShell.NameSpace(sDrive).Self.Name = sName
   End If
End Sub

Open in new window

Thank you.
Welcome.

~bp