Итак, я работал над выключением WMI для компьютеров с Windows 10 на сервере / в сети, я пробовал несколько примеров кода, но обычно возвращаюсь с той же ошибкой
Я уже пытался подключитьсяк другим компьютерам, без олицетворения, имени пользователя / пароля, и я попробовал это с тем, что я получил большую часть этого кода от этого источника
Я ожидаю, что это соединит и выключит машинуно я получаю эту ошибку:
private void systemcontrolsremote(int flag, string target, string username, string password, string domain)
{
try
{
this.Console.Clear();
this.Console.Text = "Attpting to execute the command with the additional command line arguments:\nTarget: " + target + "\nUsername: " + username + "\nPassword: " + password + "\nDomain: " + domain + "\n";
ManagementScope scope = new ManagementScope("\\\\" + target + "\\root\\CIMV2", new ConnectionOptions()
{
EnablePrivileges = true,
Username = username,
Password = password,
Authority = "ntlmdomain:" + domain,
Impersonation = ImpersonationLevel.Impersonate
});
try
{
scope.Connect();
}
catch
{
try
{
scope.Options.EnablePrivileges = true;
scope.Connect();
}
catch
{
try
{
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Connect();
}
catch
{
try
{
scope.Options.EnablePrivileges = true;
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Connect();
}
catch (ManagementException ex)
{
this.Console.AppendText("An error occurred while trying to execute the WMI method: " + ex.Message);
}
catch (UnauthorizedAccessException ex)
{
this.Console.AppendText("Connection error (user name or password might be incorrect): " + ex.Message);
}
}
}
}
SelectQuery selectQuery = new SelectQuery("Win32_OperatingSystem");
foreach (ManagementObject managementObject in new ManagementObjectSearcher(scope, (ObjectQuery) selectQuery).Get())
{
ManagementBaseObject methodParameters = managementObject.GetMethodParameters("Win32Shutdown");
methodParameters["Flags"] = (object) flag;
managementObject.InvokeMethod("Win32Shutdown", methodParameters, (InvokeMethodOptions) null);
}
}
catch (ManagementException ex)
{
this.Console.AppendText("An error occurred while trying to execute the WMI method: " + ex.Message);
}
catch (UnauthorizedAccessException ex)
{
this.Console.AppendText("Connection error (user name or password might be incorrect): " + ex.Message);
}
}```