BAT文件映射到网络驱动器而不以管理员身份运行

时间:2014-05-08 19:27:06

标签: windows batch-file drive

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

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

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

有没有办法可以创建这个,这样用户就不必右键单击并以管理员身份运行?如果他们只需单击.bat文件就可以映射它们。

5 个答案:

答案 0 :(得分:21)

test.bat中保存以下内容,它将对您有用:

@echo off

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

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

例如:

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

答案 1 :(得分:5)

我刚想通了!我所做的是我创建了批处理文件,就像我原来的那样:

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

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

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

答案 2 :(得分:2)

@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%

答案 3 :(得分:0)

我试图通过&net; net use'来创建映射的网络驱动程序。具有管理员权限但失败,它不显示。如果我通过UI添加它,它在重新启动后消失了,现在我通过PowerShell实现了这一点。 所以,我认为你可以从.bat文件运行powershell脚本,脚本是

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

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

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

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

答案 4 :(得分:0)

  

此.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)