[英]Reboot computer through WCF with WMI hosted on IIS c#
I have a WCF Service which is running on IIS. 我有一个正在IIS上运行的WCF服务。 ApplicationPool uses the LocalSystem Identity.
ApplicationPool使用LocalSystem身份。 The WCF Service has an method for rebooting the system with WMI using the following code:
WCF服务提供了一种使用以下代码通过WMI重新启动系统的方法:
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = null;
try
{
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 6 means we want to trigger a force reboot
mboShutdownParams["Flags"] = "6";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
var result = Convert.ToInt32(mboShutdown["returnValue"]);
if (result != 0) throw new Win32Exception(result, "Could not restart local machine!");
}
}
catch (Exception ex)
{
SkippyLogger.WriteError(ex, "Error in IISAdminService.Restart");
throw ex;
}
finally
{
if (mcWin32 != null)
mcWin32.Dispose();
}
The method throw an exception with "Privilege not held". 该方法将引发“未保留特权”的异常。
Few things to try... 几件事可以尝试...
Can you run the same method, as LocalSystem outside of IIS - ie host it as a windows service installed of IIS? 能否在IIS外部运行与LocalSystem相同的方法-即将其作为IIS安装的Windows服务托管?
We have a similar system (hosted as a windows service) and it performs the actual reboot using this class. 我们有一个类似的系统(托管作为Windows服务),并进行使用了实际重启这一类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.