使用 .bat 腳本覆蓋已對應的磁碟機名稱?

使用 .bat 腳本覆蓋已對應的磁碟機名稱?

我有以下批次腳本,它成功地為我們的用戶映射了一些驅動器:

@echo off
net use * /delete /yes
net use x: \\192.168.1.52\xrays
net use s: \\192.168.1.52\common
net use p: \\192.168.1.52\public
net use o: \\192.168.1.52\office
net use y: \\192.168.1.52\drives
EXIT

映射驅動器的名稱取自共享名本身。但是,如果我可以將名稱覆蓋為對使用者更有用的名稱,那將非常方便。

我在網路上查看了很多帶有 net use 命令範例的文檔,但我只能看到諸如憑證之類的選項,但與命名無關。

該腳本在 Windows XP 和 Windows 7 工作站上執行。

任何幫助,將不勝感激。

答案1

有一種方法可以從命令列執行此操作,而無需使用 VBScript。您可以使用命令編輯註冊表reg add。恕我直言,這樣做比使用 VBScript 更改標籤更好,因為它不會將標籤與磁碟機號碼關聯起來,而是會將標籤與共用關聯起來。因此,如果最終用戶稍後斷開連接X:並手動安裝xrays共享以表示R:驅動器,則標籤仍然會正確顯示(無論您在腳本中分配給它什麼)。

您要寫入的鍵的HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\子鍵是共用路徑,所有反斜線都替換為井號 (#)。

筆記: 我尚未測試您應該如何處理其中包含空格(甚至井號符號)的共用名稱。

@echo off

net use * /delete /yes

:: Set the label in the registry
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#xrays /v _LabelFromReg /t REG_SZ /f /d "X-Rays"
:: Map the drive
net use x: \\192.168.1.52\xrays

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#common /v _LabelFromReg /t REG_SZ /f /d "Common"
net use s: \\192.168.1.52\common

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#public /v _LabelFromReg /t REG_SZ /f /d "Public"
net use p: \\192.168.1.52\public

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#office /v _LabelFromReg /t REG_SZ /f /d "Office"
net use o: \\192.168.1.52\office

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#drives /v _LabelFromReg /t REG_SZ /f /d "Drives"
net use y: \\192.168.1.52\drives

EXIT

答案2

無法僅使用命令來執行此操作net use(請參閱文件),但是有一種方法可以使用 vb 腳本來完成此操作,如 Guy Thomas 在computerperformance.co.uk 上所描述的那樣這裡

以防萬一他的傢伙的網站後來消失了,這是他的腳本副本:

' NameDrive.vbs
' VBScript to map a network drive.
' Authors Guy Thomas and Barry Maybury
' Version 1.4 - April 2010
' ----------------------------------------'
'
Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName
'
strDriveLetter = "W:"
strRemotePath = "\\192.168.1.2\example\sharename"
strNewName = "Example Readable Label"

' Section to map the network drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

Wscript.Echo "Check : "& strDriveLetter & " for " & strNewName
WScript.Quit

' End of Example VBScript.

筆記:

  • 如果W:不適合您,請嘗試W:\(使用斜線)
  • 此方法將設定驅動器盤符標籤永久,即如果您稍後將另一個共用連接到相同磁碟機盤符,則新共享也將獲得舊標籤。可以透過在連接共享時始終使用此腳本或刪除註冊表中的項目以恢復正常行為來解決此問題。

恢復正常行為:

  • 啟動 regedit,然後按一下Edit(選單)-> Find->Example Readable Label
  • 或在 regedit 中導航至HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2->_LabelFromReg
  • 只需刪除該值 - 將其留空。結果是未來的磁碟機映射將恢復到傳統的映射樣式。

所有這些在蓋伊的網站上都有更詳細的描述。

答案3

@echo off
echo --------------------------delete map drive all------------------------
net use * /delete /yes
echo ------------------create drive --------------------------------
net use m: \\172.16.0.136\Source /user:aleg\masr masr2006*
net use n: \\172.16.0.136\scanner_bat_test /user:alwq\4288044 masr2006*
echo ---------------------------------------------------

EXIT

相關內容