电脑启动后,自动运行任务计划,运行vbs脚本修改管理员密码,然后引导用户自行输入个性化内容,再然后调用ps1脚本修改计算机名、加域、添加本地管理员权限
join.vbs脚本内容如下:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''脚本说明''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '该脚本用来修改本地管理员密码,自动连接WiFi,提示用户输入域账号、员工编号, '然后调用PowerShell脚本修改计算机名、加域、添加域账号到本地管理员组
'脚本运行完成后删除自身、ps1脚本、任务计划
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' dim wshell,PS_ScriptName,UserName,UserCode,Inputcontent set wshell=createobject("wscript.shell") set fs =createobject("scripting.filesystemobject") '修改本地管理员密码 wshell.run "net user administrator password" ,vbhide '定义加域脚本名称、错误日志、WiFi配置文件、输出文件名称 PS_ScriptName = "JoinDomain.ps1" error_logName = "errorlog.log" wlan_profileName="wlan.xml" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''定义函数''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '定义输入域账号、员工编号函数 Function Inputuser(prompting) inputstr = inputbox(prompting &":") inputstr = Trim(inputstr) if inputstr = Empty Then Inputuser(prompting) 'wscript.quit else inputstr = split(inputstr,"@")(0) Inputcontent = inputstr End if End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''脚本开始''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '拼接脚本路径 PS_ScriptPath = wshell.CurrentDirectory + "\" + PS_ScriptName error_logPath = wshell.CurrentDirectory + "\" + error_logName wlan_profile = wshell.CurrentDirectory + "\" + wlan_profileName 'msgbox(PS_ScriptPath & UserName & UserCode) '自动连接WiFi wshell.run "netsh wlan add profile filename="&wlan_profile&"",vbhide,true WScript.Sleep 1000 wshell.run "netsh wlan connect name=wlan",vbhide,true WScript.Sleep 2000 msgbox("即将开始为您分配系统权限,请先确认电脑已接入职场有线/无线网络,然后点击“确定”按钮开始配置。") Inputuser("请输入您的域(邮箱)账号,如 zhangsan") UserName = "domain\" + Inputcontent Inputuser("请输入您的员工编号") UserCode = Inputcontent wshell.run "mshta vbscript:msgbox(""正在设置系统权限,需耗时大约30秒,请稍后..."",0,"""")(window.close)" '设置允许PowerShell脚本运行策略 wshell.run "powershell.exe Set-ExecutionPolicy bypass -force",vbhide,true '运行PowerShell脚本加域、域账户加入本地管理员组 command = "powershell.exe "&PS_ScriptPath&" "&UserCode&" "&UserName&" " wshell.run command,vbhide,true '检查是否有错误输出 if fs.fileExists(error_logPath) Then set ts=fs.opentextfile(error_logPath) ts = ts.ReadAll() ts = ts + "请联系IT桌面工程师协助处理" msgbox ts fs.DeleteFile(error_logPath), True else msgbox("权限设置成功,待电脑自动重启后,请使用域账号登录") fs.DeleteFile(WScript.ScriptName),True fs.DeleteFile(PS_ScriptPath),True fs.DeleteFile(wlan_profile),True wshell.run "schtasks.exe /delete /tn JoinDomain /f",vbhide,true End if '关闭msgbox提示窗口 wshell.run "taskkill.exe /F /IM mshta.exe",vbhide,true '设置禁止PowerShell脚本运行策略 wshell.run "powershell.exe Set-ExecutionPolicy restricted -force",vbhide '自动重启 WScript.Sleep 1000 wshell.run "shutdown.exe -r -t 3",vbhide,true
Joindomain.ps1脚本内容如下:
param($UserCode,$UserName) #$UserCode|Out-File D:\jd\a.txt -Append $UserName_jd = "join-domain-user" $Password_jd = "joinpassword" $DomainName = "xx.com" #定义错误日志输出位置 $Currentpath = Split-Path -parent $MyInvocation.MyCommand.Definition $errlogpath = Join-Path $Currentpath "errorlog.log" #检查域名是否可以Ping通 if ( Test-Connection $DomainName -Count 1 -Quiet ) { $Password_sec = ConvertTo-SecureString $Password_jd -AsPlainText -Force $cred = New-Object System.Management.Automation.PSCredential($UserName_jd,$Password_sec) try{ $ErrorActionPreference='stop' #重命名计算机名称,检查是PC还是Notebook $chassistypes = (gwmi win32_systemenclosure |select chassistypes).chassistypes if($chassistypes -eq 9 -or $chassistypes -eq 10 -or $chassistypes -eq 14) {$model = "-NB"} else {$model = "-PC"} if(gwmi win32_battery) {$model = "-NB"} else {$model = "-PC"} $computer_newname = $UserCode.ToUpper() + $model + (Get-Date -UFormat "%M").tostring() Rename-Computer -NewName $computer_newname Start-Sleep -Seconds 3 #使用新的计算机名称加域 Add-Computer -NewName $computer_newname -DomainCredential $cred -DomainName $DomainName Start-Sleep -Seconds 4 #将域账号加入本地管理员组 Add-LocalGroupMember -Group "Administrators" -Member $UserName Start-Sleep -Seconds 1 #net.exe localgroup administrators $UserName /add } catch{ $_.exception.message | Out-File $errlogpath -Encoding default -Append } } else { "$DomainName 无法Ping通,请确保电脑已接入有线/无线网络。" | Out-File $errlogpath -Encoding default -Append }
创建任务计划:
xcopy.exe "d:\JoinDomain\*" "C:\JoinDomain\" /Y /S /Q #创建任务计划 $Task_name = "JoinDomain" $Task_cmd = "C:\JoinDomain\join.vbs" schtasks.exe /create /tn $Task_name /sc onlogon /delay 0000:10 /ru administrator /it /tr $Task_cmd /v1 /z /rl highest #在用户登录后运行脚本