如何以“管理员”身份运行vbscript,但可以访问当前登录用户VBSCRIPT的APPDATA文件夹 [英] How can I run the vbscript as the 'Administrator' but access on APPDATA folder of current logon user VBSCRIPT

查看:137
本文介绍了如何以“管理员”身份运行vbscript,但可以访问当前登录用户VBSCRIPT的APPDATA文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试访问APPDATA文件夹,该文件夹可以完美地使用以下代码(以当前登录用户身份运行):

I tried to access on the APPDATA folder, which does function with the following code perfectly (run as the current logon user):

Set objShellApp  = CreateObject("Shell.Application")

Const ssfAPPDATA = &H1A
sAppDataPath = objShellApp.NameSpace(ssfAPPDATA).Self.Path

sAppDataPath的结果是:C:\Users\ Peter \AppData\Roaming

The result of sAppDataPath is: C:\Users\ Peter \AppData\Roaming

现在的问题是,我必须以管理员帐户运行脚本。而且,如果我在客户端上以管理员身份运行此脚本,则结果始终如下:
C:\Users\ Administrator \AppData\Roaming

Now the problem is, I have to run the Script as the Administrator account. And if I run this script as the Administrator on Clients, than the result is allways following: C:\Users\ Administrator \AppData\Roaming

我该如何更改?我想要他使用当前登录用户的Appdata文件夹路径。尽管以管理员身份运行脚本。而且由于某些许可,我必须以管理员身份运行脚本。

How can I change that? I want, that he should take the Appdata folder path of the current logon user. In spite of running the script as the Administrator. And i have to run the script as Administrator because of some permission.

我怎么能意识到这一点?

How can I realize that?

推荐答案

仅靠VBS无法做到这一点。

Cannot do this with a VBS alone.

您需要创建一个批处理文件来传递此 %APPDATA%环境变量作为Argument进入脚本,然后修改VBS脚本以处理此参数。

You would need to create a batch file to pass this %APPDATA% environment variable into the script as Argument, then modify the VBS script to process this argument(s).

以下示例:

更新:

如果您不介意人们可以打开该批处理文件并知道您要以运行身份运行的用户帐户的密码,请获取 来自MS 的psexec.exe ,其中包含更新的批处理文件内容(假设psexec.exe存储在 C:\SysInt\ 中):

If you do not mind people being able to open the batch file and know the password of the user account you intend to Run As, get psexec.exe from MS with the updated batch file contents (assuming psexec.exe stored in C:\SysInt\):

VBS文件

Dim sTxt
sTxt = WScript.Arguments.Count & " arguments passed into vbs:"
sTxt = sTxt & JoinArgs
wscript.echo sTxt

Function JoinArgs()
    Dim sTmp
    sTmp = ""
    For Each oArg In Wscript.Arguments
        sTmp = sTmp & vbCrLf & oArg
    Next
    JoinArgs = sTmp
End Function

批处理文件(已更新)

@echo off
C:\SysInt\psexec.exe -u %computername%\administrator -p AccountPassword -e wscript.exe "c:\debug\vbs\test.vbs" %appdata%

这篇关于如何以“管理员”身份运行vbscript,但可以访问当前登录用户VBSCRIPT的APPDATA文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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