博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows借助脚本实现自动化加域
阅读量:6703 次
发布时间:2019-06-25

本文共 6521 字,大约阅读时间需要 21 分钟。

Windows借助脚本实现自动化加域

说到自动加域,我们作为一个运维管理员来说,是必须要掌握了的 ,通过脚本提高管理员的工作效率,一般可以将自动加域脚本(前提是需要将脚本加密,内部有明文账户及密码)通过共享的方式然后用户手动执行,前一段时间领导也有这个需求,所以就总了一些,分享给有需要的。
一般分为两步,首先是计算更改计算机名,其次就是加域了;最后是需要将用户增加到管理员组中,保证用户有足够的权限进行操作。
首先是修改计算机名:执行会提示当前的计算机名,然后输入新的计算机名,单击确认即可;

Dim revalSet objnet = CreateObject ("WScript.Network")Set R = CreateObject("WScript.Shell")reval = InputBox ("Your ComputerName is:" & objnet.ComputerName,"Input Your new ComputerName")On Error Resume NextstrComputer = "."Set objWMIService = GetObject("winmgmts:" _    & "{impersonationLevel=impersonate}!\\" _    & strComputer & "\root\cimv2")Set colComputers = objWMIService.ExecQuery _    ("Select * from Win32_ComputerSystem")For Each objComputer in colComputers    errReturn = ObjComputer.Rename (reval)    If reval <> "" Then                 return=MsgBox ("Reboot Computer?",vbokcancel+vbexclamation,"tips!")                If return=vbok Then                        R.run("Shutdown.exe -r -t 0")                End if    End If Next

修改计算机名后,然后就是加域:

On Error Resume Nextdim domainset domain =createobject("wscript.shell")Const JOIN_DOMAIN = 1Const ACCT_CREATE = 2Const ACCT_DELETE = 4Const WIN9X_UPGRADE = 16Const DOMAIN_JOIN_IF_JOINED = 32Const JOIN_UNSECURE = 64Const MACHINE_PASSWORD_PASSED = 128Const DEFERRED_SPN_SET = 256Const INSTALL_INVOCATION = 262144'Domain NamestrDomain = "ixmsoft.com"'administrator member passwordstrPassword = "123"'administrator memberstrUser = "gavin"Set objNetwork =CreateObject("WScript.Network")strComputer = objNetwork.ComputerNameSet objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\"& _strComputer &"\root\cimv2:Win32_ComputerSystem.Name='" & _strComputer & "'")ReturnValue =objComputer.JoinDomainOrWorkGroup(strDomain, _strPassword, strDomain & "\"& strUser, NULL, _JOIN_DOMAIN + ACCT_CREATE)wscript.echo "Join Domain Success!"

最后就是将用户增加到管理员组中;

dmain_user="ixmsoft\zhangsan"Set objShell=wscript.createObject("wscript.shell") objshell.Run "net localgroup Administrators " & domain_user & " /add",0,true

以上脚本需要执行三次,但是比较麻烦,我们最终将三个脚本的功能合成一个,

执行后,会提示修改计算机名,然后提交后,系统会重启,重启后,会将计算机自动加域。

On Error Resume NextErr.ClearSet oWsh = CreateObject("WScript.Shell")sRegValue = oWsh.RegRead("HKLM\Software\Microsoft\Windows\CurrentVersion\Run\rr.vbs")If sRegValue = "" Then    Dim reval_all    Set objnet = CreateObject ("WScript.Network")    Set R = CreateObject("WScript.Shell")    reval_all = InputBox ("当前计算机名称:" & objnet.ComputerName & chr(10)&chr(13) & "当前登录名:" & objnet.username & chr(10)&chr(13)  &"请输入新的计算机名和登录账户,用”,“分开.(例子:zhangsan-pc,zhangsan)"  & chr(10)&chr(13) & "注:登陆账户为登陆此计算机的域账户。" ,"请输入计算名和登录名")    arr1 = split(reval_all,",")    reval = arr1(0)    On Error Resume Next    strComputer = "."    Set objWMIService = GetObject("winmgmts:" _        & "{impersonationLevel=impersonate}!\\" _        & strComputer & "\root\cimv2")    Set colComputers = objWMIService.ExecQuery _        ("Select * from Win32_ComputerSystem")    For Each objComputer in colComputers        errReturn = ObjComputer.Rename (reval)        If reval <> "" Then             Dim AutoRunProgram     Set AutoRunProgram = WScript.CreateObject("WScript.shell")     RegPath ="HKLM\Software\Microsoft\Windows\CurrentVersion\Run\"     Type_Name = "REG_SZ"     Key_Name = "rr.vbs"     strComputer = "."    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")     Set colItems = objWMIService.ExecQuery("Select * From Win32_Process Where Name = 'wscript.exe' OR Name = 'cscript.exe'")    For Each objItem in colItems        lmq= objItem.CommandLine        lmq= replace(lmq,Chr(34),",")        arr = split(lmq,",")    Next    Key_data = arr(3)    AutorunProgram.RegWrite regpath&Key_Name,Key_data,Type_Name     AutorunProgram.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\loginName",arr1(1),Type_Name                     return=MsgBox ("Please Reboot Computer?",vbokcancel+vbexclamation,"tips!")                    If return=vbok Then                            R.run("Shutdown.exe -r -t 0")                    End if        End If     NextEnd Ifif sRegValue <> "" then    dim domain    set domain =createobject("wscript.shell")    Const JOIN_DOMAIN = 1    Const ACCT_CREATE = 2    Const ACCT_DELETE = 4    Const WIN9X_UPGRADE = 16    Const DOMAIN_JOIN_IF_JOINED = 32    Const JOIN_UNSECURE = 64    Const MACHINE_PASSWORD_PASSED = 128    Const DEFERRED_SPN_SET = 256    Const INSTALL_INVOCATION = 262144    'Domain Name    strDomain = "ixmsoft.com"    'administrator member password    strPassword = "Bey0d101"    'administrator member    strUser = "addtodomain"    Set objNetwork =CreateObject("WScript.Network")    strComputer = objNetwork.ComputerName    Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\"& _    strComputer &"\root\cimv2:Win32_ComputerSystem.Name='" & _    strComputer & "'")    ReturnValue =objComputer.JoinDomainOrWorkGroup(strDomain, _    strPassword, strDomain & "\"& strUser, NULL, _    JOIN_DOMAIN + ACCT_CREATE)       dim wsh     set wsh=wscript.createobject("wscript.shell")    wsh.regdelete "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\rr.vbs"    wscript.echo "Join Domain Success!"     Dim oWshNetwork    Set oWshNetwork = WScript.CreateObject("WScript.Network")    strComputer = oWshNetwork.ComputerName    sFlag=0    On Error Resume Next    'Set colGroups = GetObject("WinNT://" & strComputer & "/Administrators")    'For Each objUser In colGroups.Members    'Next    strTestString = "/" & strComputer & "/"    Set colGroups = GetObject("WinNT://" & strComputer & "/Administrators")    For Each objUser In colGroups.Members        If InStr(objUser.AdsPath, strTestString) Then            'Wscript.Echo "Local user: " & objUser.Name        'msgbox(objUser.ADsPath)        Else            IF objuser.Name = "DomainUser" Then               sFlag=1                                   end if         End If    Next    if sFlag=0 then     'Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")    '    Set oWsh = CreateObject("WScript.Shell")    reval = oWsh.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\loginName")        wsh.regdelete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\loginName"            adspath = "WinNT://ixmsoft/" & reval            'msgbox(adspath)            Set objUser = GetObject(adspath)            colGroups.Add(objUser.ADsPath)    end if               wscript.echo  "Added Finish"end if

转载于:https://blog.51cto.com/gaowenlong/2066290

你可能感兴趣的文章
MusicXML 3.0 (30) - 和弦图表
查看>>
大话 char、varchar、 nchar、nvarchar之间"剪不断理还乱"的关系
查看>>
系统数据库
查看>>
JAVA: java产生随机数的几种方式
查看>>
调试发现的小错误
查看>>
c#中使用NetCDF存储二维数据的读写操作简单应用
查看>>
系统操作日志设计(转载)
查看>>
sqlldr 学习总结1
查看>>
剑指 offer set 17 判断一棵树是否是平衡树
查看>>
Leetcode: Path Sum II
查看>>
ShortcutMapper – 热门应用程序的可视化快捷键
查看>>
CSS3 实现的一个简单的"动态主菜单" 示例
查看>>
delphi 新版内存表 FDMemTable
查看>>
轻量级web富文本框——wangEditor使用手册(1)——基本应用 demo
查看>>
Java在的时候,类定义HashSet初始化方法
查看>>
利用JMX统计远程JAVA进程的CPU和Memory---jVM managerment API
查看>>
android弹出时间选择框
查看>>
移动终端处理器构成和基带芯片概述
查看>>
Android 动态加载 (一) 态加载机制 案例一
查看>>
我的angularjs源码学习之旅1——初识angularjs
查看>>