vbs調用批處理、PowerShell傳參,加域等


電腦啟動后,自動運行任務計划,運行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 #在用戶登錄后運行腳本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM