Search This Blog

Friday, April 12, 2013

Creating Virtual Machine Using Powershell–Part 1

This is Part 1 of the scripting using Powershell. Total of 7 cmdlet from Windows Server 2012 Hyper-V that we are going to use.

Scenario:-

Recently we involved in nationwide branch deployment and each branches require to setup Windows Server 2012. As usual, MIS people has been assigned to be onsite to do hardware setup and enable remote desktop for us (consultant) so we can remotely configure the cluster. The scope is

a) branch people:-

  • Install operating system
  • Configure LUN
  • Enable RDP

b) HQ people

  • Setup Hyper-V
  • Configure Virtual Machine

Interesting to configure virtual machine in GUI but the process of clicking the GUI is a bit tedious on each configuration and took us few hours to configure few VMs. After a few sites of deployment, finally we’ve decided to simplify some of the work by using Powershell.

Here is an example:-

Step 1:-

Convert sysprep image and create new fixed disk for six virtual machines. For the blog purpose, I’m just going to use Android version as the virtual machine name to represent different name

VM Name Operating System Sysprep folder
Donut Windows Server 2003 with SP2 D:\AndroidISO\VHD\SYSPREP\W2K3ENT_30GB.vhd
Eclair Windows Server 2003 with SP2 D:\AndroidISO\VHD\SYSPREP\Web\WebDisk1.vhd
2nd new fixed disk = 50GB
Froyo Windows Server 2003 with SP2 D:\AndroidISO\VHD\SYSPREP\Web-DB\DBDisk1.vhd
D:\AndroidISO\VHD\SYSPREP\Web-DB\DBDisk2.vhd
Gingerbread Windows Server 2003 with SP2 D:\AndroidISO\VHD\SYSPREP\App\AppDisk1.vhd
2nd new fixed disk = 50GB
HoneyComb Windows Server 2012 D:\AndroidISO\VHD\SYSPREP\App\AppDisk1.vhd
IceCream Windows Server 2008 R2 D:\AndroidISO\VHD\SYSPREP\W2K12STD_50GB.vhdx

Cmdlet:

1. Cmdlet 1:- Convert-VHD – use to convert existing dynamic sysprep image to VHDX and Fixed Disk

2. Cmdlet 2:- New-VHD – create a new fixed virtual disk with the size 50GB

VM 1: Donut

convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\W2K3ENT_30GB.vhd -DestinationPath C:\ClusterStorage\Volume1\Donut\DonutDisk1.vhdx -VHDType Fixed

VM 2: Eclair

convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\Web\WebDisk1.vhd -DestinationPath C:\ClusterStorage\Volume1\Eclair\EclairDisk1.vhdx -VHDType Fixed
new-vhd -Path C:\ClusterStorage\Volume1\Eclair\EclairDisk2.vhdx -size 50GB –Fixed

VM 3:- Froyo


convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\Web-DB\DBDisk1.vhd -DestinationPath C:\ClusterStorage\Volume1\Froyo\FroyoDisk1.vhdx -VHDType Fixed
convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\Web-DB\DBDisk2.vhd -DestinationPath C:\ClusterStorage\Volume1\Froyo\FroyoDisk2.vhdx -VHDType Fixed

VM4: Gingerbread

convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\App\AppDisk1.vhd -DestinationPath C:\ClusterStorage\Volume1\Gingerbread\GingerbreadDisk1.vhdx -VHDType Fixed
new-vhd -Path C:\ClusterStorage\Volume1\Gingerbread\GingerbreadDisk2.vhdx -size 50GB –Fixed

VM 5: HoneyComb


convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\W2K12STD_50GB.vhdx -DestinationPath C:\ClusterStorage\Volume1\Honeycomb\HoneycombDisk1.vhdx -VHDType Fixed

VM 6: IceCream


convert-vhd -Path D:\AndroidISO\VHD\SYSPREP\W2K8STD_50GB.vhdx -DestinationPath C:\ClusterStorage\Volume1\IceCream\IceCreamDisk1.vhdx -VHDType Fixed

In this Part 1, you have learned to convert VHD to VHDX and create new virtual hard disk. Two cmdlet to remember :- Convert-VHD and New-VHD. Stay tuned for Part 2 and we still got few more cmdlets to revealed.

Relevant Post :