Create a New Virtual Machine with PowerShell: Part 1
Summary: Microsoft Windows PowerShell MVP and honorary Scripting Guy, Sean Kearney, begins a three-part series about creating new virtual machines.
Microsoft Scripting Guy, Ed Wilson, is here. If you are a seasoned Hey, Scripting Guy! Blog reader, you know that the most frequent guest blogger is Sean Kearney. If you are new to the blog, I welcome you, and I encourage you to catch up with Sean’s previous blogs.
Sean is a Windows PowerShell MVP and an Honorary Scripting Guy. Sean’s session at TechEd NA and TechEd Europe this year is Integrating with Microsoft System Center 2012 and Windows PowerShell. In his free time, Sean has written several blog posts about Hyper-V and some other cool stuff, and for the next few weeks, Sean will be the designated guest blogger on Fridays.
Take it away Sean…
I felt a little poke on my shoulder the other day. A friend came and asked me how hard it is to create a virtual machine with Windows Powershell and Hyper-V.
I typed in the following cmdlet and walked off.
He blinked and came back to me. “OK, smart guy. I see. That created a new BLANK virtual machine called “New Virtual Machine” with 512 MB of RAM, no hard drive, and unattached to any LAN. Being a little funny today, aren’t we?”
I was feeling a bit mischievous at the moment. I’m allowed from time-to-time. “Well, I did meet your needs. You said ‘create a virtual machine.’ So how about we define our parameters?”
“OK. I would like to see an example of creating a virtual machine with the following configuration:”
Name of “Brad VM SCSM”
2 gigabytes of RAM
60 gigabyte hard drive
2 CPU cores assigned
Connected to my virtual network called “Hyper-V-Prod1”
I smiled and looked across to my friend. “No problem. So, from what I’ve been showing you about Windows PowerShell, how would you determine the use of this cmdlet?”
He looked over. “First off, I remember that using Get-Help should show me the available parameters for the cmdlet.”
“Then if I need to find examples of the cmdlet in use, I would run the same with the –examples parameter.”
GET-HELP NEW-VM -examples
Looking at the information before us, we see that we can at least create a virtual machine with 2 GB of RAM, based on the first example, by using this command:
NEW-VM –Name “Brad VM SCSM” –MemoryStartupBytes 2048
Brad looked up. “Now that should work, but we still need the virtual hard disk in this configuration. From what I see in the second example, we could specify a virtual hard disk if we knew the physical path to place it. I know my VHDs are sitting on Drive E:\VHD on my Hyper-V host in the lab. So I should just be able to pick a VHD file name that is unique to my virtual machine.”
NEW-VM –Name “Brad VM SCSM” –MemoryStartupBytes 2048 –NewVHDPath E:\VHD\BradVMSCSM-C.VHDX
But when he ran the cmdlet, he got the following prompt:
Scratching his head, he looked over. “What happened?”
I smiled. “It was a poorly written example, apparently. Human error. Let’s grab lunch first, and then we’ll take a look back.”
Thanks, Sean. This is great, and I cannot wait to see the next two posts. I invite everyone to join me tomorrow for more cool Windows PowerShell stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy