如何在VBScript中获取%username%? [英] How to get %username% in VBScript?

查看:111
本文介绍了如何在VBScript中获取%username%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向域用户隐藏共享文件夹的网络路径. (Windows Server 2012)在搜索网络驱动器标签时找到了该脚本:

I am trying to hide network path of shared folders from domain users. (Windows Server 2012) I have found this script while searching for network drive labeling:

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%时,脚本不会运行.

When I insert %username%, the script does not run.

请指导我如何修改将根据我的要求运行的脚本.

Kindly guide me how to modify this script that will run as per my requirements.

推荐答案

您可以在路径字符串中扩展环境变量:

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

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

或者您可以根据共享和< UserName 属性.microsoft.com/en-us/library/s6wt333f.aspx"rel =" noreferrer> WshNetwork 您已经拥有:

or you can build the path from the share and the UserName property of the WshNetwork that you already have:

share = "\\servername\sharedfoldername1$"

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

这篇关于如何在VBScript中获取%username%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆