已解决,如果有任何需要它的话,已经在其他线程中得到了答案 .
说明:
本地WMI工作,但现在我正在尝试远程工作 . 我已经尝试禁用两个防火墙,检查WMI权限(使用管理员帐户登录),这应该没问题 . 然而,当尝试连接IP时,我不断得到:0x800706BA错误,但在使用PC名称时,没有显示错误,但WMI没有显示任何结果 .
Lansweeper成功了 . (http://www.lansweeper.com/kb/3/WMI-Access-is-denied.html)
连接错误:managementScope.Connect()上的0x800706BA
System.Runtime.InteropServices.COMException(0x800706BA):RPC服务器不可用 . (来自HRESULT的异常:0x800706BA)位于System.Management.ThreadDispatch.Start()的System.Management.ManagementScope.Initialize()位于System.Management.ManagementScope.Connect()的Admin_Helper.frmRemoteInformation.button1_Click(Object sender,EventArgs e)在c:\ Users \ Stef \ Documents \ Visual Studio 2012 \ Projects \ Admin_Helper \ Admin_Helper \ frmRemoteInformation.cs:System.Windows.Forms.Button.OnClick上System.Windows.Forms.Control.OnClick(EventArgs e)的第110行(EventArgs e)位于System.Windows.Forms.Control.WndProc的System.Windows.Forms.Control.WmMouseUp(Message&m,MouseButtons按钮,Int32单击)的System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)(消息& m)System.Windows上的System.Windows.Fornd.WandProc(Message&m)处的System.Windows.Forms.ButtonBase.WndProc(Message&m)处于System.Windows的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m) System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 m)中的.Forms.Control.ControlNativeWindow.WndProc(Message&m) essage,IntPtr wparam,IntPtr lparam)
问题:
什么是检查连接是否成功/失败或wmi以及如何修复它的最佳方法 .
ManagementScope managementScope;
ObjectQuery query;
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
ConnectionOptions remoteConnectionOptions = new ConnectionOptions();
remoteConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
remoteConnectionOptions.EnablePrivileges = true;
remoteConnectionOptions.Authentication = AuthenticationLevel.Packet;
remoteConnectionOptions.Username = txtUsername.Text;
remoteConnectionOptions.Password = txtPassword.Text;
managementScope = new ManagementScope(@"\\" + txtServer.Text + @"\root\CIMV2", remoteConnectionOptions);
managementScope.Connect();
MessageBox.Show("Connected");
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
private void cmbClassSelection_SelectedIndexChanged(object sender, EventArgs e)
{
var dctPropertyList = new Dictionary<string, string>();
query = new ObjectQuery(cmbClassSelection.SelectedItem.ToString());
new Thread(() => FindWMI(managementScope, query, dctPropertyList, lstProperties)).Start();
}
private void FindWMI(ManagementScope scope, ObjectQuery query, Dictionary<string, string> dct, ListView listView)
{
try
{
List<ListViewItem> itemsList = new List<ListViewItem>();
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(scope, query);
Invoke(new MethodInvoker(() =>
{
listView.Items.Clear();
}));
foreach (ManagementObject moObject in moSearcher.Get())
{
if (moObject != null)
{
foreach (PropertyData propData in moObject.Properties)
{
if (propData.Value != null && propData.Value.ToString() != "" && propData.Name != null && propData.Name != "")
dct[propData.Name] = propData.Value.ToString();
}
}
}
foreach (KeyValuePair<string, string> listItem in dct)
{
ListViewItem lstItem = new ListViewItem(listItem.Key);
lstItem.SubItems.Add(listItem.Value);
itemsList.Add(lstItem);
}
Invoke(new MethodInvoker(() =>
{
listView.Items.AddRange(itemsList.ToArray());
}));
}
catch (Exception) { }
}
更新:
问题似乎与WMI有关 .
更新2:
. 最愚蠢的错误 . 不得不改变两件事:
-
remoteConnectionOptions.Username = txtUsername.Text; ==> remoteConnectionOptions.Username = txtServer.Text + @"\" + txtUsername.Text;
哪个会给:"Server-Name\Username" -
Query = new ObjectQuery(cmbClassSelection.SelectedItem.ToString()); ==> objectQuery = new ObjectQuery(“select * from”cmbClassSelection.SelectedItem.ToString());
因为我使用组合框,所以忘记了“select * from” .
如果有人需要代码,我会在清理一下之后更新它 .
1 回答
自己想出来:
一定要检查防火墙和WMI设置......
好吧,检查连接是否成功的一种方法是:
现在您知道您的连接是否有效,然后只需搜索标准查询即可将其与您自己的代码进行比较...
示例:example on msdn
完成代码: