Add disks to a VM

Why

Adding a disk to a VM is not a big issue. I can use Hyper-V console or Windows Admin Center or PowerShell. But because I need to do it on a frequent basis and it’s boring I wanted to find a better way.

Just a reminder – how to add VHD to a VM:

  • In Hyper-V we use Settings menu for given VM:

    Select New, and then name it, select path and proper options.

  • In Windows Admin Center we need to get to VMs menu, Inventory, select given VM and go to disks:

Then again, fill in proper path and options.

In PowerShell – it’s sligthly better:

There are two problems with this. First – I need to know WHERE to put the files for given VM. In general I have all disks on the same CSV – along with VM configuration. Second – it’s boring 🙂

The Idea

So, I wanted to speed this up a bit. What I know?

  • In general I name disks according to this template: <VMName>_disk<number>.vhdx.
  • Disks are in the same location as other VM files (like configuration).
  • When disk is attached in correct order – it usually gets the same disk number within the Windows OS. This simplifies a bit later on.
  • I use Hyper-V hosts from 2012R2 version to 2019.
  • Some Hyper-V hosts are in different domains – meaning I need to use Credential parameter.
  • Sometimes VHD is in a different location.
  • Sometimes I want to manually name the VHD.
  • I use dynamic disks now, but I want to support other types later on as well.

Knowing this I ended up with a function that will:

  1. Use Invoke-Command
    1. I will be able to connect to different versions of Hyper-V
    2. I will be able to use optional Credential parameter to connect to Hyper-V hosts in different domains. Or run it from unprivilged powershell session.
    3. This will also work from PowerShell Core
  2. Gather some basic info about disks of given VM.
    1. This will give me current VM path location
    2. And also the number of current VHDs. (remember the <VMName>_disk<number>.vhdx part?
  3. Create VHD and then attach it to a VM.

Here’s the code:

What’s next? Well a VHD needs to be formatted right? Booring:) Glad I have this Format Drive. Remotely.

Final approach

Well if I combine both of these functions I can do it in one swing. For multiple disks at once!

Here’s the output:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s