Windows

logon.vbs 腳本未映射網路驅動器

  • September 22, 2010

我是創建 vbs 腳本以在 Windows 中映射網路驅動器的新手。出於某種原因,該腳本會執行,但在使用者登錄到域時不會映射任何網路驅動器。這是我正在使用的腳本。這非常簡單直接。

Option Explicit
Dim wshNetwork 

Set wshNetwork = CreateObject("WScript.Network")

wshNetwork.MapNetworkDrive "S:","\\server\shared"
wshNetwork.MapNetworkDrive "U:","\\server\" & wshNetwork.UserName
WScript.Quit

我究竟做錯了什麼?

嘗試:

wshNetwork.MapNetworkDrive "S:","\\server\shared", True
wshNetwork.MapNetworkDrive "U:","\\server\" & wshNetwork.UserName, True

我還添加了一個常式來刪除所有共享,以避免在映射驅動器之前出現“設備已在使用”錯誤。

wshNetwork.RemoveNetworkDrive "S:", True, True
wshNetwork.RemoveNetworkDrive "U:", True, True

wscript.sleep 300

這是我一直在使用的一個功能:

Function MapDrive(ByVal strDrive, ByVal strShare)
   ' Function to map network share to a drive letter.
   ' If the drive letter specified is already in use, the function
   ' attempts to remove the network connection.
   ' objFSO is the File System Object, with global scope.
   ' objNetwork is the Network object, with global scope.
   ' Returns True if drive mapped, False otherwise.

   Dim objDrive
   On Error Resume Next
   If (objFSO.DriveExists(strDrive) = True) Then
       Set objDrive = objFSO.GetDrive(strDrive)
       If (Err.Number <> 0) Then
           On Error GoTo 0
           MapDrive = False
           Exit Function
       End If
       If (objDrive.DriveType = 3) Then
           objNetwork.RemoveNetworkDrive strDrive, True, True
       Else
           MapDrive = False
           Exit Function
       End If
       Set objDrive = Nothing
   End If
   objNetwork.MapNetworkDrive strDrive, strShare
   If (Err.Number = 0) Then
       MapDrive = True
   Else
       Err.Clear
       MapDrive = False
   End If
   On Error GoTo 0
End Function

使用範例:

If (MapDrive("Z:", "\\yourserver\yourshare") = False) Then
   ' Do something because there was an error.        
End If

引用自:https://serverfault.com/questions/61297