Link to home
Start Free TrialLog in
Avatar of imkiosks
imkiosks

asked on

How to change a computer name with vb .net

Hi all!
I need specific vb.net code that will allow me to change the computer name. I have seen all the posts here, and have tried the many different solutions, but to no avail.  No matter what I try, the computer name is not changing - not in the properties window, or the registry (yes, I am rebooting manually after the code runs. Do I need to reboot from code???)

I am using vb .net 2003 (yes, I know 2.0/2005 has been out for a while now - just haven't gotten to it yet). I am testing this on a Windows 2000 Pro machine (is that the problem?) The target machine has Windows XP Pro.

Anyone have a good solid solution out there??
Thanks, Sharone.
Avatar of sj_hicks
sj_hicks
Flag of Australia image

I recommend using WMI to change the computer name.  Use Win32_ComputerSystem.Rename.  You'll need to add a reference to System.Management to yourt project.
Avatar of imkiosks
imkiosks

ASKER

hicks,
Are you willing to give me a little more?  I am not very familiar with using the WMI in this fashion.
I added the reference, and created the computer system class:

ComputerSystemClass = New System.Management.ManagementClass("Win32_ComputerSystem")

I'm not sure where to go from here. I read up on the rename method, but none of the examples I've seen so far really "click" with me.

Can you shed more light?
Thanks
try something like

 Try
            Dim mComputerSystem As New ManagementClass("Win32_ComputerSystem")
            Dim outParams As ManagementBaseObject
            'ManagementObjectCollection objMOC = objMC.GetInstances(); 
            Dim objNewComputerName As ManagementBaseObject = mComputerSystem.GetMethodParameters("Rename")
            'foreach( outParams in 
            'objNewComputerName. 
            objNewComputerName("Name") = "newname"
            objNewComputerName("UserName") = "administrator"
            objNewComputerName("Password") = "password"
            'int val = Convert.ToInt32(outParams["ReturnValue"]); 
 
            outParams = mComputerSystem.InvokeMethod("Rename", objNewComputerName, Nothing)
        Catch ex As Exception
            MessageBox.Show("Unable to Set Computer Name: " + ex.Message)
        End Try

Open in new window

i think you would also need to restart to get it take place
armoghan,
I tried your code. It didn't work. No error was generated - the code seemed like it ran fine. After a reboot, though, the computer name did not change. I found another piece of code on a different forum, which enumerates through the "Win32_ComputerSystem" management class and shows all the available methods. The rename method was nowhere to be found. Note, that I am running this with .net 1.1 SP1, on Windows 2000 Pro. Do you think it would run differently (correctly) on a windows XP Pro machine??  I haven't had the chance to try that yet.

Thanks, Sharone.
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
armoghan,

I tried your new code exactly as you listed it. It did NOT work. The error message is:
"....This method is not implemented in any class".

I assume it is referring to the Rename method. Was this method not implemented in .net 1.1 SP1 yet?
Actually i do not have .net 1.1 with me. i have tested it with 2.0 and this works fine on two computers with different specification

Though ManagementObject was given in 1.1, and its methods should have been given at that time as well
Make sure you've added a .NET reference to System.Management in project properties, and have got "Import System.Managemenr" at the start of your code.
sj_hicks,
Of course. Thanks for that. I do.

armoghan,
I assume you tested it with 1.1 too?  :)   Any ideas moving forward?
Unfortuantely i do not have 1.1. so could not do that.

Why dont you upgrade to 2.0 :)
armoghan,
I finally got around to testing this solution on a windows XP machine and it works perfectly!  Thanks for that. Most of the machines we have out on the field are windows XP, so this solution will work for us. We do, however, still have a few windows 2000 images that we may need to figure something out.  Regardless, full points to you.  Thanks again.