probably at the beach
probably at the beach

Reputation: 15217

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?

Upvotes: 1

Views: 116

Answers (1)

Igor Shenderchuk
Igor Shenderchuk

Reputation: 708

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 

Upvotes: 1

Related Questions