我正在尝试使用C#编写一个小命令行应用程序,它将提示用户名和密码,该用户名和密码将用于登录位于同一网络/域并启动本地会话的多个远程计算机。
我尝试连接到远程计算机并使用以下代码查询远程PC的操作系统信息:
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope(@"\\REMOTE_COMPUTER_NAME");
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}", m["csname"]);
Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
}
但是,这仅返回我正在处理的本地PC上的信息,而不是远程PC上的信息。
我是否忽略了这段代码?是否有适当的方法来完成我想要做的事情?
答案 0 :(得分:1)
我现在没有远程机器为您提供工作示例,但您可以试试这个。 advapi32.logonuser
示例:
[DllImport("advapi32.dll")]
public static extern bool LogonUser(string name, string domain, string pass,
int logType, int logpv, out IntPtr pht);
IntPtr ptr;
// LogonUser("username", "remotemachine", "password", 2, 0, out ptr);
LogonUser("username", "remotemachine", "password", 9, 0, out ptr);
WindowsIdentity windowsIdentity = new WindowsIdentity(ptr);
var impersonationContext = windowsIdentity.Impersonate();
// your code goes here...
impersonationContext.Undo();
此登录类型允许调用者克隆其当前令牌和 为出站连接指定新凭据。新登录 session具有相同的本地标识符,但使用不同的凭据 用于其他网络连接。注意:支持此登录类型 仅由LOGON32_PROVIDER_WINNT50登录提供程序提供。注意:Windows NT: 不支持此值。
http://www.pinvoke.net/default.aspx/advapi32.logonuser
修改
试一试cassia
ITerminalServicesManager manager = new TerminalServicesManager();
using (ITerminalServer server = manager.GetRemoteServer("servername"))
{
server.Open();
foreach (ITerminalServicesSession session in server.GetSessions())
{
Console.WriteLine("Hi there, " + session.UserAccount + " on session " + session.SessionId);
Console.WriteLine("It looks like you logged on at " + session.LoginTime +
" and are now " + session.ConnectionState);
}
}
答案 1 :(得分:0)
您必须使用ConnectionOptions并将其传递给ManagementScope
public void GetSystemInformation(string _yourDomain, string _hostName, string _userName, SecureString _password)
{
ManagementScope Scope = null;
string computerName = _hostName;
string userName = _userName;
SecureString password = _password;
ManagementObjectCollection collection = null;
try
{
SelectQuery query = new SelectQuery("SELECT * FROM Win32_OperatingSystem");
//string query = "SELECT * FROM Win32_NetworkAdapterConfiguration" + " WHERE IPEnabled = 'TRUE'";
var options = new ConnectionOptions
{
EnablePrivileges = false,
Impersonation = ImpersonationLevel.Impersonate,
Username = _userName,
SecurePassword = _password,
Authority = "ntlmdomain:" + _yourDomain
};
Scope.Options = options;
Scope.Connect();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(Scope, query);
collection = searcher.Get();
//Do something with the collection
}
catch (ManagementException ex)
{
Console.WriteLine(ex.Message);
}
catch (UnauthorizedAccessException ex)
{
throw new ArgumentException(ex.Message);
}
}
private static SecureString CreateSecuredString(string pw)
{
var secureString = new SecureString();
foreach (var c in pw)
{
secureString.AppendChar(c);
}
return secureString;
}
您可能必须使用变量 EnablePrivileges 和模拟
尝试一些不同的统计数据修改强>
如果您想从您的电脑(本地)获取您的信息而不是将选项传递到示波器。