使用 C# 远程更改 Windows Server 2008 计算机的计算机名称? [英] Remotely change computer name for a Windows Server 2008 machine using C#?

查看:104
本文介绍了使用 C# 远程更改 Windows Server 2008 计算机的计算机名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人能够向我指出一个决定性的资源,以了解如何使用 C# 远程更改 Windows Server 2008 机器上的计算机名称

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#

我查看了很多网站寻求帮助,现在在我的任务的第二天并没有真正接近(除了决定 WMI 几乎是我唯一的选择)完全超出了我的正常技能集,所以我猜几乎没有信息会很好,但尤其是与远程更改计算机名称有关的任何事情.(这会在我从图像远程启动虚拟机后立即发生……是的,我意识到需要重新启动)

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)

谢谢

推荐答案

这里有一个很好的链接,详细讨论了它,除了本地机器名称之外,还处理活动目录成员资格和机器命名.http://derricksweng.blogspot.com/2009/04/programmatically-renaming-computer.html

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

(顺便说一句,如果您必须处理 Active Directory 命名,我会考虑使用 ComputerPrincipal 类来自 System.DirectoryServices.AccountManagement 命名空间,而不是来自博客文章中使用的 System.DirectoryServices 命名空间的任何内容.)

(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.)

调整了博客文章中的代码(您需要在项目中添加对 System.Management 的引用):

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);

    }

这篇关于使用 C# 远程更改 Windows Server 2008 计算机的计算机名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆