如何在VBScript中获得%username%?

时间:2017-03-04 09:15:12

标签: windows networking vbscript

我正在尝试隐藏域用户的共享文件夹的网络路径。 (Windows Server 2012)我在搜索网络驱动器标签时发现了这个脚本:

Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName

strDriveLetter = "H:"
strRemotePath = "\\servername\sharedfoldername$\"
strNewName = "Save Your Files Here"

'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

我的网络路径如下:

strRemotePath = "\\servername\sharedfoldername1$\%username%"
strRemotePath = "\\servername\sharedfoldername2$\%username%"
strRemotePath = "\\servername\sharedfoldername5$\%username%"
strRemotePath = "\\servername\sharedfoldernameNNN$\%username%"

当我插入%username%时,脚本不会运行。

请指导我如何修改将按照我的要求运行的脚本。

1 个答案:

答案 0 :(得分:4)

您可以在路径字符串中expand environment variables

strRemotePath = "\\servername\sharedfoldername1$\%username%"

Set sh = CreateObject("WScript.Shell")
WScript.Echo sh.ExpandEnvironmentStrings(strRemotePath)

或者您可以从共享中build the path以及您已拥有的UserName WshNetwork属性:

share = "\\servername\sharedfoldername1$"

Set fso = CreateObject("Scripting.FileSystemObject")
WScript.Echo fso.BuildPath(share, objNetwork.UserName)