Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Invoking a WMI method on a Remote Machine

Invoking a WMI method on a Remote Machine

Scheduled Pinned Locked Moved C#
csharphtmlvisual-studiocomhelp
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bbranded
    wrote on last edited by
    #1

    Hello, I am attempting to invoke the GetVersionVector method of the DfsrReplicatedFolderInfo on a remote machine. After referring to two articles (1, 2) about the subject on InvokeMethod in C#, I have written the following code: string TargetMachine = textBox2.Text; System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" + TargetMachine + "\\root\\microsoftdfs"); oMs.Connect(); System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("SELECT * FROM DfsrReplicatedFolderInfo"); //WHERE ReplicationGroupName='" + comboBox6.Text + "'"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection MgmtObject = oSearcher.Get(); foreach (ManagementObject item in MgmtObject) { //Get an input parameters object for this method //ManagementBaseObject inParams = item.GetMethodParameters("GetVersionVector"); //Fill in input parameter values //inParams["CommandLine"] = "calc.exe"; //Execute the method ManagementObject outParams = (ManagementObject)item.InvokeMethod("GetVersionVector", null); //Display results //Note: The return code of the method is provided in the "returnValue" property of the outParams object MessageBox.Show(outParams.ToString()); } When attempting to InvokeMethod, the following error is thrown: Object reference not set to an instance of an object. If I do away with the cast and use declare outParams as an object instead, i get the same error. Any input is appreciated, Matt Brown

    B 1 Reply Last reply
    0
    • B bbranded

      Hello, I am attempting to invoke the GetVersionVector method of the DfsrReplicatedFolderInfo on a remote machine. After referring to two articles (1, 2) about the subject on InvokeMethod in C#, I have written the following code: string TargetMachine = textBox2.Text; System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" + TargetMachine + "\\root\\microsoftdfs"); oMs.Connect(); System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("SELECT * FROM DfsrReplicatedFolderInfo"); //WHERE ReplicationGroupName='" + comboBox6.Text + "'"); ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); ManagementObjectCollection MgmtObject = oSearcher.Get(); foreach (ManagementObject item in MgmtObject) { //Get an input parameters object for this method //ManagementBaseObject inParams = item.GetMethodParameters("GetVersionVector"); //Fill in input parameter values //inParams["CommandLine"] = "calc.exe"; //Execute the method ManagementObject outParams = (ManagementObject)item.InvokeMethod("GetVersionVector", null); //Display results //Note: The return code of the method is provided in the "returnValue" property of the outParams object MessageBox.Show(outParams.ToString()); } When attempting to InvokeMethod, the following error is thrown: Object reference not set to an instance of an object. If I do away with the cast and use declare outParams as an object instead, i get the same error. Any input is appreciated, Matt Brown

      B Offline
      B Offline
      bbranded
      wrote on last edited by
      #2

      Here's the solution.... Tweak query as desired...

      private void button7_Click(object sender, EventArgs e)
      {

              string TargetMachine = textBox2.Text;
      
      
      
              ManagementPath path = new ManagementPath("\\\\\\\\" + TargetMachine + "\\\\root\\\\microsoftdfs");
      
              ManagementScope scope = new ManagementScope(path);
              
              SelectQuery msQuery = new SelectQuery("SELECT \* FROM DfsrReplicatedFolderInfo");
      
              ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(scope, msQuery);
      
              
              
              object\[\] methodArgs = { "" };
              
      
              foreach (ManagementObject item in searchProcedure.Get())
              {
                  try
                  {
                      item.InvokeMethod("GetVersionVector", methodArgs);
                      
                      foreach (string returned in methodArgs)
                      {
                          MessageBox.Show(returned.ToString());
                      }
                  }
      
                  catch (SystemException excep)
                  {
                      MessageBox.Show(excep.Message);
                  }
      
              }
          }
      
      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups