连接到远程 PC 以监视 运行 服务时出错

Error connecing to remote PC to monitor running services

用 C# 编写。我完成的第二个控制台程序。它编译并运行。但是当它连接到客户端时。它说客户端不存在。我通过 PC 名称在代码中连接到我的本地计算机。即使在尝试连接到远程计算机时,我也会收到相同的消息。

我删除了计算机名称、域和用户名,但在输入信息时留下了文本。

第 50 行错误,foreach(所有进程中的 CimInstance 进程)

using System;
using System.Text;
using System.Threading;
using System.Management;
using System.Security;
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;



namespace WMITest
{
class Program
{
    static void Main(string[] args)
    {

        string computer = "MyPC";
        string domain = "MyDomain";
        string username = "MyUserName";

        string password;

        Console.WriteLine("Enter Password:");
        password = Console.ReadLine();

        SecureString securepassword = new SecureString();
        foreach (char c in password)
        {
            securepassword.AppendChar(c);
        }

        // Create Creds
        CimCredential Credentials = new CimCredential(PasswordAuthenticationMechanism.Default, 
                                                    domain,
                                                   username,
                                                   securepassword);

        // Create Session Options using Creds
        WSManSessionOptions sessionOptions = new WSManSessionOptions();
        sessionOptions.AddDestinationCredentials(Credentials);

        //create session using computer, sessionOptions
        CimSession Session = CimSession.Create(computer, sessionOptions);

        var allProcesses = Session.QueryInstances(@"root\cimv2", "WQL", "Select * from Win32_Service where name like '%MSSQL$%'");

// Connection Options
ConnectionOptions wmcon = new ConnectionOptions();
        ManagementScope wmscp = null;

        wmcon.Username = username;
        wmcon.Password = password;
        wmcon.Authority = "ntlmdomain:" + domain;

        wmscp = new ManagementScope("\\" + computer + "\root\CIMV2", wmcon);

        wmscp.Connect();

        // Loop Through instances
        foreach (CimInstance process in allProcesses) //<--- This line is where error is popping up
        {

            if (process.CimInstanceProperties["SQL Services"].ToString()[0] > ' ')
            {
                Console.WriteLine("SQL server {0} is currently {1}",
                            process.CimInstanceProperties["Name"],
                            process.CimInstanceProperties["Status"]);
            }
        }Console.ReadLine();
    }   
}

}

下面是我得到的错误

Microsoft.Management.Infrastructure.CimException: 'The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: "winrm quickconfig".'

我已经确认服务在我的本地和远程机器上都是 运行。我很茫然。

因此,在考虑了一会儿之后,我决定重新开始使用 Microsoft 的 WMI Code Creator (URL: https://www.microsoft.com/en-us/download/details.aspx?id=8572)。这基本上构建了我使用与登录时不同的帐户进行远程连接所需的一切。

感谢那些花时间查看本文的人。

using System;
using System.Text;
using System.Threading;
using System.Management;
using System.Security;
using Microsoft.Management.Infrastructure;
using Microsoft.Management.Infrastructure.Options;



namespace WMITest
{
class Program
{
    static void Main(string[] args)
    {

        string computer;
        string domain = "";
        string username = "Administrator";

        string password;

        Console.WriteLine("Enter PC IP Address:");
        computer = Console.ReadLine();
        Console.Clear();

        Console.WriteLine("Enter Password:");
        password = Console.ReadLine();
        Console.Clear();

        try
        {
            ConnectionOptions connection = new ConnectionOptions();
            connection.Username = username;
            connection.Password = password;
            connection.Authority = "ntlmdomain:" + domain;

            ManagementScope scope = new ManagementScope(
                "\\" + computer + "\root\cimv2", connection);
            scope.Connect();

            ObjectQuery query = new ObjectQuery(
                "SELECT * FROM Win32_Service WHERE Name like 'MSSQL$%'");

            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(scope, query);

            foreach (ManagementObject queryObj in searcher.Get())
            {
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Win32_Service instance");
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("Name: {0}", queryObj["Name"]);
            }
            Console.ReadLine();
        }
        catch (ManagementException err)
        {
            Console.WriteLine("An error occurred while querying for WMI data: " + err.Message);
        }
        catch (System.UnauthorizedAccessException unauthorizedErr)
        {
            Console.WriteLine("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
        }
    }

}
}

"The error means you haven’t got the winrm service running on the local/remote machine. On modern Windows remoting, and therefore winrm, are enable by default for servers but disable for client OS e.g. Windows 10. Easiest way to get this to work is run 'Enable-PSremoting' from and elevated PowerShell prompt."

We should not be using WMI anymore because it is deprecated