天天看點

通過AD域遠端修改計算機名(含批量腳本)

公司内的計算機名常常需要統一規範,但告知員工整改之後往往整改進度推進緩慢,那麼有沒有什麼方法能遠端修改掉不合規的機器名呢?

答案是肯定的,隻需在域控執行以下執行以下指令:

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檔案,而後加殼,避免該問題。

繼續閱讀