2

Might someone be able to point me towards a conclusive resource to learn how to remotely change a computer name on a Windows Server 2008 machine using C#

I've looked at lots of sites for help and now in day two of my task and not really any closer (other than deciding WMI is pretty much my only option) Totally out of my normal skillset so I guess pretty much any info would be nice, but especially anything having to do with changing a computer name remotely. (this would occur right after I remotely spin up a virutal from an image...and yes, i realize a reboot will be required)

thanks

J Benjamin
  • 4,722
  • 6
  • 29
  • 39
  • Can I ask the reason why you would need to do this? – Mark Kram Jun 01 '11 at 20:52
  • So are you looking for example code on how to use WMI classes in C# to do the rename? – squillman Jun 01 '11 at 20:53
  • Maybe powershell might be easier to use to accomplish what you need. - http://poshcode.org/541 – istudy0 Jun 01 '11 at 20:55
  • @Mark, I've been tasked with using Amazon's cloud to spin up instances of an image. The network gurus here know more details but from what I understand, our data center firewalls are going to need to know host names and mac address so they can open up ports or something like that. With Amazon EBS instances, the MAC and computer name are generated by Amazon and I don't know what they are until the instance is running...so I need to remotely rename it and reboot to get it to the ready state they've defined. – J Benjamin Jun 01 '11 at 22:52
  • @squillman, that would be ideal but I'm not above doing my own footwork either. I think I'm making progress though...research has taken me to the System.Management namespace. I've been trying to get a handle on it all day but meetings have really squelched progress so far. I'm starting in on that area now. – J Benjamin Jun 01 '11 at 22:54
  • @poshcode, ya that's my plan B. I'd rather not do it that way because of the extra future maintenance that I'd be passing on to someone. I'd like to have it all built in and I'm sure the network guys would appreciate having it all in one little package (and actually, it's going into our system for load balancing and automated server instance creation so fewer outside variables to maintain the better – J Benjamin Jun 01 '11 at 22:57

1 Answers1

5

Here is a nice link that discusses it in detail and also deals with active directory membership and machine naming in addition to the local machine name. http://derricksweng.blogspot.com/2009/04/programmatically-renaming-computer.html

(Btw, should you have to deal with Active Directory naming, I would consider using the ComputerPrincipal class from the System.DirectoryServices.AccountManagement namespace vice anything from System.DirectoryServices namespace that was used in the blog post.)

Tweaked code from the blog post (you will need to add a reference to System.Management to your project):

    public void RenameRemotePC(String oldName, String newName, String domain, NetworkCredential accountWithPermissions)
    {
        var remoteControlObject = new ManagementPath
                                      {
                                          ClassName = "Win32_ComputerSystem",
                                          Server = oldName,
                                          Path =
                                              oldName + "\\root\\cimv2:Win32_ComputerSystem.Name='" + oldName + "'",
                                          NamespacePath = "\\\\" + oldName + "\\root\\cimv2"
                                      };

        var conn = new ConnectionOptions
                                     {
                                         Authentication = AuthenticationLevel.PacketPrivacy,
                                         Username = oldName + "\\" + accountWithPermissions.UserName,
                                         Password = accountWithPermissions.Password
                                     };

        var remoteScope = new ManagementScope(remoteControlObject, conn);

        var remoteSystem = new ManagementObject(remoteScope, remoteControlObject, null);

        ManagementBaseObject newRemoteSystemName = remoteSystem.GetMethodParameters("Rename");
        var methodOptions = new InvokeMethodOptions();

        newRemoteSystemName.SetPropertyValue("Name", newName);
        newRemoteSystemName.SetPropertyValue("UserName", accountWithPermissions.UserName);
        newRemoteSystemName.SetPropertyValue("Password", accountWithPermissions.Password);

        methodOptions.Timeout = new TimeSpan(0, 10, 0);
        ManagementBaseObject outParams = remoteSystem.InvokeMethod("Rename", newRemoteSystemName, null);

    }
Jason
  • 4,897
  • 2
  • 33
  • 40
  • awesome, thanks for the response. I'll check it out asap...one thing though, I'm not finding that AuthenticationLevel.PacketPrivacy property...appears that my only options there are MutualAuthRequested, MutualAuthRequired, and None – J Benjamin Jun 08 '11 at 18:26
  • whoops, nevermind, looks like I might have got the system.net.security namespace mixed in there (which also has an AuthenticationLevel class apparently) – J Benjamin Jun 08 '11 at 18:28
  • I've had somewhat of a chance to look at this....all afternoon actually. Wow, thanks for the link. I haven't had a successful test yet but it looks very promising....if I can't do it with something this promising then I'm conceding defeat and taking the powershell route. – J Benjamin Jun 08 '11 at 20:52
  • 100% successful proof case written! Awesome man, I dunno how many people told me it could not be done remotely. :) – J Benjamin Jun 08 '11 at 22:23