Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, discusses using Windows PowerShell 3.0 to configure a virtual machine on his laptop.
Microsoft Scripting Guy, Ed Wilson, is here. One of the things I really appreciate is weekends. Most of the time, I do not even open my email. Freedom from email is something I dream about. Recently, my cell phone went on a walkabout, so my dreams are coming more and more true.
Anyway, I am sitting on the lanai sipping a cup of white tea with a few organic blueberries in it. It is a nice refreshing cup of tea and a great way to start the day. I have my laptop, freshly built with Windows 8, and I want to spend a bit of time configuring the virtual machine that I created yesterday.
Note Yesterday in my blog, Use PowerShell to Create a New Virtual Machine, I used Windows PowerShell 3.0 to create a new virtual machine on my laptop, which is running Windows 8. Today I will configure the virtual machine.
When I create a virtual machine, there are always a few things I do prior to the first use of the machine. These include installing the integration services and configuring dynamic memory.
Note Admin rights are required to use the Hyper-V module.
One of the things I always do is ensure that the virtual machine additions (integration services) install properly. In the past, checking this seemed to involve attempting a new installation, and then watching for messages. In the Hyper-V module, I decide to look for cmdlets that are related to the additions. Following is the command I use. (gcm is an alias for the Get-Command cmdlet. I use –mod instead of typing the longer Module parameter. I also use a wildcard (*integ*) to help me look for cmdlets that are related to the virtual machine integration services.)
PS C:\> gcm -mod hyp* -Noun *integ*
CommandType Name ModuleName
----------- ---- ----------
Cmdlet Disable-VMIntegrationService Hyper-V
Cmdlet Enable-VMIntegrationService Hyper-V
Cmdlet Get-VMIntegrationService Hyper-V
This command shows me that I could enable or disable the integration services by using a cmdlet. I can also check to ensure that they are enabled.
I decide that I will use the Get-VMIntegrationService cmdlet to ensure the presence of the integration services. I know that a well-planned cmdlet accepts pipelined information. So I use the Get-VM cmdlet and pipe the results to the Get-VMIntegrationService cmdlet. I give it a whirl to see what happens. The command is shown here.
get-vm | Get-VMIntegrationService
The command and associated output are shown in the image that follows.
I assume that the Primary and Secondary status descriptions have something to do with whether a virtual machine runs, because only DC1 is up right now. The output, although impressive, is a bit much to read, so I group the information to make things easier to understand. The revised command is shown here.
get-vm | Get-VMIntegrationService | group name –NoElement
The following output tells me that all four virtual machines contain each of the integration services.
PS C:\> get-vm | Get-VMIntegrationService | group name -NoElement
4 Time Synchronization
4 Key-Value Pair Exchange
I conjecture that I will need a cmdlet that uses the verb of Set to permit configuring dynamic memory. In addition, I surmise that the noun will have something to do with memory. I use the following command to find the cmdlet I need to use (you can see that my guess was pretty close).
PS C:\> gcm -Verb set -Noun *mem* -Mod *hyper*
Cmdlet Set-VMMemory Hyper-V
Now I use the Get-Command cmdlet to look at the syntax of the Set-VMMemory cmdlet. This command is shown here.
gcm -Syntax Set-VMMemory
The command and its associated output are shown in the image that follows.
The syntax of the Set-VMMemory cmdlet tells me that there are three ways to use the cmdlet. The first is to supply one or more virtual machine names as strings. The third way is to supply one or more virtual machines (I assume I can get these via the Get-VM cmdlet). I am not sure what the second syntax does, but it does not matter, because I need to supply a virtual machine name.
Note The fact that I can supply arrays for input is awesome, and it can lead to extremely powerful Hyper-V management scenarios.
I decide to configure my virtual machine with dynamic memory, a minimum value of 512 MB of RAM, and a maximum of 2048 MB of RAM. I also set a 20% buffer. The following command accomplishes these tasks.
Set-VMMemory -VMName c7 -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 2048MB -Buffer 20
I can use the Get-VMMemory cmdlet to ensure my changes took place. This command appears here.
PS C:\> Get-VMMemory -VMName c7
VMName DynamicMemoryEnabled Minimum(M) Startup(M) Maximum(M)
------ -------------------- ---------- ---------- ----------
c7 True 512 512 2048
Remember, this is still Windows PowerShell I am using. So I can look for additional information by piping the cmdlet to the Format-List cmdlet and using a * to look for all properties. Here is the command.
Get-VMMemory -VMName c7 | fl *
In addition, I can use pipeline cmdlets into the Get-VMMemory cmdlet. I use the following command to verify that I have dynamic memory enabled on all the virtual machines on my laptop.
PS C:\> get-vm | Get-VMMemory
c1 True 512 512 1024
C2 True 512 512 1024
c7 True 512 512 2048
DC1 True 512 512 1024
I see that I have allocated too much memory for my c7 virtual machine. I use the Set-Virtual MachineMemory cmdlet to make the change. I only need to specify the revised value. The command is shown here.
Set-VMMemory -VMName c7 -MaximumBytes 1024MB
I use the Get-VM | Get-VMMemory command to ensure that the change worked. It does, as shown here.
c7 True 512 512 1024
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
Ed, you have probably done "replace all" to change "VM" to "virtual machine", so almost all cmdlets have wrong names now - Enable-virtual machineIntegrationService, Get-virtual machineMemory, Set-virtual machineMemory...
@Aleksandar Nikolic - looks like you are right and that the editor got froggy with the replace all. I will contact her, and have her fix the code.
@Aleksandar Nikolic it is fixed now, and therefore should be good to go. Again thank you for catching this. Hope all is well with you.
@Ed You're welcome.