我想创建一个可以使用 WinRM 而不是 WMI 收集系统信息 (Win32_blablabla) 的小型应用程序。我如何从 C# 中做到这一点?
主要目标是使用 WS-Man (WinRm) 而不是 DCOM (WMI)。
最佳答案
我想最简单的方法是使用 WSMAN 自动化。在您的项目中引用 windwos\system32 中的 wsmauto.dll:
那么,下面的代码应该适合您。 API 说明在这里:msdn: WinRM C++ API
IWSMan wsman = new WSManClass();
IWSManConnectionOptions options = (IWSManConnectionOptions)wsman.CreateConnectionOptions();
if (options != null)
{
try
{
// options.UserName = ???;
// options.Password = ???;
IWSManSession session = (IWSManSession)wsman.CreateSession("http://<your_server_name>/wsman", 0, options);
if (session != null)
{
try
{
// retrieve the Win32_Service xml representation
var reply = session.Get("http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/Win32_Service?Name=winmgmt", 0);
// parse xml and dump service name and description
var doc = new XmlDocument();
doc.LoadXml(reply);
foreach (var elementName in new string[] { "p:Caption", "p:Description" })
{
var node = doc.GetElementsByTagName(elementName)[0];
if (node != null) Console.WriteLine(node.InnerText);
}
}
finally
{
Marshal.ReleaseComObject(session);
}
}
}
finally
{
Marshal.ReleaseComObject(options);
}
}
希望这对你有帮助,问候
关于c# - 如何在 C# 中访问 WinRM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3771920/