PowerShell批量部署Hyper-V Windows虚机
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PowerShell批量部署Hyper-V Windows虚机相关的知识,希望对你有一定的参考价值。
第一步:制作模板VHDX文件(略); 第二步:制作初始化系统脚本(自动设置IP、GW等); 将以下2个脚本放置到模板VHDX中的C:\Windows\Setup\Scripts目录下: 第1个脚本:FirstRun.ps1 #获取计算机名称 $VMname=hostname #根据计算机名称获取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") exit 第2个脚本:SetupComplete.cmd (该脚本名称必须为SetupComplete) Powershell -Command "C:\Windows\Setup\Scripts\‘FirstRun.ps1‘" timeout 3 gpupdate /force cd C:\Windows\System32 wuauclt.exe /DetectNow /UpdateNow ::cd C:\FalconAgent ::falcon-agent.exe -service install ::sc.exe config falconagent Start=delayed-auto ::falcon-agent.exe -service start del C:\Windows\Setup\Scripts\FirstRun.ps1 del %0 第三步:创建自动化部署脚本 # ================================ # # Create-VM_V1.0(Windows).PS1 # # 通过PowerShell创建Hyper-V虚机 # # ================================ # #Param($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 Server还是SQL Server?默认App Server回车即可(如:sql 回车或者2 回车)‘ [String]$VM_OSClass= Read-Host #判断虚机名称是否为大写字母 if ($VM_Name |findstr ‘vwsr‘) { Write-Host -NoNewline -ForegroundColor Yellow ‘注意:虚机名需要使用大写字母!!!‘ break } Else { } $VM_Memory= 4GB $VM_CpuCount= 4 $Owner= "IT" $Org= "sxleilong.com" $NetworkSwitch= "FSNNetwork" #$AdminPassword = "Administrator Password" $SourcePath= "D:\VM_Deploy_Script" $DjoinSource= $SourcePath + ‘\‘ + $VM_Name +‘.txt‘ #if判断要部署App Server还是SQL Server,默认是App Server if (($VM_OSClass -eq ‘2‘) -or ($VM_OSClass -eq ‘sql‘) -or ($VM_OSClass -eq ‘SQL‘)) {$Template_VHDX= $SourcePath + ‘\‘ + ‘En_WinSrv12R2_For_SQL.vhdx‘ Write-Host -ForegroundColor Green "您选择安装SQL虚机!!!" } Else {$Template_VHDX= $SourcePath + ‘\‘ + ‘En_WinSrv12R2_For_APP.vhdx‘ Write-Host -ForegroundColor Green "您选择安装App虚机!!!" } #if判断如果虚机所在宿主机为管理系,虚机文件存放位置为E盘 if (( $VM_HostName -eq ‘PWSR252001‘ ) -or ($VM_HostName -eq ‘PWSR252002‘ )) { $VM_Path = ‘E:\‘ + $VM_HostName + ‘_E_VMs‘ $VM_RemotePath = ‘\\‘ + $VM_HostName + ‘\E$\‘ + $VM_HostName + ‘_E_VMs‘ } Else { $VM_Path = ‘D:\‘ + $VM_HostName + ‘_D_VMs‘ $VM_RemotePath = ‘\\‘ + $VM_HostName + ‘\D$\‘ + $VM_HostName + ‘_D_VMs‘ } $VM_VHDPath = $VM_Path + ‘\‘ + $VM_Name + ‘\Virtual Hard Disks\‘+ $VM_Name + ‘.vhdx‘ $VM_VHDRemotePath = $VM_RemotePath + ‘\‘ + $VM_Name + ‘\Virtual Hard Disks\‘ + $VM_Name + ‘.vhdx‘ #if判断是否为0系管理服务器,用以自动加入不同OU if ($VM_Name |findstr ‘VWSR000‘ ) { $VM_MachineOU= "OU=MgmtServers,OU=sxleilong,DC=sxleilong,DC=cn" } Else { $VM_MachineOU= "OU=NewInfra,OU=Production Servers,OU=sxleilong,DC=sxleilong,DC=cn" } #编辑应答文件模板 $UnattendTemplate= "Unattend-template.xml" $Unattendfile= New-Object XML $Unattendfile.Load($SourcePath+"\"+$UnattendTemplate) $Unattendfile.unattend.settings.component[2].ComputerName=$VM_Name $Unattendfile.unattend.settings.component[2].RegisteredOrganization=$Org $Unattendfile.unattend.settings.component[2].RegisteredOwner=$Owner $Unattendfile.unattend.settings.Component[3].RegisteredOrganization=$Org $Unattendfile.unattend.settings.Component[3].RegisteredOwner=$Owner #$UnattendFile.unattend.settings.component[3].UserAccounts.AdministratorPassword.Value=$AdminPassword #$UnattendFile.unattend.settings.component[3].autologon.password.value=$AdminPassword $UnattendXML=$SourcePath+"\"+$VM_Name+".xml" $Unattendfile.save($UnattendXML) #Mount模板VHDX Mount-diskimage $Template_VHDX $DriveLetter=((Get-DiskImage $Template_VHDX | get-disk | get-partition | Where-Object Type -eq "Basic").DriveLetter)+":" #注入Unattend.xml文件到VHDX中 $UnattendXml_Destination=$Driveletter+"\Windows\System32\Sysprep\unattend.xml" Copy-Item $UnattendXml $UnattendXml_Destination -Force #生成Offline JoinDomain文件 Djoin /Provision /Domain sxleilong.com /Machine $VM_Name /MachineOU $VM_MachineOU /Savefile $DjoinSource /Reuse #注入Offline JoinDomain文件,并移除历史DjoinFile $DjoinDestination= $Driveletter+‘\Windows‘ $His_DjoinFile= $DjoinDestination +‘\‘+‘VWSR*.txt‘ Remove-Item -Path $His_DjoinFile -Force Copy-Item $DjoinSource $DjoinDestination -Force Djoin /RequestODJ /Loadfile "$VM_Name.txt" /WindowsPath $DjoinDestination /LocalOS #注入开机运行脚本 $ScriptFolder=$DriveLetter+"\Windows\Setup\Scripts\" $Scriptname=$Scriptfolder+"FirstRun.ps1" If (Test-Path $ScriptFolder) {} Else { New-Item $ScriptFolder -ItemType Directory Copy-Item "$SourcePath\Scripts\*" $ScriptFolder -Force } #DisMount模板VHDX dismount-diskimage $Template_VHDX #Remove应答文件和Djoin文件 Remove-Item -Path $DjoinSource Remove-Item -Path $UnattendXml #Copy虚拟机VHDX文件 New-item -path $VM_RemotePath\$VM_Name -name "Virtual Hard Disks" -ItemType directory Copy-Item -Path $Template_VHDX -Destination $VM_VHDRemotePath #新建虚机 New-VM –Name $VM_Name -ComputerName $VM_HostName –Generation 2 -MemoryStartupBytes $VM_Memory -SwitchName $NetworkSwitch –Path $VM_Path -VHDPath $VM_VHDPath #设置CPU个数 Set-VM -Name $VM_Name -ComputerName $VM_HostName -ProcessorCount $VM_CpuCount #禁用动态内存功能 #Set-VMMemory -Name $VM_Name -ComputerName $VM_HostName -DynamicMemoryEnabled $False #添加DVDDrive Get-VM -Name $VM_Name -ComputerName $VM_HostName |Add-VMDvdDrive -ControllerNumber 0 #设置VLANID #Set-VMNetworkAdapterVlan -ComputerName $VM_HostName -VMName $VM_Name -Access -VlanId 1112 #通过SCVMM分配静态MACAddress #Import-Module virtualmachinemanager Read-SCVMHost -VMHost $VM_HostName |Out-Null $VM=Get-SCVirtualMachine -Name $VM_Name $Adapter=$VM.VirtualNetworkAdapters[0] Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $Adapter[0] -MACAddressType "Static" -MACAddress "00-00-00-00-00-00" |Out-Null #启动虚机 #Start-VM -Name $VM_Name -ComputerName $VM_HostName Start-SCVirtualMachine -VM $VM_Name
本文出自 “Ray” 博客,请务必保留此出处http://sxleilong.blog.51cto.com/5022169/1916614
以上是关于PowerShell批量部署Hyper-V Windows虚机的主要内容,如果未能解决你的问题,请参考以下文章
Hyper-V 2016 系列教程39 在 Windows 10中使用 Hyper-V 和 Windows PowerShell
Hyper-V 批量创建虚拟机自动改IP并配置PPPOE拨号
Win10+Hyper-V ttrss&rsshub部署过程