通过AD域远程修改计算机名(含批量脚本)

来源:互联网 发布:网络黑白txt下载 编辑:程序博客网 时间:2024/05/17 17:45

公司内的计算机名常常需要统一规范,但告知员工整改之后往往整改进度推进缓慢,那么有没有什么方法能远程修改掉不合规的机器名呢?

答案是肯定的,只需在域控执行以下执行以下命令:

netdom renamecomputer 要修改的计算机名 /newname:新的计算机名 /userd:域名\管理员名 /password:密码

之后弹出一个询问框,输入y即可,如果不想进行确认,一律强制执行,则在上述命令行后加上参数 /force。

修改计算机名后,用户需重启计算机才能生效,也可通过下面的命令远程重启对方机器:

/usero:本地管理员账号 /passwordo:本地管理员密码 /reboot:过多少秒自动重启

 

一个例子:

netdom renamecomputer oldname /newname:newname /userd:hirain.com\Administrator /password:123456

/usero:localAdmin /passwordo:ABCDEF /reboot:1


问题二,如果现在公司内有一大批机器需要修改机器名,如何批量实现?

我的想法是编写一个批量脚本生成程序,建一个csv文件,将现在的机器名放在第一列,想要修改成的用户名放在第二列,运行脚本自动生成批量脚本。

脚本生成程序的C#代码如下:


using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;namespace userFliter{    class Program    {        static void Main(string[] args)        {            String[] org = File.ReadAllLines("d:/1.csv", Encoding.Default);            String newText = null;            newText = coding(org, newText);            StreamWriter sw = new StreamWriter("d:/code.bat");            sw.Write(newText);            sw.Flush();            sw.Close();        }        static private String coding(String[] oldLine, String newText)        {            String oldName = null;            String newName = null;            for (int i = 0; i < oldLine.Length; i++)            {                oldName = oldLine[i].Substring(0, oldLine[i].IndexOf(","));                newName = oldLine[i].Substring(oldLine[i].IndexOf(",") + 1);                newText = newText + "echo "+oldName+" >>D:\\log.txt\r\n";                newText = newText + "netdom renamecomputer " + oldName + " /newname:" + newName + " /userd:域\\管理员 /passwordd:管理员密码 /force>>D:\\log.txt\r\n";                                newText = newText + "echo.>>D:\\log.txt\r\n";            }            newText = newText + "echo ok!\r\n";            newText = newText + "pause\r\n";            return newText;        }    }}


生成的脚本打开后能看到管理员密码,可以通过软件转化为exe文件,而后加壳,避免该问题。

0 0
原创粉丝点击