PowerShell bulk Deploy Hyper-V Windows virtual machine

Source: Internet
Author: User

First step: Make template Vhdx file (slightly); second step: Make initialization system script (auto set IP, GW, etc.); Place the following 2 scripts in the C:\Windows\Setup\Scripts directory in the template VHDX: 1th script: firstrun.ps1# Gets the computer name $vmname=hostname# the IP address based on the computer name 6-bit [int] $IP 1= $VMname. Substring ($VMname. length - 6,3) [int] $IP 2=$ Vmname.substring ($VMname. length - 3,3) #设定FSNNetwork $IPaddr 1= $IPaddr 1 = ' 172.1. '  +  $IP 1 +  '. '  +  $IP 2new-netipaddress -interfaceindex 12 -ipaddress  $IPaddr 1 - Prefixlength 16 -defaultgateway 172.1.0.250set-dnsclientserveraddress -interfaceindex  12 -ServerAddresses  ("172.1.0.1", "172.1.0.2") exit 2nd Script:setupcomplete.cmd  ( The script name must be Setupcomplete) powershell -command  "C:\Windows\Setup\Scripts\ ' Firstrun.ps1 '" timeout  3gpupdate /forcecd  c:\windows\system32wuauclt.exe /detectnow /updatenow::cd   c:\falconagent::falcon-agent.exe -service install::sc.exe config  falconagent  start=delayed-auto::falcon-agent.exe -service startdel c:\windows\setup\scripts\firstrun.ps1del %0 Step Three: Create an automated deployment script # ======= ========================= ##    create-vm_v1.0 (Windows). ps1   #   #    creating Hyper-V virtual machines through PowerShell   #  #  ================================ # #Param ($VM _name, $VM _hostname) write-host -nonewline - foregroundcolor magenta  ' Enter the name of the virtual machine you want to create (for example: vwsr******) ' [String] $VM _name = read-hostwrite-host  -NoNewline -ForegroundColor Magenta  ' Please enter which host you need to put on (e.g. pwsr******) ' [String] $VM _hostname=  Read-HostWrite-Host -NoNewline -ForegroundColor Magenta  ' to deploy App server or SQL  server? The default App server carriage return (such as:sql  carriage return or 2  carriage return) ' [String] $VM _osclass= read-host# determine if the virtual machine name is uppercase if  ($VM _ name |findstr  ' VWSR ')    { Write-Host -NoNewline -ForegroundColor  yellow  ' NOTE: The virtual machine name needs to use uppercase letters!!! '  &Nbsp;   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 or Sql server, default is app  Serverif  ($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   "You choose to install SQL virtual machine!!!" "    }else   {$Template _vhdx=  $SourcePath  +  '  +  ' EN_WINSRV12R2_FOR_APP.VHDX '     Write-Host -ForegroundColor Green  ' You choose to install the app virtual machine!!! "&nbSP;  &NBSP: #if判断如果虚机所在宿主机为管理系, the virtual machine file is stored in 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系管理服务器 to automatically add different ouif  ($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"  }# Edit answer File template $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模板VHDXMount-diskimage   $Template _vhdx$driveletter= (get-diskimage  $Template _vhdx | get-disk |  get-partition | where-object type -eq  "Basic"). DriveLetter) + ":" #注入Unattend. xml file into the VHDX $unattendxml_destination= $Driveletter + "\windows\system32\sysprep\ Unattend.xml "copy-item  $UnattendXml   $UnattendXml _destination -force# generation offline  JoinDomain file djoin /provision /domain sxleilong.com /machine  $VM _name /machineou   $VM _machineou /savefile  $DjoinSource  /reuse# inject offline joindomain file and remove history djoinfile$ djoindestination=  $Driveletter + ' \ Windows ' $His _djoinfile=  $DjoinDestination  + ' \ ' + ' vwsr*.txt ' remove-item -path  $His _djoinfile -forcecopy-item  $DjoinSource    $DjoinDestination  -ForceDjoin /RequestODJ /Loadfile   "$VM _name.txt"  /windowspath  $DjoinDestination  /localos# Injection boot Run script $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 template vhdxdismount-diskimage  $Template _vhdx#remove answer file and Djoin file remove-item -path $ djoinsourceremove-item -path  $UnattendXml #copy Virtual machine vhdx file new-item -path  $VM _remotepath\ $VM _ name -name  "Virtual hard disks"  -itemtype directorycopy-item -path $ template_vhdx -destination  $VM _vhdremotepath# New virtual machine new-vm –name  $VM _name -computername   $VM _hostname –generation 2 -memorystartupbytes  $VM _memory -switchname $ networkswitch –path  $VM _path -vhdpath  $VM _vhdpath# Set the number of CPUs set-vm -name  $VM _name -computername  $VM _hostname - processorcount  $VM _cpucount# Disable dynamic memory function #set-vmmemory -name  $VM _name -computername  $VM _ hostname -dynamicmemoryenabled  $False    #添加DVDDriveGet-vm -name  $VM _name - computername  $VM _hostname | add-vmdvddrive -controllernumber 0# Set vlanid#set-vmnetworkadaptervlan -computername  $VM _ hostname -vmname  $VM _name -access -vlanid 1112# assigning static macaddress#import-module through SCVMM  virtualmachinemanagerRead-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 virtual machine #start-vm -name  $VM _name -computername  $VM _hostnamestart-scvirtualmachine -vm  $VM _name 

This article is from the "Ray" blog, be sure to keep this source http://sxleilong.blog.51cto.com/5022169/1916614

PowerShell bulk Deploy Hyper-V Windows virtual machine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.