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

时间:2011-05-05 19:00:28

标签: c# windows-xp workgroup system.management

我正在尝试用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/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(老实说,这不太合适)

也许只允许在域名中使用,但我还没有找到任何确认信息。我想避免添加其他帐户,所以有什么建议吗?

2 个答案:

答案 0 :(得分:2)

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);

答案 1 :(得分:0)

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

remote shutdown (*.bat script) without administrator rights

相关问题