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

StackOverflow https://stackoverflow.com/questions/21515854

  •  06-10-2022
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top