任何人都可以帮助我使用以下 JScript 吗?
在 Vista 中,我试图获取本地计算机上用户帐户(当前用户除外)的“本地应用程序数据”路径,但遇到一些问题。谁能帮我看看下面的代码有什么问题吗?
var HKU = 0x80000003;
var username = "xyz";
//Loading registry hive of user xyz
var WshShell = new ActiveXObject("WScript.Shell");
var LoadHiveCmd = "REG LOAD " + "HKU" + "\\" + username + " \"" + "c:\\users\\xyz\\NTUSER.DAT" + "\"";
var oExec = WshShell.Exec(strLoadHiveCmd);
var oReg = GetObject("WinMgmts:/root/default:StdRegProv");
var profileRegPath = username + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
var method, inparams, outparams;
method = oReg.Methods_.Item("GetExpandedStringValue");
inparams = method.InParameters.SpawnInstance_();
inparams.hDefKey = HKU;
inparams.sSubKeyName = profileRegPath ;
inparams.sValueName = "Local AppData";
outparams = oReg.ExecMethod_(method.Name, inparams);
var appDataPath= outparams.sValue;
这里注册表中的 appDataPath 值是 %USERPROFILE%\AppData\Local
但我得到一个值 C:\Windows\system32\config\systemprofile\AppData\Local
我不明白值 c:\windows\system32\config\systemprofile
来自何处以及它如何替换 %USERPROFILE%
值。
In Vista, I am trying to get "Local AppData" path for an user account (other than current user) on a local machine but facing some issue. can anyone pls help me what is wrong with the below code.
var HKU = 0x80000003;
var username = "xyz";
//Loading registry hive of user xyz
var WshShell = new ActiveXObject("WScript.Shell");
var LoadHiveCmd = "REG LOAD " + "HKU" + "\\" + username + " \"" + "c:\\users\\xyz\\NTUSER.DAT" + "\"";
var oExec = WshShell.Exec(strLoadHiveCmd);
var oReg = GetObject("WinMgmts:/root/default:StdRegProv");
var profileRegPath = username + "\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
var method, inparams, outparams;
method = oReg.Methods_.Item("GetExpandedStringValue");
inparams = method.InParameters.SpawnInstance_();
inparams.hDefKey = HKU;
inparams.sSubKeyName = profileRegPath ;
inparams.sValueName = "Local AppData";
outparams = oReg.ExecMethod_(method.Name, inparams);
var appDataPath= outparams.sValue;
Here the appDataPath value in the registry is %USERPROFILE%\AppData\Local
But I am getting a value C:\Windows\system32\config\systemprofile\AppData\Local
I dont understand from where the value c:\windows\system32\config\systemprofile
is coming and how it replaced %USERPROFILE%
value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
USERPROFILE 是一个环境变量,将替换 %USERPROFILE% 以获取它在此计算机上的正确位置。它随着计算机的不同而变化。
要查看所有环境变量,请在命令 shell 上键入“set”,或转到“控制面板”>“设置”。 “系统设置”> “高级”>环境变量
USERPROFILE is a environment variable and will replace %USERPROFILE% to get it's correct location on this computer. it changes from computer to computer.
To see all environment variables type "set" on a command shell, or go to "Control Panel" > "System settings" > "Advanced" > Environment variables
GetExpandedStringValue
自动将注册表值数据中包含的任何环境变量替换为这些变量的实际值。最有可能的是,%USERPROFILE% 扩展为 C:\Windows\system32\config\systemprofile 而不是 C:\users\admin,因为 WMI 服务本身在本地系统帐户下运行。要使脚本正常工作,您需要执行以下操作:
使用
GetStringValue
而不是GetExpandedStringValue
来读取未展开的Local AppData
值,通过读取所需用户的个人资料路径来自
HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\
键的ProfileImagePath
值,执行字符串替换以替换 %USERPROFILE % 替换为配置文件路径。
您可以在我的回答中找到如何执行此操作的示例:
获取给定用户的特殊文件夹路径在 Jscript
您可能还想使用
WshShell.RegRead
而不是 WMI,因为它对 JScript 更友好。GetExpandedStringValue
automatically replaces any environment variables included in the registry value data with actual values of these variables. Most likely, %USERPROFILE% expands to C:\Windows\system32\config\systemprofile instead of C:\users\admin because the WMI service itself runs under Local System account.What you need to get your script working is to:
use
GetStringValue
instead ofGetExpandedStringValue
to read the unexpandedLocal AppData
value,get the profile path of the needed user by reading the
ProfileImagePath
value from theHKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\<user_SID>
key,do a string replace to substitute %USERPROFILE% with the profile path.
You can find an example of how to do this in this my answer:
Getting special Folder path for a given user in Jscript
You may also want to use
WshShell.RegRead
instead of WMI, because it's more JScript-friendly.