windows 映射网络驱动器的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/563028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:04:44  来源:igfitidea点击:

script to map network drive

windowslanguage-agnosticscripting

提问by Dónal

I want to be able to connect to a (wifi) network hard drive from my laptop, but only occasionally. If I use the "Map a network drive" command in WinXP explorer, I have to put in the drive's IP address and name, then the router name and its password. Too much to remember!

我希望能够从我的笔记本电脑连接到(wifi)网络硬盘驱动器,但只是偶尔。如果我在 WinXP 资源管理器中使用“映射网络驱动器”命令,我必须输入驱动器的 IP 地址和名称,然后是路由器名称和它的密码。太多记不住了!

I'm looking for a way of scripting this activity (in any language), something like:

我正在寻找一种编写此活动脚本的方法(以任何语言),例如:

map Z: \10.0.1.1\DRIVENAME "ROUTERNAME\PW"

I don't particularly care what language the script is written in. BTW, I'm aware of the DOS 'subst' command, but I don't think I can use that in this case because of the password protection.

我并不特别关心脚本是用什么语言编写的。顺便说一句,我知道 DOS 的“subst”命令,但由于密码保护,我认为在这种情况下我不能使用它。

回答by Mike

use the net usecommand:

使用net use命令:

net use Z: \10.0.1.1\DRIVENAME

Edit 1:Also, I believe the password should be simply appended:

编辑1:另外,我相信密码应该简单地附加:

net use Z: \10.0.1.1\DRIVENAME PASSWORD

You can find out more about this command and its arguments via:

您可以通过以下方式找到有关此命令及其参数的更多信息:

net use ?

Edit 2:As Tomalak mentioned in comments, you can later un-map it via

编辑 2:正如 Tomalak 在评论中提到的,您稍后可以通过

net use Z: \delete

回答by Tomalak

Does this not work (assuming "ROUTERNAME" is the user name the router expects)?

这不起作用(假设“ROUTERNAME”是路由器期望的用户名)?

net use Z: "\10.0.1.1\DRIVENAME" /user:"ROUTERNAME" "PW"

Alternatively, you can use use a small VBScript:

或者,您可以使用一个小的 VBScript:

Option Explicit
Dim u, p, s, l
Dim Network: Set Network= CreateObject("WScript.Network")

l = "Z:"
s = "\10.0.1.1\DRIVENAME"

u = "ROUTERNAME"
p = "PW"

Network.MapNetworkDrive l, s, False, u, p

回答by JohnB

Tomalak'sanswer worked great for me (+1)

Tomalak 的回答对我很有用(+1)

I only needed to make alter it slightly for my purposes, and I didn't need a password - it's for corporate domain:

我只需要为了我的目的稍微改变它,我不需要密码 - 它是用于公司域的:

Option Explicit
Dim l: l = "Z:"
Dim s: s = "\10.10.10.1\share"
Dim Network: Set Network = CreateObject("WScript.Network")
Dim CheckDrive: Set CheckDrive = Network.EnumNetworkDrives()
Dim DriveExists: DriveExists = False
Dim i

For i = 0 to CheckDrive.Count - 1
  If CheckDrive.Item(i) = l Then
    DriveExists = True
  End If
Next

If DriveExists = False Then
  Network.MapNetworkDrive l, s, False
Else
  MsgBox l + " Drive already mapped"
End If

Or if you want to disconnect the drive:

或者,如果您想断开驱动器的连接:

For i = 0 to CheckDrive.Count - 1
  If CheckDrive.Item(i) = l Then 
    WshNetwork.RemoveNetworkDrive CheckDrive.Item(i)
  End If
Next

回答by C. Ross

Why not map the network drive but deselect "Reconnect at logon"? The drive will only connect when you try to access it. Note that some applications will fail if they point to it, but if you're accessing files directly through Windows Explorer this works great.

为什么不映射网络驱动器而是取消选择“登录时重新连接”?只有当您尝试访问驱动器时,驱动器才会连接。请注意,某些应用程序如果指向它就会失败,但如果您直接通过 Windows 资源管理器访问文件,这将非常有效。

回答by Eric Petroelje

Try the net usecommand

试试net use命令

回答by 166_MMX

Here a JScript variant of JohnB's answer

这里的一个JScript变种JohnB答案

// Below the MSDN page for MapNetworkDrive Method with link and in case if Microsoft breaks it like every now and then the path to the documentation of now.
// https://msdn.microsoft.com/en-us/library/8kst88h6(v=vs.84).aspx
// MSDN Library -> Web Development -> Scripting -> JScript and VBScript -> Windows Scripting -> Windows Script Host -> Reference (Windows Script Host) -> Methods (Windows Script Host) -> MapNetworkDrive Method

var WshNetwork = WScript.CreateObject('WScript.Network');
function localNameInUse(localName) {
    var driveIterator = WshNetwork.EnumNetworkDrives();
    for (var i=0, l=driveIterator.length; i < l; i += 2) {
        if (driveIterator.Item(i) == localName) {
            return true;
        }
    }
    return false;
}

function mount(localName, remoteName) {
    if (localNameInUse(localName)) {
        WScript.Echo('"' + localName + '" drive letter already in use.');
    } else {
        WshNetwork.MapNetworkDrive(localName, remoteName);
    }
}

function unmount(localName) {
    if (localNameInUse(localName)) {
        WshNetwork.RemoveNetworkDrive(localName);
    }
}