简体   繁体   English

以管理员身份执行程序时获取登录用户

[英]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). 当我的程序使用msiexec.exe安装时,它将向任务计划程序中添加一个任务,以检查它是否正在运行,如果没有运行则启动(这是一种售货亭方案)。

        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. 不幸的是,我的任务被添加为SYSTEM ,当它运行时将无法访问UserEnvironment。 Does anyone know of a way to get the user who originiated the command rather than the user msiexec.exe is running as? 有谁知道一种获取发出该命令的用户而不是运行msiexec.exe的用户的方法吗?

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. 如果需要在安装过程中进行检查-例如,可以在VBScript上使用自定义操作,该操作将在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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 登录用户而不是运行程序C#,Windows的用户 - Getting logged in user not the user running the program C#, Windows 使用不是管理员组成员的用户登录时无法加载文件或程序集“Microsoft.CSharp” - Could not load file or assembly 'Microsoft.CSharp' when logged in with user which is not a member of administrator group 从管理员程序以用户身份启动程序 - Start Program as User From Administrator Program 获取登录用户信息 - Getting logged in user info 在Powershell中获取登录用户 - Getting the logged in user in Powershell 即使将app.manifest设置为以管理员身份运行程序,也可以获得“对路径的访问……被拒绝” - Getting “Access to path… denied” even when app.manifest is set to to run program as administrator Facebook Graph API在用户未登录时获取数据 - Facebook Graph API getting data when user not logged in 没有管理员权限 在具有管理员权限的登录用户上下文中启动桌面应用程序 - Without Administrator Privileges Launching Desktop Application in a logged in User context with Administrator Privileges 以管理员身份运行时出现“找不到文件”的程序错误 - Program errors with “File Not Found” when Run As Administrator 程序启动时如何请求管理员权限? - How to request administrator permissions when the program starts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM