[英]Getting the logged in user when a program is executed as Administrator
當我的程序使用msiexec.exe安裝時,它將向任務計划程序中添加一個任務,以檢查它是否正在運行,如果沒有運行則啟動(這是一種售貨亭方案)。
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
string user = windowsIdentity.Name
不幸的是,我的任務被添加為SYSTEM
,當它運行時將無法訪問UserEnvironment。 有誰知道一種獲取發出該命令的用戶而不是運行msiexec.exe的用戶的方法嗎?
如果需要在安裝過程中進行檢查-例如,可以在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.