Monday, 21 January 2013

Create-VM.PS1

  1. # Create-VM.ps1 
  2. # Script that creates VMs 
  3. # Version 1.0.0 - 20 Jan 2013 
  4. # See http://tfl09.blogspot.co.uk/2013/01/building-hyper-v-test-lab-on-windows-8.html 
  5.  
  6. # First define the Create-VM Function 
  7.  
  8. Function Create-VM { 
  9. #=================================================== 
  10. # Create a New VM 
  11. #=================================================== 
  12.  
  13. # Parameters are Name, Virtual Machine Path, path to reference Vhdx,
  14. # network switch to use, VM Memory, Unattend file, IP address and DNS
  15. # Server to set. Default values are specified in the Param block,
  16. # but these are normally overridden in the call to Create0VM 
  17.  
  18.  
  19. [Cmdletbinding()] 
  20. Param (  
  21.  $Name         = "Server", 
  22.  $VmPath       = "C:\v3", 
  23.  $ReferenceVHD = "C:\v3\Ref2012.vhdx", 
  24.  $Network      = "Internal", 
  25.  $VMMemory     = 512mb, 
  26.  $UnattendXML  = "C:\v3\unattend.xml", 
  27.  $IPAddr       = '10.0.0.250/24', 
  28.  $DnsSvr       = '10.0.0.10' 
  29. ) 
  30.  
  31. $Starttime = Get-Date 
  32. Write-Verbose "Starting Create-VM at $Starttime" 
  33. Write-verbose "Creating VM: [$name]" 
  34. Write-verbose "Path to VM : [$VMpath]" 
  35.  
  36. #    Set path to differencing disk location 
  37. $path = "$vmpath\$name.vhdx" 
  38. Write-Verbose "Creating Disk at [$path]" 
  39.  
  40. #    Add a new differencing VHDX, Based on parent parent 
  41. $vmDisk01 = New-VHD –Path $path -Differencing –ParentPath $ReferenceVHD -ErrorAction Stop 
  42. Write-Verbose "Added VM Disk [$VMdisk01], pointing to [ReferenceVHD]" 
  43.  
  44. #    Create a New VM 
  45. $VM = New-VM –Name $name –MemoryStartupBytes $VMMemory –VHDPath $VMDisk01.path -SwitchName $Network -Path $vmPath 
  46. Write-Verbose "VM [$name] created" 
  47.  
  48. # Mount the Disk into the VM 
  49. Mount-DiskImage -ImagePath $path 
  50. $VHDDisk = Get-DiskImage -ImagePath $path | Get-Disk 
  51. $VHDPart = Get-Partition -DiskNumber $VHDDisk.Number 
  52. $VHDVolumeName = [string]$VHDPart.DriveLetter 
  53. $VHDVolume = [string]$VHDPart.DriveLetter + ":" 
  54. Write-verbose "Volume [$Volumename] created in VM [$name]" 
  55.  
  56.  
  57. #    Get Unattended.XML file 
  58. Write-Verbose "Using Unattended XML file [$unattendXML]" 
  59.  
  60. #    Open XML file 
  61. $Xml = [xml](get-content $UnattendXML) 
  62.  
  63. #    Change ComputerName 
  64. Write-Verbose "Setting VM ComputerName to: [$name]" 
  65. $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-Shell-Setup" } | 
  66.  ForEach-Object { 
  67.    if($_.ComputerName) { 
  68.      $_.ComputerName = $name 
  69.    } 
  70. } 
  71.  
  72. #    Change IP address 
  73. Write-Verbose "Setting VM ComputerName to: [$name]" 
  74. $Xml.unattend.settings.component | Where-Object { $_.Name -eq "Microsoft-Windows-TCPIP" } | 
  75.   ForEach-Object { 
  76.  
  77.     if($_.Interfaces) { 
  78.       $ht='#text' 
  79.       $_.interfaces.interface.unicastIPaddresses.ipaddress.$ht = $IPAddr 
  80.   } 
  81. } 
  82.  
  83. #    Change DNS Server address 
  84. #    Use obscure way to create the #TEXT node 
  85. Write-Verbose "Setting VM DNS address to: [$DNSSvr]" 
  86. $Xml.Unattend.Settings.Component | Where-Object { $_.Name -eq "Microsoft-Windows-DNS-Client" } | 
  87.   ForEach-Object { 
  88.       if($_.Interfaces) { 
  89.       $ht='#text' 
  90.       $_.Interfaces.Interface.DNSServerSearchOrder.Ipaddress.$ht = $DNSSvr 
  91.   } 
  92. } 
  93.  
  94. #    Save XML File on Mounted VHDX differencing disk 
  95. $xml.Save("$VHDVolume\Unattend.XML") 
  96. Write-Verbose "Unattended XML file saved to vhd [$vhdvolume\unattend.xml]" 
  97.  
  98. #    Dismount VHDX  
  99. Write-Verbose "Dismounting disk image: [$Path]" 
  100. Dismount-DiskImage -ImagePath $path 
  101.  
  102. #    Update additional VM settings 
  103. Write-Verbose 'Setting additional VM settings' 
  104. Set-VM -Name $name -DynamicMemory 
  105. Set-VM -Name $name -MemoryMinimumBytes $VMMemory 
  106. Set-VM -Name $name -AutomaticStartAction Nothing 
  107. Set-Vm -Name $name -AutomaticStopAction ShutDown 
  108.  
  109. #    Show what has been created! 
  110. "VM Created:" 
  111. Get-VM -Name $name | fl * 
  112.  
  113. #    Start VM 
  114. Write-verbose "VM [$Name] being started" 
  115. Start-VM -Name $name 
  116.  
  117. #    Now work out and write how long it took to create the VM 
  118. $Finishtime = Get-Date 
  119. Write-Verbose ("Creating VB ($name) took {0} seconds" -f ($FinishTime - $Starttime).totalseconds) 
  120. }  # End of Create-VM function 
  121.  
  122.  
  123. ####################################################################################################### 
  124. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  125. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  126. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  127.  
  128. # Location of Server 2012 DVD Iso Image 
  129. $iso   = 'c:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso' 
  130. # Where we put the reference VHDX 
  131. $ref   = 'c:\v3\Ref2012.vhdx' 
  132. # Path were VMs, VHDXs and unattend.txt files live 
  133. $path  = 'c:\V3' 
  134. # Location of Unattend.xml - first for workstation systems, second for domain joined systems  
  135. $una   = 'c:\V3\UnAttend.xml' 
  136. $unadj = 'c:\V3\UnAttend.dj.xml' 
  137.  
  138. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  139. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  140. #       CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS     # 
  141. ####################################################################################################### 
  142.  
  143. #   Now run the script to create the VMs as appropriate. 
  144. $Start = Get-Date 
  145. "Create-VM --- Started at: $Start" 
  146.  
  147. #################################################################### 
  148. # Comment out VMs you do NOT want to create then run the entire script 
  149. # To comment out a VM creation, just add a '#" at the start of the line.  
  150. #        Removing the comment line means you want to create that VM.  
  151. #        BE creful!  If you make a mistake, stop the script. Kill any VMs created, then remove the 
  152. #        storage for the VMs.  
  153. ####################################################################################################### 
  154.  
  155. #    Create the DC - NON-domained joined 
  156. # Create-VM -name "DC1"  -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $una -Verbose -IPAddr '10.0.0.10/24' -DNSSvr 10.0.0.10  -VMMemory 1gb 
  157.  
  158. #    Remaining VMs use the domain-join version of unattend.xml 
  159. # Create-VM -name "Srv1"  -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.30/24' -DNSSvr 10.0.0.10  -VMMemory 512mb 
  160. # Create-VM -name "Srv2"  -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.31/24' -DNSSvr 10.0.0.10  -VMMemory 512mb 
  161. # Create-VM -name "Sql1"  -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.20/24' -DNSSvr 10.0.0.10  -VMMemory 768mb 
  162. # Create-VM -name "Exch1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.21/24' -DNSSvr 10.0.0.10  -VMMemory 768mb 
  163.  
  164. #    DHCP 1,2 for advanced networking class 
  165. # Create-VM -name "DHCP1" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.51/24' -DNSSvr 10.0.0.10 -VMMemory 512mb 
  166. # Create-VM -name "DHCP2" -VmPath $path -ReferenceVHD $ref -Network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.52/24' -DNSSvr 10.0.0.10 -VMMemory 512mb 
  167.  
  168. #    Create a second DC for reskit.org for advanced class 
  169. # Create-VM -name "DC2"  -vmPath $path -ReferenceVHD $ref -network "Internal" -UnattendXML $unadj -Verbose -IPAddr '10.0.0.11/24' -DNSSvr 10.0.0.10  -VMMemory 512mb 
  170.  
  171.  
  172. #  script is all done - just say nice things and quit. 
  173. $Finish = Get-Date 
  174. "Create-VM --- Finished at: $Finish" 
  175. "Elapsed Time :  $(($Finish-$Start).totalseconds) seconds" 
  176.   
Technorati Tags: ,,,

1 comment:

James Stephan said...

Hey I am a fan of your posts and I just want to point you to my blog and a couple of powershell scripts

I have created them from a hodge podge of sites ( yours for one) and just trial and error, not a programer just old school administrator
I have created them from a hodge podge of sites ( yours for one) and just trial and error, not a programer just old school administrator

http://stephanco.blogspot.com/2013/05/detailed-report-of-your-hyper-v-server.html

http://stephanco.blogspot.com/2013/05/new-vm-powershell-commandlet-to-easily.html

I was hoping you could improve them or post them for others to use, my blog is not all that popular but I want to share.