Wednesday, August 20, 2014

Part - 7 : PowerShell and Hyper-V : Adding Virtual Disk to the Virtual Machines in Hyper-v

 

In my previous post, we see that how can we create a new virtual hard disk.

If you remember yesterday we have creates a 5Gb of fixed hard disk image with the name of "myHDDFixed.vhd".

Today, we are going to add the same  virtual disk which we have created yesterday to the one of our prebuilt virtual machine.

As you know that we can use the Get-VM cmdlet to get the list of all of our virtual machines.

In the below screenshot, you can see all of my virtual machines. We are going to add the virtual hard disk to the  "Windows 2008 R2" virtual machine.

2

In the below screenshot you can see that there are no secondary hard disk in the "Windows 2008 R2" virtual machine.

1

Remember : We can only add virtual hard disk to the virtual machines, when they are Powered Off, You can't add virtual disk if the virtual machines in Powered ON.

Let's store the information of the "Windows 2008 R2" in to a variable, as we need this information as "Virtual Machine" object not as a plain string.

$vm = Get-VM -Name 'Windows 2008 R2'
3

We use the "Add-VMHardDiskDrive" cmdlet to add the virtual hard disk to the virtual machine.


The basic syntax of Add-VMHardDiskDrive is simple, we need to provide the path of the virtual hard disk file which we need to add to the virtual machine and the  name of the virtual machine as Virtual machine object.


In our case the the path of the virtual hard disk is "'F:\VirtualMachines\Hyper-V\myHDD\myHDDFixed.vhd' and the virtual machine is "Windows 2008 R2" , and we stored the Virtual Machine Object in $vm variable.


Add-VMHardDiskDrive -Path 'F:\VirtualMachines\Hyper-V\myHDD\myHDDFixed.vhd' -VM $vm

in the below screenshot, you can see that our command has run successfully.


4


Let's start the virtual machine and see if the new hard disk is added successfully.


5


If you check the setting of "Windows 2008 R2" virtual machine, you can see that, our "myHDDFixed.vhd" is added successfully.


6


When i opened the "Disk Management" on "Windows 2008 R2", it's showing me that one disk is added and i need to initialize it.


7


After initialize, you can see that a new 5GB of hard disk is added to the server. Smile 


8





Simple.. and easy... isn't..........


 tumblr_mfxux9bqN61qzmvsio8_250


Take care of yourself, and keep smiling, World need You..


Thanks.


Aman Dhally


If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.