zoukankan      html  css  js  c++  java
  • 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:jda.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:JoinDomainjoin.vbs"
    schtasks.exe /create /tn $Task_name /sc onlogon  /delay 0000:10  /ru administrator  /it  /tr $Task_cmd /v1 /z /rl highest #在用户登录后运行脚本
  • 相关阅读:
    在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好
    # 会员注册与登录模块
    文本文件从磁盘读取、写入
    简单的web三层架构系统【第五版】
    Nginx负载均衡中后端节点服务器健康检查的一种简单方式
    编译安装php-7.1.17及部分扩展
    wkhtmltopdf 安装过程不包含php扩展部分
    Centos6下安装中文字体
    xen 配置vm 跟随xen server一起启动
    CENTOS 升级Nodejs 到最新版本
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/14012475.html
Copyright © 2011-2022 走看看