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
  1. Home
  2. General Programming
  3. C#
  4. Win32_CompurterSystem: Rename, JoinDomainOrWorkgroup, UnjoinDomainOrWorkgroup - HELP

Win32_CompurterSystem: Rename, JoinDomainOrWorkgroup, UnjoinDomainOrWorkgroup - HELP

Scheduled Pinned Locked Moved C#
helptutorialcsharp
5 Posts 2 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.
  • M Offline
    M Offline
    mcldev
    wrote on last edited by
    #1

    In C#, using WMI has been great for access SI data. For reasons still unbeknownst to me, I cannot get a ManagementBaseObject instance (lets's call it mob) to properly invoke any of these function using InvokeMethod. For example:

    mof = "Rename";
    name = "NEWNAME";
    password = "adminpw";
    user = "administrator";

    object[] args = new args[3];
    args[0] = name;
    args[1] = password;
    args[1] = user;

    ManagementBaseObject mob = new ManagementBaseObject();
    long retval = (long) mob.InvokeMethod(mof, name, password, user);

    if (retval == 0)
    Console.Writeline("Reboot PC for RENAME to take effect");
    else
    Console.Writeline("RENAME error {0}");

    Everytime I get Invalid Method Parameter(s) message. If anyone knows how to succcessfully call Rename, JoinDomainOrWorkgroup, etc from C#, please share the technique. Thanks

    U 1 Reply Last reply
    0
    • M mcldev

      In C#, using WMI has been great for access SI data. For reasons still unbeknownst to me, I cannot get a ManagementBaseObject instance (lets's call it mob) to properly invoke any of these function using InvokeMethod. For example:

      mof = "Rename";
      name = "NEWNAME";
      password = "adminpw";
      user = "administrator";

      object[] args = new args[3];
      args[0] = name;
      args[1] = password;
      args[1] = user;

      ManagementBaseObject mob = new ManagementBaseObject();
      long retval = (long) mob.InvokeMethod(mof, name, password, user);

      if (retval == 0)
      Console.Writeline("Reboot PC for RENAME to take effect");
      else
      Console.Writeline("RENAME error {0}");

      Everytime I get Invalid Method Parameter(s) message. If anyone knows how to succcessfully call Rename, JoinDomainOrWorkgroup, etc from C#, please share the technique. Thanks

      U Offline
      U Offline
      Uros Calakovic
      wrote on last edited by
      #2

      There is a sample for InvokeMethod() here[^] and also here.[^] Frankly, I don't know how you managed to get to Invalid Method Parameter(s) with your code.

      In January you said "Money in April" - That was two years ago! B. Python

      M 1 Reply Last reply
      0
      • U Uros Calakovic

        There is a sample for InvokeMethod() here[^] and also here.[^] Frankly, I don't know how you managed to get to Invalid Method Parameter(s) with your code.

        In January you said "Money in April" - That was two years ago! B. Python

        M Offline
        M Offline
        mcldev
        wrote on last edited by
        #3

        The examples on MSDN are for Win32_Process and they work when I test. For some reason, Win32_ComputerSystem is behaving differently. I think there must a different way of calling the Win32_ComputerSystem functions. Here is the exact code that yields this result every time! :wtf: Exception caught!!! System.Management: Invalid method Parameter(s)

           public bool RenameComputer(string name, string password, string user)
            {
                bool ok = true;
                string mofName = "Rename";
        
                try
                {
                    //Get an input parameters object for this method
                    ManagementClass processClass = new ManagementClass("Win32\_ComputerSystem");
        
                    object\[\] methodArgs = { name, password, user};
        
                    for (int i = 0; i < methodArgs.Length; ++i)
                        Console.WriteLine("methodArgs\[{0}\] = {1}", i, methodArgs\[i\]);
        
                    object retval = processClass.InvokeMethod(mofName, methodArgs);
        
                    Console.WriteLine("InvokeMethod Status {0}, Process ID {1}", 
                        retval, methodArgs\[3\]);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception caught!!!");
                    Console.WriteLine("{0}: {1}", e.Source, e.Message);
                    ok = false;
                }
                return (ok);
            }
        

        Has anyone been sucessful in calling Win32_ComputerSystem InvokeMethod() for Rename, or JoinDomainOrWorkgroup or UnjoinDomainOrWorkgroup. Help is greatly appreciated by me and anyone else dealing with this challenge.

        U 1 Reply Last reply
        0
        • M mcldev

          The examples on MSDN are for Win32_Process and they work when I test. For some reason, Win32_ComputerSystem is behaving differently. I think there must a different way of calling the Win32_ComputerSystem functions. Here is the exact code that yields this result every time! :wtf: Exception caught!!! System.Management: Invalid method Parameter(s)

             public bool RenameComputer(string name, string password, string user)
              {
                  bool ok = true;
                  string mofName = "Rename";
          
                  try
                  {
                      //Get an input parameters object for this method
                      ManagementClass processClass = new ManagementClass("Win32\_ComputerSystem");
          
                      object\[\] methodArgs = { name, password, user};
          
                      for (int i = 0; i < methodArgs.Length; ++i)
                          Console.WriteLine("methodArgs\[{0}\] = {1}", i, methodArgs\[i\]);
          
                      object retval = processClass.InvokeMethod(mofName, methodArgs);
          
                      Console.WriteLine("InvokeMethod Status {0}, Process ID {1}", 
                          retval, methodArgs\[3\]);
                  }
                  catch (Exception e)
                  {
                      Console.WriteLine("Exception caught!!!");
                      Console.WriteLine("{0}: {1}", e.Source, e.Message);
                      ok = false;
                  }
                  return (ok);
              }
          

          Has anyone been sucessful in calling Win32_ComputerSystem InvokeMethod() for Rename, or JoinDomainOrWorkgroup or UnjoinDomainOrWorkgroup. Help is greatly appreciated by me and anyone else dealing with this challenge.

          U Offline
          U Offline
          Uros Calakovic
          wrote on last edited by
          #4

          Win32_ComputerSystem.Rename is an instance method so you first need to get the Win32_ComputerSystem instance to invoke it on:

          ManagementClass computerSystemClass =
          new ManagementClass("Win32_ComputerSystem");

          object[] methodArgs = { name, password, user };

          foreach (ManagementObject computerSystem in computerSystemClass.GetInstances())
          {
          object retval = computerSystem.InvokeMethod(mofName, methodArgs);
          }

          computerSystemClass.GetInstances() will return only one instance but you still need to use foreach. To avoid it, you could use the ManagementObject constructor that accepts the object path to get the instance:

          ManagementObject computerSystem =
          new ManagementObject("Win32_ComputerSystem.Name='SomeName'");

          but you would have to get the computer name that you want to change first.

          In January you said "Money in April" - That was two years ago! B. Python

          M 1 Reply Last reply
          0
          • U Uros Calakovic

            Win32_ComputerSystem.Rename is an instance method so you first need to get the Win32_ComputerSystem instance to invoke it on:

            ManagementClass computerSystemClass =
            new ManagementClass("Win32_ComputerSystem");

            object[] methodArgs = { name, password, user };

            foreach (ManagementObject computerSystem in computerSystemClass.GetInstances())
            {
            object retval = computerSystem.InvokeMethod(mofName, methodArgs);
            }

            computerSystemClass.GetInstances() will return only one instance but you still need to use foreach. To avoid it, you could use the ManagementObject constructor that accepts the object path to get the instance:

            ManagementObject computerSystem =
            new ManagementObject("Win32_ComputerSystem.Name='SomeName'");

            but you would have to get the computer name that you want to change first.

            In January you said "Money in April" - That was two years ago! B. Python

            M Offline
            M Offline
            mcldev
            wrote on last edited by
            #5

            Thanks!

            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