Windows PowerShell批量部署hyper - v虚机

  第一步:制作模板VHDX文件(略);   第二步:制作初始化系统脚本(自动设置IP、瓦等);         将以下2个脚本放置到模板VHDX中的C: \ Windows \安装\脚本目录下:   第1个脚本:FirstRun.ps1   #获取计算机名称   $ VMname=主机名      #根据计算机名称获取IP地址后6位   (int) IP1=VMname.Substring美元($ VMname.Length 作用;6,3)   (int) IP2=VMname.Substring美元(VMname.Length 美元;作用;3、3)      #设定FSNNetwork   IPaddr1=IPaddr1美元=172.1。,+,IP1 美元;+,“。”,+,IP2美元   New-NetIPAddress  -InterfaceIndex  12, -IPAddress  IPaddr1 美元;-PrefixLength  16, -DefaultGateway  172.1.0.250   Set-DnsClientServerAddress  -InterfaceIndex  12, -ServerAddresses  (“172.1.0.1”、“172.1.0.2”)   退出      第2个脚本:SetupComplete.cmd (该脚本名称必须为SetupComplete)   Powershell  -Command “C: \ Windows \安装\ Scripts \ FirstRun.ps1”   timeout  3   gpupdate /力   cd  C: \ Windows \ System32系统   wuauclt.exe /DetectNow /UpdateNow   ::cd  C: \ FalconAgent   ::falcon-agent.exe  -service 安装   ::sc.exe  config  falconagent =delayed-auto开始   ::falcon-agent.exe  -service 开始   del  C: \ Windows \安装\ \ FirstRun.ps1的脚本   del  % 0         第三步:创建自动化部署脚本   #,================================,#   #,,,,Create-VM_V1.0 (Windows) .PS1 ,, #,,,   #,,,通过PowerShell创建hyper - v虚机,,#,,   #,================================,#   #参数(VM_Name, VM_HostName美元)      Write-Host  -NoNewline  -ForegroundColor  Magenta “请输入要创建的虚机名称(如:VWSR * * * * * *)”   [String] VM_Name 美元;=Read-Host   Write-Host  -NoNewline  -ForegroundColor  Magenta “请输入需要放在哪台宿主机上(如:PWSR * * * * * *)”   [String] $ VM_HostName=Read-Host   Write-Host  -NoNewline  -ForegroundColor  Magenta “要部署App 服务器还是SQL 服务器?默认App 服务器回车即可(如:SQL 回车或者2,回车)'   [String] $ VM_OSClass=Read-Host      #判断虚机名称是否为大写字母   if  (VM_Name 美元;| findstr “vwsr”)   ,,,{Write-Host  -NoNewline  -ForegroundColor  Yellow “注意:虚机名需要使用大写字母! ! !”   ,,,,break },   其他的   ,,{,}   ,,   $ VM_Memory=4 gb   $ VM_CpuCount=4   美元=所有者,“它”   Org=美元," sxleilong.com "   $ NetworkSwitch=,“FSNNetwork”=# $ AdminPassword “Administrator 密码”   路径=美元,“D: \ VM_Deploy_Script”   DjoinSource=, SourcePath 美元+,“\”,+,VM_Name 美元;+ ' . txt '      #如果判断要部署App 服务器还是SQL 服务器,默认是App 服务器   if  (($ VM_OSClass  -eq “2”),或是;(VM_OSClass 美元;-eq “sql”),或是;(VM_OSClass 美元;-eq “sql”))   ,,{Template_VHDX=, SourcePath 美元+,“\”,+,“En_WinSrv12R2_For_SQL.vhdx”   ,,,Write-Host  -ForegroundColor  Green “您选择安装SQL虚机! ! !”   ,,,}   其他的   ,,{Template_VHDX=, SourcePath 美元+,“\”,+,“En_WinSrv12R2_For_APP.vhdx”   ,,,Write-Host  -ForegroundColor  Green “您选择安装应用虚机! ! !”   ,,,}      #如果判断如果虚机所在宿主机为管理系,虚机文件存放位置为E盘,,   if  ((, VM_HostName 美元;-eq “PWSR252001”,),或是;(VM_HostName 美元;-eq “PWSR252002”,))   ,,,{VM_Path 美元;=,“E: \”, +, VM_HostName 美元;+,“_E_VMs”   ,,,,VM_RemotePath 美元;=,“\ \”,+,VM_HostName 美元;+,' \ E $ \ ', +, VM_HostName 美元;+,‘_E_VMs’,}   其他的   ,,,{VM_Path 美元;=,“D: \”, +, VM_HostName 美元;+,“_D_VMs”   ,,,,VM_RemotePath 美元;=,“\ \”,+,VM_HostName 美元;+,' \ D $ \ ', +, VM_HostName 美元;+,‘_D_VMs’,},   $ VM_VHDPath =, VM_Path 美元;+,“\”,+,VM_Name 美元;+,' \ ' \ Virtual  Hard 磁盘+,VM_Name 美元;+,“.vhdx”   $ VM_VHDRemotePath =, VM_RemotePath 美元;+,“\”,+,VM_Name 美元;+,“\ \ Virtual  Hard 磁盘”,+,VM_Name 美元;+,“.vhdx”      #如果判断是否为0系管理服务器,用以自动加入不同OU   if  (VM_Name 美元;| findstr “VWSR000”)   ,,{,$ VM_MachineOU=癘U=MgmtServers, OU=sxleilong, DC=sxleilong, DC=cn”,}   其他的   ,,{,$ VM_MachineOU=癘U=NewInfra, OU=Production 服务器,OU=sxleilong, DC=sxleilong, DC=cn”,}      #编辑应答文件模板   $ UnattendTemplate=,“Unattend-template.xml”   $ Unattendfile=, New-Object  XML   (Unattendfile.Load美元路径+“\”+ UnattendTemplate美元)   美元Unattendfile.unattend.settings.component [2] .ComputerName=$ VM_Name   美元Unattendfile.unattend.settings.component [2] .RegisteredOrganization=$ Org   美元Unattendfile.unattend.settings.component [2] .RegisteredOwner=$所有者   美元Unattendfile.unattend.settings.Component [3] .RegisteredOrganization=$ Org   美元Unattendfile.unattend.settings.Component [3] .RegisteredOwner=$所有者   # $ UnattendFile.unattend.settings.component [3] .UserAccounts.AdministratorPassword.Value=https://www.yisu.com/zixun/AdminPassword美元   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Windows PowerShell批量部署hyper - v虚机