如何通过VBS添加映射的网络驱动器?

时间:2015-05-14 21:37:12

标签: windows vbscript

我的vbs脚本存在一些问题。它只会添加F驱动器,而不会添加G驱动器。我做错了什么?

'## This is for network drives
Set objNetwork = CreateObject("WScript.Network") 
objNetwork.RemoveNetworkDrive "F:", True, True

'## for adding
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "F:" , "\\myserver\share1"
objNetwork.MapNetworkDrive "G:" , "\\myserver\share2"

2 个答案:

答案 0 :(得分:0)

<强> MapDrive.vbs

将驱动器号映射到网络文件共享(非持久性)的VBScript。

此脚本专为高于速度的可靠性而设计,因此每次登录时都会重新连接。 它记住了#39;记住&#39;连接,包括不再存在或脱机的文件共享的连接。 对于并非始终连接到域的计算机,这是一种很好的方法,例如笔记本电脑。

Windows XP不会映射“记住”&#39;连接到其他服务器,除非您首先取消映射&amp;取消记住现有连接,即使旧的连接路径当前已断开连接,这也适用。

对于每个驱动器号,有几种可能的状态,可能必须由脚本处理: - 记住(持久连接)/不记得 - 已连接/已连接到错误的网络共享/未连接。

此脚本将在连接到正确的文件共享之前删除任何现有的驱动器映射。

' Map a network drive 
' Usage
'    cscript MapDrive.vbs drive fileshare //NoLogo
'    cscript MapDrive.vbs H: \\MyServer\MyShare //NoLogo
'
' This script will remove any existing drive map to the same drive letter
' including persistent or remembered connections (Q303209)

Option Explicit
Dim objNetwork, objDrives, objReg, i
Dim strLocalDrive, strRemoteShare, strShareConnected, strMessage
Dim bolFoundExisting, bolFoundRemembered
Const HKCU = &H80000001

' Check both parameters have been passed 
If WScript.Arguments.Count < 2 Then
 wscript.echo "Usage: cscript MapDrive.vbs drive fileshare //NoLogo"
  WScript.Quit(1)
End If

strLocalDrive = UCase(Left(WScript.Arguments.Item(0), 2))
strRemoteShare = WScript.Arguments.Item(1)
bolFoundExisting = False

' Check parameters passed make sense
If Right(strLocalDrive, 1) <> ":" OR Left(strRemoteShare, 2) <> "\\" Then
 wscript.echo "Usage: cscript MapDrive.vbs drive fileshare //NoLogo"
  WScript.Quit(1)
End If

wscript.echo " - Mapping: " + strLocalDrive + " to " + strRemoteShare

Set objNetwork = WScript.CreateObject("WScript.Network")

' Loop through the network drive connections and disconnect any that match strLocalDrive
Set objDrives = objNetwork.EnumNetworkDrives
If objDrives.Count > 0 Then
  For i = 0 To objDrives.Count-1 Step 2
    If objDrives.Item(i) = strLocalDrive Then
      strShareConnected = objDrives.Item(i+1)
      objNetwork.RemoveNetworkDrive strLocalDrive, True, True
      i=objDrives.Count-1
      bolFoundExisting = True
    End If
  Next
End If

' If there's a remembered location (persistent mapping) delete the associated HKCU registry key
If bolFoundExisting <> True Then
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
  objReg.GetStringValue HKCU, "Network\" & Left(strLocalDrive, 1), "RemotePath", strShareConnected
  If strShareConnected <> "" Then
    objReg.DeleteKey HKCU, "Network\" & Left(strLocalDrive, 1)
    Set objReg = Nothing
    bolFoundRemembered = True
  End If
End If

'Now actually do the drive map (not persistent)
Err.Clear
On Error Resume Next
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False

'Error traps
If Err <> 0 Then
  Select Case Err.Number
    Case -2147023694
      'Persistent connection so try a second time
      On Error Goto 0
      objNetwork.RemoveNetworkDrive strLocalDrive, True, True
      objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False
      WScript.Echo "Second attempt to map drive " & strLocalDrive & " to " & strRemoteShare
    Case Else
      On Error GoTo 0
      WScript.Echo " - ERROR: Failed to map drive " & strLocalDrive & " to " & strRemoteShare
  End Select
  Err.Clear
End If

Set objNetwork = Nothing

来自http://ss64.com/vb/syntax-mapdrive.html

答案 1 :(得分:0)

我之前就是这样做的:

dim objNet, strLocal, strPath, fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set objNet = WScript.CreateObject("WScript.Network")
'Name the drives and their paths
strLocal = Array("H:","M:")
strPath = Array("\\Full\Path\Number1", _
    \\Full\Path\Number2")
'Loop to check if they are mapped, map it if they are not
For i = LBound(strLocal) To UBound(strLocal)
    If fso.FolderExists(strLocal(i)) = True Then
        wscript.echo(strLocal(i) & " Mapped")
    Else
        objNet.MapNetworkDrive strLocal(i), strPath(i), False
        wscript.echo(strLocal(i) & " Re-mapped")
    End If
Next
'Wrap up the script
WScript.Echo("")
WScript.Echo("Mapping Completed")
WScript.Sleep(2000)
'Keep the command prompt open long enough to see that it is completed
Set fso=Nothing
Set objNet=Nothing

基本上,它会检查驱动器是否已经映射,如果没有,则会映射它。我将此添加到我的启动文件夹中,因为我在重新启动时不断让我的公司网络驱动器丢失连接。