none
WMI programmatically RRS feed

  • Question

  • Hi,

     

    I am trying to get all the processes on the remote machine using System.Management assembly.  Here is the code.

     

    Code Snippet

    System.Management.ConnectionOptions conO = new System.Management.ConnectionOptions();

    System.Management.ManagementScope oMs = new System.Management.ManagementScope(@"\\10.110.32.61\root\cimv2", conO);

    //get all the Processes running on remote m/c

    System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("Select * from Win32_Process");

    //Execute the query

    System.Management.ManagementObjectSearcher oSearcher = new System.Management.ManagementObjectSearcher(oMs,oQuery);

    //Get the results

    System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();

    //loop through found process and terminate

    foreach (System.Management.ManagementObject oReturn in oReturnCollection)

    {

    string[] argList = new string[] { string.Empty };

    //Name of process

    if (oReturn["Name"].ToString().ToLower() == "notepad.exe")

    {

    object[] obj = new object[] { 0 };

    oReturn.InvokeMethod("Terminate", obj);

    }

    }

     

     

     

    on oSearcher.Get() method, I get the following error:

     

    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)

     

    Stack Trace ----

    "   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)\r\n   at System.Management.ManagementScope.InitializeGuts(Object o)\r\n   at System.Management.ManagementScope.Initialize()\r\n   at System.Management.ManagementObjectSearcher.Initialize()\r\n   at System.Management.ManagementObjectSearcher.Get()\r\n   at ConsoleApplication1.Program.UseManagementIn() in C:\\Documents and Settings\\Kpervaiz\\My Documents\\Visual Studio 2008\\Projects\\ConsoleApplication1\\Program.cs:line 73\r\n   at ConsoleApplication1.Program.Main(String[] args) in C:\\Documents and Settings\\Kpervaiz\\My Documents\\Visual Studio 2008\\Projects\\ConsoleApplication1\\Program.cs:line 13\r\n   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"

     

    When using System.Diagnostics:

     

    Code Snippet

    System.Net.IPHostEntry entryIP = System.Net.Dns.GetHostByAddress("10.110.32.61");

    //This will get all the processes on the remote machine

    System.Diagnostics.Process[] rProc = System.Diagnostics.Process.GetProcesses(entryIP.HostName);

    System.Collections.IEnumerator reNm = proc.GetEnumerator();

    while (eNm.MoveNext())

    {

    System.Diagnostics.Process nProc = (System.Diagnostics.Process)eNm.Current;

    if (nProc.ProcessName.ToString().ToLower().Equals("notepad"))

    nProc.Kill();

    }

     

     

     

    on Process.GetProcesses(string), I get the following error:

     

    Couldn't connect to remote machine.

     

    Inner Exception:  Access denied

    Stack Trace:

     

    "   at System.Diagnostics.NtProcessManager.GetProcessInfos(String machineName, Boolean isRemoteMachine)\r\n   at System.Diagnostics.ProcessManager.GetProcessInfos(String machineName)\r\n   at System.Diagnostics.Process.GetProcesses(String machineName)\r\n   at ConsoleApplication1.Program.Main(String[] args) in C:\\Documents and Settings\\Kpervaiz\\My Documents\\Visual Studio 2008\\Projects\\ConsoleApplication1\\Program.cs:line 40\r\n   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"

     

    Please advice.  Thank you.

    Thursday, August 7, 2008 12:44 PM

Answers

  • I was able to get around this.

     

    System.Management.ConnetionOptions oCon = new System.Management.ConnectionOptions();

    oCon.UserName = @"domain\username";

    oCon.Password = "Password";

     

    and then making this same account part of the administrator group on the remote machine.

     

    Monday, August 11, 2008 4:57 PM