简体   繁体   中英

Getting the logged in user when a program is executed as Administrator

When my program installs using msiexec.exe it adds a task to the task scheduler to check if it's running and start up if it's not (it's a kiosk scenario).

        WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
        string user = windowsIdentity.Name

Unfortunately my task is being added as SYSTEM which when run won't be able to access the UserEnvironment. Does anyone know of a way to get the user who originiated the command rather than the user msiexec.exe is running as?

If you need check it during installation process - you can use custom action on VBScript, for example, which checks logged users with help of WMI.

Option Explicit

Dim objReg, objWMI, colSessions

Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2") 

Set colSessions = objWMI.ExecQuery("Select * from Win32_LogonSession Where LogonType = 2 Or LogonType = 10") 
If colSessions.Count <> 0 Then 
    'Some users are logged into Windows.
    'For example set property 
    'Session.Property("SOMEONELOGGED") = "1"
End If 

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