В своем приложении для удаленного администрирования я просто нахожу небольшое, но неприятное исключение.
В какой-то момент мне нужно проверить, нужно ли перезагрузить удаленный компьютер. Одной из частей этой проверки является вызов метода «DetermineIfRebootPending» в классе WMI «CCM_ClientUtilities».
Все отлично работает, но ...
Когда я перезагружаю целевой компьютер, вызов метода вызывает исключение для первой попытки. Со второй попытки снова все в порядке. Или если я поставлю точку останова при отладке в режиме онлайн с. 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: строка 176
в L_Tool_2._0.RemoteMachineClasses.Relation.GetPendingReboot (Machine
remoteMachine, Boolean checkWMICCM) v C: ... \ source \ repos \ L_Tool
2.0 \ L_Tool 2.0 \ RemoteMachineClasses \ Relation.cs: строка 645