WMI远程连接,失败

时间:2013-01-31 16:23:16

标签: c# wmi remote-access

已解决,如果有任何需要它的话,可以在其他线程中得到答案。

说明:

本地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)at   System.Management.ThreadDispatch.Start()at   System.Management.ManagementScope.Initialize()at   System.Management.ManagementScope.Connect()at   Admin_Helper.frmRemoteInformation.button1_Click(对象发件人,   EventArgs e)在c:\ Users \ Stef \ Documents \ Visual Studio中   2012 \ Projects \ Admin_Helper \ Admin_Helper \ frmRemoteInformation.cs:line   110在System.Windows.Forms.Control.OnClick(EventArgs e)at   System.Windows.Forms.Button.OnClick(EventArgs e)at   System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)at at   System.Windows.Forms.Control.WmMouseUp(Message& m,MouseButtons   按钮,Int32点击)at   System.Windows.Forms.Control.WndProc(Message& m)at   System.Windows.Forms.ButtonBase.WndProc(Message& m)at   System.Windows.Forms.Button.WndProc(Message& m)at   System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)   在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&   m)在System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32)   消息,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) { }
    }

UPDATE:

问题似乎与WMI有关。

UPDATE2:

。最愚蠢的错误。不得不改变两件事:

  1. remoteConnectionOptions.Username = txtUsername.Text; ==> remoteConnectionOptions.Username = txtServer.Text + @"\" + txtUsername.Text; 这将给出:“服务器名称\用户名”

  2. Query = new ObjectQuery(cmbClassSelection.SelectedItem.ToString()); ==&GT; objectQuery = new ObjectQuery(“select * from”+ cmbClassSelection.SelectedItem.ToString());

  3. 忘记了“select * from”,因为我使用了组合框。

    如果有人需要代码,我会在清理一下后更新它。

1 个答案:

答案 0 :(得分:2)

自己想出来:

务必检查防火墙和防火墙WMI设置......

好的,检查连接是否成功的一种方法是:

ManagementScope myscope = new ManagementScope(@"\\Server\Username", ConnectionOptions);

if (myscope.IsConnected) { MessageBox.Show("Connected"); } else { MessageBox.Show("Disconnected"); }

现在您知道您的连接是否有效,然后只需搜索标准查询即可将其与您自己的代码进行比较...

示例:example on msdn

完成代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Management;
using System.Diagnostics;

namespace Admin_Helper
{
public partial class frmRemoteInformation : Form
{
    public frmRemoteInformation()
    {
        InitializeComponent();
    }

    string strServername;
    string strUsername;
    string strPassword;
    string strClassSelection;
    ConnectionOptions rcOptions;
    ManagementObjectCollection moCollection;
    ObjectQuery oQuery;
    ManagementObjectSearcher moSearcher;
    ManagementScope mScope;

    private void frmRemoteInformation_Load(object sender, EventArgs e)
    {
        if (mScope.IsConnected == true) { lblConnectionStateWarning.Text = "Connected"; } else { lblConnectionStateWarning.Text = "Disconnected"; } //I have a label that displays connectionstate, you can leave that out
    }

    private void btnConnect_Click(object sender, EventArgs e)
    {
        try
        {
            strServername = txtServer.Text;
            strUsername = txtUsername.Text;
            strPassword = txtPassword.Text;

            remoteConnection(strServername, strUsername, strPassword);
        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }

    }

    private void cmbClassSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
            var dctPropertyList = new Dictionary<string, string>();
            strClassSelection = cmbClassSelection.SelectedItem.ToString();
            new Thread(() => FindWMI(strServername, strClassSelection, rcOptions, dctPropertyList, lstProperties)).Start();
    }

    private void remoteConnection(string servername, string username, string password)
    {
        try
        {
            rcOptions = new ConnectionOptions();
            rcOptions.Authentication = AuthenticationLevel.Packet;
            rcOptions.Impersonation = ImpersonationLevel.Impersonate;
            rcOptions.EnablePrivileges = true;
            rcOptions.Username = servername + @"\" + username;
            rcOptions.Password = password;

            mScope = new ManagementScope(String.Format(@"\\{0}\root\cimv2", servername), rcOptions);
            mScope.Connect();
            if (mScope.IsConnected == true) { MessageBox.Show("Connection Succeeded", "Alert"); } else { MessageBox.Show("Connection Failed", "Alert"); }
            if (mScope.IsConnected == true) { lblConnectionStateWarning.Text = "Connected"; } else { lblConnectionStateWarning.Text = "Disconnected"; } //I have a label that displays connectionstate, you can leave that out

        }
        catch (Exception ex) { MessageBox.Show(ex.Message); }
    }

    private void FindWMI(string servername, string classSelection, ConnectionOptions rcOptions, Dictionary<string, string> dct, ListView listView)
    {
        try
        {
            List<ListViewItem> itemsList = new List<ListViewItem>();
            oQuery = new ObjectQuery("select * from " + classSelection);
            moSearcher = new ManagementObjectSearcher(mScope, oQuery);
            moCollection = moSearcher.Get();

            Invoke(new MethodInvoker(() =>
            {
                listView.Items.Clear();
            }));

            foreach (ManagementObject mObject in moCollection)
            {
                if (mObject != null)
                {
                    foreach (PropertyData propData in mObject.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) { }
    }

    private void frmRemoteInformation_FormClosed(object sender, FormClosedEventArgs e)
    {
        foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
        {
            if (myProc.ProcessName == "WmiPrvSE")
            {
                myProc.Kill();
            }
        }

        if (mScope.IsConnected == true) { mScope.Options.Authentication = AuthenticationLevel.None; }; //Change option so that the connection closes, no disconnect option
    }

}
}
相关问题