在我用于远程管理的表单应用程序中,我发现很少但只有一个令人讨厌的异常。
在某些时候,我需要检查远程计算机是否需要重新启动。此检查的一部分是在WMI的“ CCM_ClientUtilities”类中调用方法“ DetermineIfRebootPending”。
一切正常,但是... 当我重新启动目标计算机时,调用该方法将为第一次尝试抛出异常。第二次尝试一切都还好。或者,如果我在使用.InvokeMethod进行在线调试时放了断点,即使第一次也没问题。
我将非常感谢您的任何建议,谢谢。
有代码示例:
//Method where I'm calling the method invoker
private static bool? GetPendingReboot(...)
{
//Some other code
string ccmServiceName = "CcmExec";
//Just checking and starting service...
if (RemoteTasks.CheckServiceExists(ccmServiceName, remoteMachine.Name) &&
RemoteTasks.StartService(ccmServiceName, timeOutServices, remoteMachine.Name))
{
Dictionary<string, string> ccmsResult =
RemoteTasks.CallMethodWMI(
new ManagementScope(
string.Format("\\\\{0}\\root\\ccm\\ClientSDK", remoteMachine.Name)),
"DetermineIfRebootPending",
"CCM_ClientUtilities"
);
if (bool.Parse(ccmsResult["IsHardRebootPending"]) ||
bool.Parse(ccmsResult["RebootPending"]))
{
return true;
}
}
}
//WMI method Invoker
public static Dictionary<string, string> CallMethodWMI(ManagementScope wmiScope, string methodName, string className, Dictionary<string, string> inputParams = null)
{
Dictionary<string, string> result = new Dictionary<string, string>();
using (ManagementClass managementClass = new ManagementClass(wmiScope.Path.Path, className, null))
{
using (ManagementBaseObject inParams = (inputParams != null) ? managementClass.GetMethodParameters(methodName) : null)
{
if (inputParams != null)
{
foreach (KeyValuePair<string, string> param in inputParams)
{
inParams[param.Key] = param.Value;
}
}
//This .InvokeMethod throws the exception, but like a I said, only after machine restart and only for first time call
using (ManagementBaseObject outParams = managementClass.InvokeMethod(methodName, inParams, null))
{
foreach (PropertyData data in outParams.Properties)
{
result.Add(data.Name, data.Value.ToString());
}
}
}
}
return result;
}
例外是:
消息:“”
类型:UnauthorizedAccessException
HResult :-2147024891
StackTrace :
在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo)
System.Management.ManagementObject.InvokeMethod中的(String methodName,ManagementBaseObject inParameters,InvokeMethodOptions 选项)
在L_Tool_2._0.RemoteMachineClasses.RemoteTasks.CallMethodWMI(ManagementScope wmiScope,String methodName,String className,Dictionary`2 inputParams)v C:... \ source \ repos \ L_Tool 2.0 \ L_Tool 2.0 \ RemoteMachineClasses \ RemoteTasks.cs:line 176
在L_Tool_2._0.RemoteMachineClasses.Relation.GetPendingReboot(Machine remoteMachine,布尔值检查(WMICCM)v C:... \ source \ repos \ L_Tool 2.0 \ L_Tool 2.0 \ RemoteMachineClasses \ Relation.cs:line 645