使用WMI获取当前登录的用户 [英] Using WMI to get Current Logged On User

查看:167
本文介绍了使用WMI获取当前登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WMI获取远程计算机的当前登录用户.我查询W32_ComputerSystem并查找UserName的值.这种方法在我的本地系统上似乎可以正常工作,但在远程系统上却不能.该查询返回> 50个属性,而不是UserName.返回的属性在下面列出.我用来拨打电话的用户名具有DomainAdmin权限.

I am attempting to use WMI to obtain the current logged on user of a remote computer.  I query W32_ComputerSystem and look for the value of UserName.  This method seems to work fine on my local system but not on a remote system.  The query returns >50 properties, just not UserName.  The properties returned are listed below.  The Username that I am using to make the call has DomainAdmin rights.

任何帮助都会得到极大的帮助!

Any help would be greatly appriciated!

Vincent Fournier

Vincent Fournier

代码:

public string GetW32_ComputerInfo( 字符串 strComputer, 字符串 strUserName, 字符串 strPassword)

public string GetW32_ComputerInfo(string strComputer, string strUserName, string strPassword)

{

字符串 strReturnValue = null ;

string strReturnValue = null;

ConnectionOptions options = new ConnectionOptions ();

ConnectionOptions options = new ConnectionOptions();

如果 (strUserName!= null && strUserName!= "" )

if (strUserName != null && strUserName != "")

{

options.Username = strUserName;

options.Username = strUserName;

options.Password = strPassword;

options.Password = strPassword;

options.Impersonation = System.Management. ImpersonationLevel .Impersonate;

options.Impersonation = System.Management.ImpersonationLevel.Impersonate;

options.Authentication = System.Management. AuthenticationLevel .默认;

options.Authentication = System.Management.AuthenticationLevel.Default;

options.EnablePrivileges = true ;

options.EnablePrivileges = true;

}

ManagementScope scope = new ManagementScope ( @'"\\" + strComputer + @" \ root \ cimv2" > ,选项);

ManagementScope scope = new ManagementScope(@"\\" + strComputer + @"\root\cimv2", options);

scope.Connect();

scope.Connect();

ObjectQuery query = new ObjectQuery ( " SELECT * FROM Win32_ComputerSystem" );

ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_ComputerSystem");

ManagementObjectSearcher searcher = ManagementObjectSearcher (范围,查询);

ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

ManagementObjectCollection queryCollection = searcher.Get();

ManagementObjectCollection queryCollection = searcher.Get();

foreach ( ManagementObject m queryCollection)

foreach (ManagementObject m in queryCollection)

{

strReturnValue = m.GetText( TextFormat .Mof);

strReturnValue = m.GetText(TextFormat.Mof);

}

返回 strReturnValue;

return strReturnValue;

}

结果:

Win32_ComputerSystem的实例

instance of Win32_ComputerSystem

{

AdminPasswordStatus = 3;

AdminPasswordStatus = 3;

AutomaticResetBootOption = FALSE;

AutomaticResetBootOption = FALSE;

AutomaticResetCapability = TRUE;

AutomaticResetCapability = TRUE;

BootROMSupported = TRUE;

BootROMSupported = TRUE;

BootupState ="Normal";

BootupState = "Normal";

标题="UNISYS-13744751";

Caption = "UNISYS-13744751";

ChassisBootupState = 3;

ChassisBootupState = 3;

CreationClassName ="Win32_ComputerSystem";

CreationClassName = "Win32_ComputerSystem";

CurrentTimeZone = -480;

CurrentTimeZone = -480;

DaylightInEffect = FALSE;

DaylightInEffect = FALSE;

说明="AT/AT兼容";

Description = "AT/AT COMPATIBLE";

Domain ="FFE";

Domain = "FFE";

DomainRole = 1;

DomainRole = 1;

FrontPanelResetStatus = 3;

FrontPanelResetStatus = 3;

InfraredSupported =否;

InfraredSupported = FALSE;

KeyboardPasswordStatus = 3;

KeyboardPasswordStatus = 3;

制造商="Compaq";

Manufacturer = "Compaq";

Model =市场名称";

Model = "Marketing Name";

名称="UNISYS-13744751";

Name = "UNISYS-13744751";

NetworkServerModeEnabled = TRUE;

NetworkServerModeEnabled = TRUE;

NumberOfProcessors = 1;

NumberOfProcessors = 1;

OEMStringArray = {" "};

OEMStringArray = {" "};

PauseAfterReset ="-1";

PauseAfterReset = "-1";

PowerOnPasswordStatus = 3;

PowerOnPasswordStatus = 3;

PowerState = 0;

PowerState = 0;

PowerSupplyState = 3;

PowerSupplyState = 3;

PrimaryOwnerName ="20世纪福克斯";

PrimaryOwnerName = "20th Century Fox";

ResetCapability = 1;

ResetCapability = 1;

ResetCount = -1;

ResetCount = -1;

ResetLimit = -1;

ResetLimit = -1;

角色= {"LM_Workstation","LM_Server","NT"};

Roles = {"LM_Workstation", "LM_Server", "NT"};

状态=确定";

SystemStartupDelay = 2;

SystemStartupDelay = 2;

SystemStartupOptions = {"\" Microsoft Windows 2000 Professional \"/fastdetect","\" Microsoft Windows 2000恢复控制台\" /cmdcons"};

SystemStartupOptions = {"\"Microsoft Windows 2000 Professional\" /fastdetect", "\"Microsoft Windows 2000 Recovery Console\" /cmdcons"};

SystemStartupSetting = 0;

SystemStartupSetting = 0;

SystemType =基于X86的PC";

SystemType = "X86-based PC";

ThermalState = 3;

ThermalState = 3;

TotalPhysicalMemory ="259506176";

TotalPhysicalMemory = "259506176";

WakeUpType = 6;

WakeUpType = 6;

};

};

推荐答案

首先,我建议下载,运行,连接到远程计算机,然后运行代码并查看其内容拿起:

firstly I would suggest downloading this, running it, connecting to a remote computer then run the code and see what it picks up:

http://www.microsoft.com/downloads/details.aspx?familyid=2CC30A64 -EA15-4661-8DA4-55BBC145C30E& displaylang = zh-CN

 

第二次我可以在计算机上执行此操作,但是我确实具有管理员访问权限(请确保您也具有管理员权限,因为这可能是不检索信息的原因之一)

secondly I have been able to do this fine on my computers but I did have admin access (be sure you have too as this may be one reason for not retrieving the info)

ManagementScope theScope = 新的 ManagementScope (" \\\\ ComputerName \\ root \\ cimv2 " );

ManagementScope theScope = new ManagementScope("\\\\ComputerName\\root\\cimv2");

ObjectQuery theQuery = ObjectQuery ( "SELECT  username FROM Win32_ComputerSystem" );

ObjectQuery theQuery = new ObjectQuery("SELECT username FROM Win32_ComputerSystem");

ManagementObjectSearcher theSearcher = ManagementObjectSearcher (theScope,theQuery);

ManagementObjectSearcher theSearcher = new ManagementObjectSearcher(theScope, theQuery);

ManagementObjectCollection theCollection = theSearcher.Get();

ManagementObjectCollection theCollection = theSearcher.Get();

foreach ( ManagementObject theCurObject theCollection中)

foreach (ManagementObject theCurObject in theCollection)

{

MessageBox.Show(theCurObject ["username"].ToString());

MessageBox.Show(theCurObject["username"].ToString());

}


这篇关于使用WMI获取当前登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆