Tuesday 15 January 2013

Create-ReferenceVHDX.ps1

  1. # Create-ReferenceVHDX.ps1
  2. # Script that will create a reference VHDX for later VM Creation
  3. # Version 1.0.0 - 14 Jan 2013
  4. # Define a function to create a reference VHDX.
  5. Function Create-ReferenceVHDX {
  6. [Cmdletbinding()]
  7. Param (
  8. # ISO of OS
  9. [string] $Iso = 'C:\downloads\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso',
  10. # Path to reference VHD
  11. [string] $RefVHDXPath = "C:\vhd\Ref2012.vhdx"
  12. )
  13. # Get start time
  14. $StartTime = Get-Date
  15. Write-Verbose "Beginning at $StartTime"
  16. #--------------------------------------------------+
  17. # Mount an ISO and check out available OS versions!
  18. #--------------------------------------------------+
  19. # Import the DISM module
  20. Write-Verbose 'Loading DISM module' -Verbose:$false
  21. Import-Module -Name DISM -Verbose:$False
  22. # Mount the OS ISO image onto the local machine
  23. Write-Verbose "Mounting ISO image [$iso]"
  24. Mount-DiskImage -ImagePath $iso
  25. # Get the Volume the Image is mounted to
  26. Write-Verbose 'Getting disk image of the ISO'
  27. $ISOImage = Get-DiskImage -ImagePath $ISO | Get-Volume
  28. Write-Verbose "Got disk image [$($ISOImage.DriveLetter)]"
  29. # And get the drive Letter of the dirve where the image is mounted
  30. # add the drive letter separator (:)
  31. $ISODrive = [string]$ISOImage.DriveLetter+":"
  32. Write-Verbose "OS ISO mounted on drive letter [$ISODrive]"
  33. # Next we will get the installation versions from the install.wim.
  34. # $Indexlist is the index of WIMs on the DVD - display the versions
  35. # available in the DVD and let user select the one to serve as the base
  36. # image - probably DataCentre Full Install
  37. $IndexList = Get-WindowsImage -ImagePath $ISODrive\sources\install.wim
  38. Write-Verbose 'Got Index list - displaying it now!'
  39. # Display the list and return the index
  40. $item = $IndexList | Out-GridView -OutputMode Single
  41. $index = $item.ImageIndex
  42. Write-Verbose "Selected image index [$index]"
  43. #---------------------------------+
  44. # Create a Reference Image !
  45. #---------------------------------+
  46. # Create the VHDX for the reference image
  47. $VMDisk01 = New-VHD –Path $RefVHDXPath -SizeBytes 15GB
  48. Write-Verbose "Created VHDX File [$($vmdisk01.path)]"
  49. # Get the disk number
  50. Mount-DiskImage -ImagePath $RefVHDXPath
  51. $VHDDisk = Get-DiskImage -ImagePath $RefVHDXPath | Get-Disk
  52. $VHDDiskNumber = [string]$VHDDisk.Number
  53. Write-Verbose "IReference image is on disk number [$VhddiskNumber]"
  54. # Create a New Partition
  55. Initialize-Disk -Number $VHDDiskNumber -PartitionStyle MBR
  56. $VHDDrive = New-Partition -DiskNumber $VHDDiskNumber -UseMaximumSize -AssignDriveLetter -IsActive | Format-Volume -Confirm:$false
  57. $VHDVolume = [string]$VHDDrive.DriveLetter+":"
  58. Write-Verbose "VHD drive [$vhddrive], Vhd volume [$vhdvolume]"
  59. # Execute DISM to apply image to base disk
  60. Write-Verbose 'Using DISM to apply image to the volume'
  61. Write-Verbose 'This will take some time'
  62. Dism.exe /apply-Image /ImageFile:$ISODrive\Sources\install.wim /index:$Index /ApplyDir:$VHDVolume\
  63. # Execute BCDBoot so volume will boot
  64. Write-Verbose 'Setting BCDBoot'
  65. BCDBoot.exe $VHDVolume\Windows /s $VHDVolume /f BIOS
  66. # Dismount the Images
  67. Write-Verbose "Dismounting ISO and new disk"
  68. Dismount-DiskImage -ImagePath $ISO
  69. Dismount-DiskImage -ImagePath $RefVHDXPath
  70. Write-Verbose "Created Reference Disk [$RefVHDXPath]"
  71. Get-ChildItem $RefVHDXPath
  72. $FinishTime = Get-Date
  73. $tt= $FinishTime - $StartTime
  74. Write-Verbose "Finishing at $FinishTime"
  75. Write-verbose "Creating base image took [$($tt.totalminutes)] minutes"
  76. } # End of Create-ReferenceVHDX
  77. ################################################################################################################
  78. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  79. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  80. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  81. # Path to Server 2012 DVD
  82. $iso = 'C:\Builds\9200.16384.120725-1247_x64frev_Server_Datacenter_VL_HRM_SSS_X64FREV_EN-US_DVD.iso'
  83. # PathTo the reference VDHX is to go
  84. $refvhdxpath = 'C:\V3\ref2012.vhdx'
  85. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  86. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  87. # CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS ===== CHECK THESE PATHS #
  88. #################################################################################################################
  89. Create-ReferenceVHDX -iso $iso -RefVHDXPath $RefVHDXPath -Verbose

No comments: