ManagementClass.InvokeMethod throws exception after restart target computer

  • Thread starter Thread starter Pavel Horák
  • Start date Start date
P

Pavel Horák

Guest
In my form app for the remote administration, I just find little but an unpleasant exception.

In some point, I need to check if remote machine needs restart or not. One part of this check is invoking method "DetermineIfRebootPending" in "CCM_ClientUtilities" class of WMI.

Everything works perfect, but... When I reboot the target machine, the invoking the method throws an exception. When I invoke it again after few seconds, everything is OK again, but when i Invoke it right after first attempt there is still same exception. So I have to call it once, wait few secs and call it again. If I put breakpoint when debugging online with.InvokeMethod everything is Ok too, even the first time.

I will be really grateful for any advice, thank you.

There is the code example:


//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;
}


The exception is:

Message: ""
Type: UnauthorizedAccessException
HResult: -2147024891
StackTrace:


in System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)

in System.Management.ManagementObject.InvokeMethod(String methodName, ManagementBaseObject inParameters, InvokeMethodOptions options)

in 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

in 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:line 645

Continue reading...
 
Back
Top