How can I run the vbscript as the 'Administrator' but access on APPDATA folder of current logon user VBSCRIPT

狂风中的少年 提交于 2019-12-13 06:26:20

问题


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

The result of sAppDataPath is: C:\Users\ Peter \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

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?


回答1:


Cannot do this with a VBS alone.

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

Sample below:

UPDATE:

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 File

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

Batch File (UPDATED)

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


来源:https://stackoverflow.com/questions/21515854/how-can-i-run-the-vbscript-as-the-administrator-but-access-on-appdata-folder-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!