gpt4 book ai didi

c# - WMI 重启远程机器

转载 作者:可可西里 更新时间:2023-11-01 08:06:02 26 4
gpt4 key购买 nike

我在一个旧线程上找到了这段代码来关闭本地机器:

using System.Management;

void Shutdown()
{
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();

// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams =
mcWin32.GetMethodParameters("Win32Shutdown");

// Flag 1 means we want to shut down the system. Use "2" to reboot.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown",
mboShutdownParams, null);
}
}

是否可以使用类似的 WMI 方法重新启动标记为“2”的远程机器,我只有机器名,没有 IP 地址。

编辑:我目前有:

SearchResultCollection allMachinesCollected = machineSearch.FindAll();
Methods myMethods = new Methods();
string pcName;
ArrayList allComputers = new ArrayList();
foreach (SearchResult oneMachine in allMachinesCollected)
{
//pcName = oneMachine.Properties.PropertyNames.ToString();
pcName = oneMachine.Properties["name"][0].ToString();
allComputers.Add(pcName);
MessageBox.Show(pcName + "has been sent the restart command.");
Process.Start("shutdown.exe", "-r -f -t 0 -m \\" + pcName);
}

但这行不通,我更喜欢 WMI。

最佳答案

要将 WMI 查询发送到远程计算机,您只需在 ManagementScope 中指定该计算机的名称(或 IP 地址)即可。对象。

我不太擅长 C#,但这是我使用 MSDN 和 WMI Code Creator 想出的一个例子(顺便说一句,这是生成 WMI 代码的出色工具,并且支持 C# 等)。希望这段代码会给你灵感。

(免责声明:此代码未经测试。)

using System;
using System.Management;
...

void Shutdown()
{
try
{
const string computerName = "COMPUTER"; // computer name or IP address

ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
// To connect to the remote computer using a different account, specify these values:
// options.Username = "USERNAME";
// options.Password = "PASSWORD";
// options.Authority = "ntlmdomain:DOMAIN";

ManagementScope scope = new ManagementScope(
"\\\\" + computerName + "\\root\\CIMV2", options);
scope.Connect();

SelectQuery query = new SelectQuery("Win32_OperatingSystem");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(scope, query);

foreach (ManagementObject os in searcher.Get())
{
// Obtain in-parameters for the method
ManagementBaseObject inParams =
os.GetMethodParameters("Win32Shutdown");

// Add the input parameters.
inParams["Flags"] = 2;

// Execute the method and obtain the return values.
ManagementBaseObject outParams =
os.InvokeMethod("Win32Shutdown", inParams, null);
}
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
}
catch(System.UnauthorizedAccessException unauthorizedErr)
{
MessageBox.Show("Connection error (user name or password might be incorrect): " + unauthorizedErr.Message);
}
}

关于c# - WMI 重启远程机器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2921905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com