windows 无需以管理员身份运行即可映射到网络驱动器的 BAT 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23550814/
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 11:34:09  来源:igfitidea点击:

BAT file to map to network drive without running as admin

windowsbatch-filedrive

提问by Bhetzie

I'm trying to create a .bat file that will map to a network drive when it is clicked (it would be even better if it could connect automatically on login if connected to the network, otherwise do not connect)

我正在尝试创建一个 .bat 文件,该文件将在单击时映射到网络驱动器(如果连接到网络,它可以在登录时自动连接会更好,否则不要连接)

What I have so far is:

到目前为止我所拥有的是:

net use P: "\server\foldername\foldername"

Is there a way that I can create this so the users will not have to right click and run as an administrator? I would like it if they could just click the .bat file and it will map for them.

有没有一种方法可以创建它,这样用户就不必右键单击并以管理员身份运行?如果他们可以单击 .bat 文件并且它会为他们映射,我会很高兴。

采纳答案by Bhetzie

I just figured it out! What I did was I created the batch file like I had it originally:

我刚刚想通了!我所做的是创建批处理文件,就像我最初拥有的一样:

net use P: "\server\foldername\foldername"

I then saved it to the desktop and right clicked the properties and checked run as administrator. I then copied the file to C:\Users\"TheUser"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

然后我将它保存到桌面并右键单击属性并选中以管理员身份运行。然后我将文件复制到 C:\Users\"TheUser"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Where "TheUser" was the desired user I wanted to add it to.

其中“TheUser”是我想要将其添加到的所需用户。

回答by Sunny

Save below in a test.batand It'll work for you:

将下面保存在 a 中test.bat,它将为您工作:

@echo off

net use Z: \server\SharedFolderName password /user:domain\Username /persistent:yes

/persistent:yesflag will tell the computer to automatically reconnect this share on logon. Otherwise, you need to run the script again during each boot to map the drive.

/persistent:yes标志将告诉计算机在登录时自动重新连接此共享。否则,您需要在每次引导期间再次运行脚本以映射驱动器。

For Example:

例如:

net use Z: \WindowsServer123\g$ P@ssw0rd /user:Mynetdomain\Sysadmin /persistent:yes

回答by Henry

@echo off
net use z: /delete
cmdkey /add:servername /user:userserver /pass:userstrongpass

net use z: \servername\userserver /savecred /persistent:yes
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"

echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "%USERPROFILE%\Desktop\userserver_in_server.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "Z:\" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%

cscript /nologo %SCRIPT%
del %SCRIPT%

回答by Airton J. Colombini

This .vbs code creates a .bat file with the current mapped network drives. Then, just put the created file into the machine which you want to re-create the mappings and double-click it. It will try to create all mappings using the same drive letters (errors can occur if any letter is in use). This method also can be used as a backup of the current mappings. Save the code bellow as a .vbs file (e.g. Mappings.vbs) and double-click it.

此 .vbs 代码使用当前映射的网络驱动器创建一个 .bat 文件。然后,只需将创建的文件放入要重新创建映射的机器中并双击它。它将尝试使用相同的驱动器号创建所有映射(如果正在使用任何字母,则可能会发生错误)。此方法也可用作当前映射的备份。将下面的代码另存为 .vbs 文件(例如 Mappings.vbs)并双击它。

' ********** My Code **********
Set wshShell = CreateObject( "WScript.Shell" )

' ********** Get ComputerName
strComputer = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

' ********** Get Domain 
sUserDomain = createobject("wscript.network").UserDomain

Set Connect = GetObject("winmgmts://"&strComputer)
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDrives = WshNetwork.EnumNetworkDrives
Set oPrinters = WshNetwork.EnumPrinterConnections

' ********** Current Path
sCurrentPath = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

' ********** Blank the report message
strMsg = ""

' ********** Set objects 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")
Set objWbem = GetObject("winmgmts:")
Set objRegistry = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv")

' ********** Get UserName
sUser = CreateObject("WScript.Network").UserName

' ********** Print user and computer
'strMsg = strMsg & "    User: " & sUser & VbCrLf
'strMsg = strMsg & "Computer: " & strComputer & VbCrLf & VbCrLf

strMsg = strMsg & "###  COPIED FROM " & strComputer & " ###" & VbCrLf& VbCrLf
strMsg = strMsg & "@echo off" & vbCrLf

For i = 0 to oDrives.Count - 1 Step 2
    strMsg = strMsg & "net use " & oDrives.Item(i) & " " & oDrives.Item(i+1) & " /user:" & sUserDomain & "\" & sUser & " /persistent:yes" & VbCrLf
Next
strMsg = strMsg & ":exit" & VbCrLf
strMsg = strMsg & "@pause" & VbCrLf

' ********** write the file to disk.
strDirectory = sCurrentPath 
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
    ' Procede
Else
    Set objFolder = objFSO.CreateFolder(strDirectory)
End if

' ********** Calculate date serial for filename **********
intMonth = month(now)
if intMonth < 10 then
    strThisMonth = "0" & intMonth
else
    strThisMonth = intMOnth
end if
intDay = Day(now)
if intDay < 10 then
    strThisDay = "0" & intDay
else
    strThisDay = intDay
end if
strFilenameDateSerial = year(now) & strThisMonth & strThisDay
    sFileName = strDirectory & "\" & strComputer & "_" & sUser & "_MappedDrives" & "_" & strFilenameDateSerial & ".bat"
    Set objFile = objFSO.CreateTextFile(sFileName,True) 
objFile.Write strMsg & vbCrLf

' ********** Ask to view file
strFinish = "End: A .bat was generated. " & VbCrLf & "Copy the generated file  (" & sFileName & ")  into the machine where you want to recreate the mappings and double-click it." & VbCrLf & VbCrLf 
MsgBox(strFinish)

回答by guanyuan he

I tried to create a mapped network driver via 'net use' with admin privilege but failed, it does not show. And if I add it through UI, it disappeared after reboot, now I made that through powershell. So, I think you can run powershell scripts from a .bat file, and the script is

我试图通过具有管理员权限的“网络使用”创建映射网络驱动程序,但失败了,它没有显示。如果我通过 UI 添加它,它会在重启后消失,现在我通过 powershell 实现了。因此,我认为您可以从 .bat 文件运行 powershell 脚本,并且该脚本是

New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\Server01\Public"

New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\Server01\Public"

add -persistat the end, you will create a persisted mapped network drive

添加-persist在最后,您将创建一个持久映射网络驱动器

New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\Server01\Scripts" -Persist

New-PSDrive -Name "P" -PSProvider "FileSystem" -Root "\\Server01\Scripts" -Persist

for more details, refer New-PSDrive - Microsoft Docs

有关更多详细信息,请参阅New-PSDrive - Microsoft Docs