Author Topic: programmatically renaming a computer - question  (Read 849 times)

0 Members and 1 Guest are viewing this topic.

Ecrofirt

  • Heavy Metal Jesus
  • Senior Member
programmatically renaming a computer - question
« on: May 26, 2008, 02:13:17 PM »
OK, so I'm writing a program to execute a bunch of computer tasks. One of the things it's going to be able to do is rename the computer.

I've found 3 easy ways to rename a computer:
1) Win32_ComputerSystem.Rename() (WMI) - http://msdn.microsoft.com/en-us/library/aa393056(VS.85).aspx
2) SetComputerName() (Windows API call) - http://msdn.microsoft.com/en-us/library/ms724930(VS.85).aspx
3) SetComputerNameEx() (Windows API call) - http://msdn.microsoft.com/en-us/library/ms724931(VS.85).aspx

Now, all three of these can clearly rename a computer. Here's where I've got some questions, though.

The computer that's going to be renamed is likely to be on a domain, Because of this, I'm not exactly sure which function I'm supposed to use.

SetComputerName only seems to set the NetBIOS name of the computer, so I've got no idea how that's going to to affect computers in a domain.

SetComputerNameEx will allow me to set the NetBIOS name or the DNS hostname of the computer.

Win32_ComputerSystem.Rename doesn't specify what it renames.

Also, if I change the name, is that going to screw up anything in Active Directory? I ask because I got something sort-of working at work, but it took my computer out of the OU it was in and put it back in the default OU. At least  ithink it did that. It may have actually removed my PC from the domain, the more that I think about it.

Anyone with any helpful ideas is awesome.
8=D

Van Cruncheon

  • live mas or die trying
  • Banned
Re: programmatically renaming a computer - question
« Reply #1 on: May 26, 2008, 02:21:21 PM »
use the WMI API, since its behavior is the same as renaming a computer through the UI.
duc

Ecrofirt

  • Heavy Metal Jesus
  • Senior Member
Re: programmatically renaming a computer - question
« Reply #2 on: May 26, 2008, 02:23:13 PM »
use the WMI API, since its behavior is the same as renaming a computer through the UI.

OK, that's easy enough.

Any idea how (if at all) this will affect computers in a domain? Will it do anything to screw up the OU that the computer is in?
8=D

Van Cruncheon

  • live mas or die trying
  • Banned
Re: programmatically renaming a computer - question
« Reply #3 on: May 26, 2008, 02:27:52 PM »
since it requires an administrator domain account directly (also note: no impersonation context, so you might wanna create a "special" admin account that can only add machines to a domain and rename machines), it will attempt to add the machine into the domain. beyond that, i dunno, but i've never had a problem calling it.
duc

Ecrofirt

  • Heavy Metal Jesus
  • Senior Member
Re: programmatically renaming a computer - question
« Reply #4 on: May 26, 2008, 03:18:43 PM »
ah, hrm.

So, if I want to rename a computer that's not on a domain I'm not going to get any weird issues with this am I?
8=D

Van Cruncheon

  • live mas or die trying
  • Banned
Re: programmatically renaming a computer - question
« Reply #5 on: May 26, 2008, 03:21:53 PM »
shouldn't!*



*never tried it, myself.
duc

Ecrofirt

  • Heavy Metal Jesus
  • Senior Member
Re: programmatically renaming a computer - question
« Reply #6 on: May 26, 2008, 03:27:04 PM »
Well, sounds good enough then. I'll get this whipped up and test it out tomorrow.
8=D