Create a New Virtual Machine with PowerShell: Part 2
Summary: Microsoft PowerShell MVP and honorary Scripting Guy, Sean Kearney, continues his series about using Windows PowerShell to create a virtual machine.
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.
To read Part 1 of this 3-part series, please see Create a New Virtual Machine with Windows PowerShell: Part 1.
Brad and I returned from lunch to again look at the message we received after we specified the following cmdlet in Windows Server 2012 with Hyper-V:
NEW-VM –Name “Brad VM SCSM” –MemoryStartupBytes 2048 –NewVHDPath E:\VHD\BradVMSCSM-C.VHDX
We got this prompt:
I looked at the message. “It seems to be asking for how large the VHD file should be. From the example of the memory, I think we can just type it in as 60 GB.”
We typed that and got a different error message:
Cannot recognize “60000MB” as a System.UInt64 due to a format error.
I looked up. “Ah! A .NET error. I guess it wants us to type 60 GB in long form. If we’re going to do that, let’s type a single-line command:
NEW-VM –Name “Brad VM SCSM” –MemoryStartupBytes 2048 –NewVHDPath E:\VHD\BradVMSCSM-C.VHDX –NewVHDSizeBytes 6000000000
A moment later, we checked the Hyper-V console and saw a newly created virtual machine.
“How did you know the parameter name was -NewVHDSizeBytes?” he asked.
I looked over at Brad. “If a parameter is needed and is not specified, Windows PowerShell will show the parameter name and prompt for the needed value. So I knew from experience using Windows PowerShell that if I saw a prompt asking for a value, it would be a parameter for the cmdlet.”
Brad looked at me. “But what about the CPU cores? What about the network settings? I didn’t see anything about those in the cmdlet we entered. How do we deal with that?”
“Let’s see if there are any cmdlets for the virtual machine network adapter,” I suggested.
GET-COMMAND *Network* -module Hyper-V
The results gave us an answer almost immediately. Near the top of the list was the Add-VMNetworkAdapter cmdlet.
Brad quickly typed the cmdlet name to see an example of its use.
GET-HELP ADD-VMNetworkAdapter –examples
He looked up and said, “Is it really that easy? Just add the network adapter to the virtual machine? So if I want to call the network adapter to my virtual switch called ‘Hyper-V-Prod1’ and attach it to the ‘Brad VM SCSM’ virtual machine, I would type this…”
ADD-VMNetworkAdapter –VMName “Brad VM SCSM” –Switchname “Hyper-V-Prod1”
Now to the second task: set the CPU settings. Brad was already at the keyboard figuring it out. “So, let’s see. I can find a cmdlet that might meet my need for the processor. Let’s search.”
GET-COMMAND *Processor* -module Hyper-V
Looking at the available cmdlets for processor settings, it seemed logical to Brad to use SET-VMProcessor if he wants to SET the virtual machine’s settings for the processor. He typed Get-Help to find examples for its use.
GET-HELP SET-VMProcessor -examples
The first example seemed to meet the need. He entered his virtual machine name, “Brad VM SCSM”, and added a count of 2 virtual CPUs.
SET-VMProcessor –VMName “Brad VM SCSM” –Count 2
We pulled up the properties of the virtual machine to check the configuration.
A smile crossed Brad’s face. “How can I make this repeatable for multiple machines?”
Thank Sean, this is really good stuff. Join Sean next Friday for the exciting conclusion of this series, and 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