使用 C# 远程更改 Windows Server 2008 计算机的计算机名称?

发布于 2024-11-13 08:03:31 字数 243 浏览 4 评论 0原文

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

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2024-11-20 08:03:31

这是一个很好的链接,它详细讨论了它,并且除了本地计算机名称之外还处理活动目录成员身份和计算机命名。
http://derricksweng.blogspot.com/2009/04/programmatically-renaming -computer.html

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

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

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

    }

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

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文