您好,登錄后才能下訂單哦!
第一步:制作模板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-diskp_w_picpath $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-diskp_w_picpath $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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。