我正在使用 WinRM API 试图从运行 Windows 或 Linux 的多个主机收集一些 CIM 实例。我的代码在连接到 Windows 主机时工作正常,但如果我尝试连接到运行 SFCB CIM 服务器的 Linux 机器,则会引发异常。我可以通过 WBEM 很好地从 Linux 主机检索 CIM 实例,但不能通过 WS-MAN/WinRM。
这是从 Windows 主机获取的示例代码CIM_OperatingSystem
- 这很好用:
WSMan wsman = new WSMan();
IWSManConnectionOptions options = (IWSManConnectionOptions)wsman.CreateConnectionOptions();
try
{
string remoteHost = "WindowsHost1";
options.UserName = @"domain\User";
options.Password = "somePwd";
IWSManSession session = (IWSManSession)wsman.CreateSession(remoteHost, wsman.SessionFlagCredUsernamePassword(), options);
try
{
IWSManEnumerator cimInstances = session.Enumerate("http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/CIM_OperatingSystem");
// Enumerate returned CIM instances.
while (!cimInstances.AtEndOfStream)
{
string item = cimInstances.ReadItem();
XDocument doc = XDocument.Parse(item);
var resultSet = from e in doc.Elements() select e;
foreach (var element in resultSet)
{
Console.WriteLine(element);
}
}
}
finally
{
Marshal.ReleaseComObject(session);
}
}
finally
{
Marshal.ReleaseComObject(options);
}
如果remoteHost
指向 Linux 机器(在我的示例中为 openSUSE VM),会发生以下情况:
remoteHost = "myLinuxHost"
如果我只指定主机名,即session.Enumerate()
失败:
未处理的异常:System.IO.FileNotFoundException:找不到网络路径。在 WSManAutomation.IWSManSession.Enumerate(对象 resourceUri、字符串过滤器、字符串方言、Int32 标志)
我可以成功 ping 机器,所以它应该是可见的。但是,主机名仅映射到我的 Windowshosts
文件中的 IP。如果我尝试使用 PowerShell 创建到这台机器的会话,也会发生错误:
PS C:\Windows\system32> $session = new-cimsession myLinuxHost -credential user
new-cimsession :WinRM 无法处理该请求。使用 Kerberos 身份验证时出现以下错误:找不到计算机 myLinuxHost。验证计算机是否存在于网络上,并且提供的名称拼写正确。
- 如果我指定了完整的主机 URL(一个我可以使用 WBEM 检索 CIM 实例的 URL),即,
remoteHost = "https://<ip>:5989"
或者remoteHost = "https://myLinuxHost:5989"
枚举失败并显示:
未处理的异常:System.Runtime.InteropServices.COMException:WSManAutomation.IWSManSession.Enumerate(对象 resourceUri,字符串过滤器,字符串方言,Int32 标志)发生安全错误
细节:
System.Runtime.InteropServices.COMException was unhandled
HResult=-2147012721
Message=A security error occurred
Source=Session
ErrorCode=-2147012721
StackTrace:
at WSManAutomation.IWSManSession.Enumerate(Object resourceUri, String filter, String dialect, Int32 flags)
at WSManTest.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
我究竟做错了什么?