【发布时间】:2018-02-17 01:44:48
【问题描述】:
这是用 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: '客户端无法连接到请求中指定的目标。验证目标上的服务是否正在运行并且正在接受请求。请查阅在目标(最常见的是 IIS 或 WinRM)上运行的 WS-Management 服务的日志和文档。如果目标是 WinRM 服务,在目标上运行以下命令来分析和配置 WinRM 服务:“winrm quickconfig”。'
我已确认该服务正在我的本地和远程计算机上运行。我很茫然。
【问题讨论】:
-
所以我发现我没有在任何地方打开连接。
-
添加了以下内容: // 连接到机器,现在访问被拒绝。无效的用户名/密码。现在得到无效的用户名/密码。我是否尝试了 2 个连接?