用C#写的。第二个控制台程序我已经完成了。它编译并运行。但是当它连接到客户端时。它说客户端不存在。我通过PC名称在代码中连接到我的本地机器。即使在尝试连接到远程计算机时,我也会收到相同的消息。
我删除了计算机名称,域名和用户名,但在输入信息时保留了文本。
错误在第50行,foreach(allProcesses中的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:'客户端无法连接到请求中指定的目标。验证目标上的服务是否正在运行并且正在接受请求。查阅目标上运行的WS-Management服务的日志和文档,最常见的是IIS或WinRM。如果目标是WinRM服务,请在目标上运行以下命令以分析和配置WinRM服务:“winrm quickconfig”。'
我已确认该服务正在本地和远程计算机上运行。我很茫然。
答案 0 :(得分:0)
因此,在稍微打了一下后,我决定重新开始使用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);
}
}
}
}
答案 1 :(得分:0)