tciAgnt Posted August 9, 2018 Posted August 9, 2018 Hi all, I am trying to write a script which will run an elevated cmd and runs a specific program under a regular user profile (domain environment). The program must be run under the logged in user's profile. I found this code to obtain the logged in username in the elevated cmd: Func _GetUsername() Local $aResult = DllCall("Wtsapi32.dll", "int", "WTSQuerySessionInformationW", "int", 0, "dword", -1, "int", 5, "dword*", 0, "dword*", 0) If @error Or $aResult[0] = 0 Then Return SetError(1, 0, 0) Local $sUsername = BinaryToString(DllStructGetData(DllStructCreate("byte[" & $aResult[5] & "]", $aResult[4]), 1), 2) DllCall("Wtsapi32.dll", "int", "WTSFreeMemory", "ptr", $aResult[4]) Return $sUsername EndFunc And this is how I run the elevated cmd: When I try to add the full path to the MsgBox command (just to test if I get the full path), it will only display the path ending with the username: MsgBox(1, "", "C:\users\" & _GetUsername() & "\Appdata\Roaming\Microsoft") Output: C:\users\loggedinusername I tried several similar solutions but can't get anything to display after the _GetUsername() func. What I want to achieve: Run a program located in the logged in user's Appdata folder, which requires elevation. The %userprofile% parameter will give the administrator's username, that's why I need this _GetUsername() part. Do you have any idea what I am doing wrong/missing? Thank you. Best regards tciAgnt
ajag Posted August 9, 2018 Posted August 9, 2018 try this Return StringLeft($sUsername,StringLen($sUsername)-1) when returning _GetUsername() tciAgnt 1 Rule #1: Always do a backup Rule #2: Always do a backup (backup of rule #1)
tciAgnt Posted August 9, 2018 Author Posted August 9, 2018 @ajag Thank you very much, it is now working!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now