使用 system.management 关闭时访问被拒绝

发布于 2024-11-05 11:32:56 字数 3079 浏览 7 评论 0原文

我正在尝试用 C# -MVC# 2008 Express 创建一个小程序,它可以让我远程打开和关闭我负责的 15 台计算机。打开它们很容易,但关闭它们似乎有点问题。

首先,我没有域或 SharePoint,只有 Windows XP 上的一个简单工作组。现在,我设法让 shutdown.exe 工作,但我认为必须有一个 C# 解决方案,因此经过一番搜索后,我发现一些使用 system.management 命名空间的代码,效果很好。

这两种解决方案的唯一问题是我需要登录相同的管理员帐户,好吧,我们只是说,并不是与我一起工作的每个人都是最精通技术的,所以一想到让他们使用管理员帐户就让我感到紧张。

我不能让他们访问该功能,但我发现了以下代码:

void Shutdown() {
  try
  {
      const string computerName = "PC05"; // 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 = "";
      //options.Password = "";
      //options.Authority = "ntlmdomain:DOMAIN";

      //ManagementScope scope = new ManagementScope("\\\\" + computerName +  "\\root\\cimv2", options);
      ManagementScope scope = new ManagementScope();
      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);
  }
} 

但是当我尝试使用它时,我不断收到访问被拒绝的错误。

“访问被拒绝。(HRESULT 异常:0x80070005 (E_ACCESSDENIED))”} System.Exception {System.UnauthorizedAccessException}

我尝试仅取消注释密码和用户名(使用我知道正确的管理员帐户的密码和用户名)也取消注释权威。我用过:

options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;

但似乎没什么用。我不知道是否需要为此设置特殊设置,但就像我说的,如果我登录到另一台计算机上使用的管理员帐户,我可以连接和关闭。我目前正在备用管理员帐户中进行测试。

我已阅读:

http://msdn.microsoft。 com/en-us/library/aa393613(v=VS.85).aspx

http://msdn.microsoft.com/en-us/库/aa393266(v=VS.85).aspx

http://msdn.microsoft.com/en-us/库/aa389286(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/ aa389290(VS.85).aspx (老实说不太明白这个)

也许它只在域中允许,但我还没有找到任何对此的确认。我想避免添加另一个帐户,所以有什么建议吗?

I'm trying to create a small program in C# -MVC# 2008 Express- that will let me turn on and off the 15 computers I'm in charge of, remotely. Turning them on was easy but turning them off seems to be a bit more problematic.

First off I don’t have domains or SharePoint’s, just a simple workgroup on Windows XP. Now, I managed to get shutdown.exe to work but I figured there must be a C# solution so after a bit of searching I found some code using system.management namespace which worked great.

Only problem with both solutions is that I needed to be login an identical administrator account and well let's just say not everyone I work with is the most tech savvy, so thought of letting them use an admin account make me nervous.

I could just not have them access that feature but I found the following code:

void Shutdown() {
  try
  {
      const string computerName = "PC05"; // 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 = "";
      //options.Password = "";
      //options.Authority = "ntlmdomain:DOMAIN";

      //ManagementScope scope = new ManagementScope("\\\\" + computerName +  "\\root\\cimv2", options);
      ManagementScope scope = new ManagementScope();
      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);
  }
} 

But I keep getting an access denied error when I try to use it.

"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"} System.Exception {System.UnauthorizedAccessException}

I tried uncommenting just password and username (with the pass and username of the admin account which I know is right) also uncommenting authority. I used:

options.Impersonation = ImpersonationLevel.Impersonate;
options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;

but nothing seems to work. I don't know if I need to set a special setting for this but like I said I can connect and shutdown if I login to the admin account used on the other machine. I currently am testing in an alternate admin account.

I have read:

http://msdn.microsoft.com/en-us/library/aa393613(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa393266(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa389286(v=VS.85).aspx

http://msdn.microsoft.com/en-us/library/aa389290(VS.85).aspx (honestly didn't quite get this one)

Maybe it's only allowed in domains but I haven't found any confirmation of this. I want to avoid having to add another account, so any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-11-12 11:32:56
ManagementBaseObject outParams = null;
ManagementClass os = new ManagementClass("Win32_OperatingSystem");
os.Get();
os.Scope.Options.EnablePrivileges = true; // enables required security privilege.
ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown");
inParams["Flags"] = "2"; // System reboot
inParams["Reserved"] = "0";
foreach (ManagementObject mo in os.GetInstances())
    outParams = mo.InvokeMethod("Win32Shutdown",
    inParams, null);
ManagementBaseObject outParams = null;
ManagementClass os = new ManagementClass("Win32_OperatingSystem");
os.Get();
os.Scope.Options.EnablePrivileges = true; // enables required security privilege.
ManagementBaseObject inParams = os.GetMethodParameters("Win32Shutdown");
inParams["Flags"] = "2"; // System reboot
inParams["Reserved"] = "0";
foreach (ManagementObject mo in os.GetInstances())
    outParams = mo.InvokeMethod("Win32Shutdown",
    inParams, null);
太阳男子 2024-11-12 11:32:56

这是一个更简单的解决方法,可能对您的情况有用(请告诉我这是否是您可能使用的解决方案):

无需管理员权限远程关机(*.bat 脚本)

Here is a simpler workaround that might be useful for your situation (let me know if it's a solution you might use):

remote shutdown (*.bat script) without administrator rights

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文