简体   繁体   中英

MSI executing VB.net application, current user is returning the system user and not the logged on user

My VB.net written application is being executed by an MSI file, and I need to get the currently logged on user (who is running the MSI). This is because I am importing xml files into the task scheduler and without the correct usersname, there is a mapping error. Currently, because the application is being run through the MSI or windows installer the System user is being used. This is causing a mapping error so I was wondering if there is any other way to find the logged on user.

MsgBox(Environment.UserName)

Dim WSHNetwork = CreateObject("WScript.Network")
MsgBox(WSHNetwork.Username)

Both message boxes return "SYSTEM", whereas I need it to return the actual logged on user.

You could potentially try using WMI:

Dim username, objItem
Dim objWMIService  : Set objWMIService = GetObject( "winmgmts:\\.\root\cimv2" )
Dim colItems  : Set colItems = objWMIService.ExecQuery( "Select * from Win32_ComputerSystem" )

For Each objItem in colItems
    username = objItem.UserName
    if (instr(username ,"\") > 0) Then
        username = Split(username, "\")(1)
    end if   
Next
msgbox username

Or Quser:

Dim strCmd : strCmd = "cmd /q /c for /f ""skip=1 tokens=1"" %a in ('quser console') do @echo %a"
username = CreateObject("WScript.Shell").Exec(strCmd).StdOut.ReadAll()
username = Right(username,Len(username)-1)
msgbox username

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM