Summary: The Scripting Wife learns to use Windows PowerShell to get networking information from all virtual machines running Hyper-V in prep for the 2013 Scripting Games.
Microsoft Scripting Guy, Ed Wilson, is here. It is the last day of the Windows PowerShell summit held in Redmond, Washington. One hundred Windows PowerShell gurus, enthusiasts, and MVPs from several countries have come together on the Microsoft campus to network and learn about the coolest Windows PowerShell ever. For the Scripting Wife and I, this will be a very, very long day. After a full day at the Windows PowerShell summit, I have a meeting with some teammates, and the Scripting Wife and I head to SeaTac for the red-eye flight back to Charlotte. Unfortunately, these days, it is nearly impossible for me to sleep on airplanes. Between the cramped seats, constant jostling, and random flashlight seatbelt checks, I just resign myself to try to get a bit of work done.
I am once again sitting in the lobby, sipping a cup of hot tea and thinking about making the most of our last day here with our friends, when along comes the Scripting Wife. She plops down in the seat beside me, and before I can say anything, she begins…
“I need to be able to find out some information from a Hyper-V server,” she exclaimed.
“My aren’t you becoming a geekette,” I said.
“Can it ,Script Monkey! I think I will need to know something about Hyper-V for the 2013 Scripting Games,” she said.
Use PowerShell to obtain basic Hyper-V information
“The good thing is that my laptop is running Hyper-V with Windows 8, and I installed the management module, so I can help you. What do you want to know?” I asked.
“Well for one thing, I think I want to know the names of the virtual machines, and maybe some networking stuff…I guess,” she said.
“Well that is pretty easy,” I said. “You can use the Get-VM Windows PowerShell cmdlet.”
The Scripting Wife did not hesitate, she immediately typed Get-VM and began to peruse the output. The command is shown here.
Get-Vm
“If you need to work with a single virtual machine, you can use the name to select just that virtual machine. To do that use the Name parameter,” I said. “Go ahead and return only the C1 virtual machine.”
“That makes sense,” she said as she began to type. Here is what she typed:
Get-VM<space>-n<tab>c1<enter>
The command is shown here:
Get-Vm -Name c1
The command and the associated output from the command is shown in the image that follows.
Finding additional virtual machine info
“Suppose I want to find out additional information about the virtual machine?” she asked.
“Well, you can pipe the virtual machine object to the Format-List cmdlet to see the other properties,” I said.
The Scripting Wife used the Up arrow to retrieve her previous command, and she added a pipeline and typed Format-List and a wildcard character. Here is what she typed:
<up arrow><space>|<space>Format-L<tab><space>*<enter>
The command is shown here.
Get-VM -Name c1 | Format-List *
“What is that NetworkAdapters property?” she asked.
“That property contains an additional object. To get inside the object, use the Select-Object cmdlet and the ExpandProperty parameter,” I said.
The Scripting Wife used the Up arrow and retrieved the previous command. She then erased the Format-List * portion of the command. She then added a pipeline character, used Select-Object, and expanded the NetworkAdapters property. The following is what she typed:
<up arrow><space>|<space>Select<space>-exp<tab><space>networkadapters<enter>
The command she created is shown here.
Get-VM -Name c1 | Select -ExpandProperty networkadapters
The command and the output are shown here.
PS C:\> Get-VM -Name c1 | Select -ExpandProperty networkadapters
Name IsManagementOs VMName SwitchName MacAddress Status IPAddresses
---- -------------- ------ ---------- ---------- ------ -----------
Network Adapter False c1 InternalSwitch 00155D003002 {Ok} {192.168...
“Well, that is pretty cool. The problem is that I only need the virtual machine name, the switch, mac address, and the IP address. Do I add the property parameter like I normally do with the Select-Object cmdlet?” she asked.
“Go ahead and try it,” I suggested.
When she typed the command, and pressed ENTER, the screen became covered with errors. The command and the associated errors are shown here.
“Well that didn’t work,” she laughed.
“Yeah, I sort of wish it would, but it don’t. What you need to do is add a second Select-Object command and choose your properties,” I said.
The Scripting Wife thought for a few seconds, and began to type. The following is the command she created.
get-vm -Name c1 | select -ExpandProperty networkadapters | select vmname, macaddress, switchname, ipaddresses
“And I can do the same thing if I want this information from all of the virtual machines?” she asked.
“Absolutely. Why don’t you try it?” I said.
She thought for about a minute, then she pressed the Up arrow and retrieved her previous command. She then removed the –name c1 portion of the command and pressed ENTER. Here is the command she created.
get-vm | select -ExpandProperty networkadapters | select vmname, macaddress, switchname, ipaddresses
The command and the associated output are shown in the image that follows.
“That is about all I need,” she said. “Make sure you get us checked out before you head to the summit,” she instructed.
I started to reply, but she was gone. When Windows PowerShell is involved, she is quick.
Join me tomorrow when the first events in the 2013 Scripting Games appear. WooHoo! The Scripting Games are here, the Scripting Games are here…almost. See you tomorrow.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy