C# 使用system.management关闭时拒绝访问
我正在尝试用C#-MVC#2008Express创建一个小程序,它可以让我远程打开和关闭我负责的15台计算机。打开它们很容易,但关闭它们似乎有点问题 首先,我没有域或SharePoint,只是Windows XP上的一个简单工作组。现在,我设法让shutdown.exe工作,但我想一定有一个C#解决方案,所以经过一点搜索后,我找到了一些使用system.management命名空间的代码,效果非常好 这两种解决方案的唯一问题是,我需要登录一个相同的管理员帐户,好吧,让我们只说不是所有与我一起工作的人都是最懂技术的,所以让他们使用管理员帐户的想法让我感到紧张 我无法让他们访问该功能,但我发现了以下代码:C# 使用system.management关闭时拒绝访问,c#,windows-xp,workgroup,system.management,C#,Windows Xp,Workgroup,System.management,我正在尝试用C#-MVC#2008Express创建一个小程序,它可以让我远程打开和关闭我负责的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;
但似乎什么都不管用。我不知道我是否需要为此设置一个特殊设置,但就像我说的,如果我登录到另一台机器上使用的管理员帐户,我可以连接并关闭。我目前正在测试一个备用管理员帐户
我读过:
(老实说,我没有完全明白这一点)
也许它只允许在域中使用,但我还没有发现任何关于这一点的确认。我想避免添加其他帐户,因此有什么建议吗?这里有一个更简单的解决方法,可能对您的情况有用(如果您可能使用此解决方案,请告诉我):
使用shutdown.exe有什么问题?没什么,但正如我提到的,我想要一个不需要以管理员身份登录的选项。我还没有找到一种允许有限帐户访问它的方法。我不确定这对我来说是不是最理想的选择,但如果我找不到其他任何东西,我会尝试一下。
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);