Add Disk to 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:
#Create VHD in given path
New-VHD -Path 'Path' -Dynamic -SizeBytes 100GB
#Attach it to a VM
Add-VMHardDiskStrive -VMName 'VM1' -ComputerName 'HyperVHost' -Path 'Path'
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:
- Use Invoke-Command
- I will be able to connect to different versions of Hyper-V
- 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.
- This will also work from PowerShell Core
- Gather some basic info about disks of given VM.
- This will give me current VM path location
- And also the number of current VHDs. (remember the
<VMName>_disk<number>.vhdx
part?
- 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 code
Well if I combine both of these functions I can do it in one swing. For multiple disks at once!
Here’s the output:
Leave a comment