开发者

Access Denied when using system.management for shutdown

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 a开发者_StackOverflowlternate 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?


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜