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.
Checking integration services
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-virtual machineIntegrationService Hyper-V
Cmdlet Enable-virtual machineIntegrationService Hyper-V
Cmdlet Get-virtual machineIntegrationService 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-Virtual MachineIntegrationService 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-Virtual MachineIntegrationService cmdlet. I give it a whirl to see what happens. The command is shown here.
get-vm | Get-virtual machineIntegrationService
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-virtual machineIntegrationService | group name –NoElement
The following output tells me that all four virtual machines contain each of the integration services.
PS C:\> get-vm | Get-virtual machineIntegrationService | group name -NoElement
Count Name
----- ----
4 Time Synchronization
4 Heartbeat
4 Key-Value Pair Exchange
4 Shutdown
4 VSS
Configure dynamic memory
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*
CommandType Name ModuleName
----------- ---- ----------
Cmdlet Set-virtual machineMemory Hyper-V
Now I use the Get-Command cmdlet to look at the syntax of the Set-virtual machineMemory cmdlet. This command, appears here.
gcm -Syntax Set-virtual machineMemory
The command and its associated output are shown in the image that follows.
The syntax of the Set-Virtual MachineMemory 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-Virtual Machine 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-virtual machineMemory -virtual machineName c7 -DynamicMemoryEnabled $true -MinimumBytes 512MB -MaximumBytes 2048MB -Buffer 20
I can use the Get-virtual machineMemory cmdlet to ensure my changes took place. This command appears here.
PS C:\> Get-virtual machineMemory -virtual machineName c7
virtual machineName 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-virtual machineMemory -virtual machineName c7 | fl *
In addition, I can use pipeline cmdlets into the Get-Virtual MachineMemory 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-virtual machineMemory
virtual machineName DynamicMemoryEnabled Minimum(M) Startup(M) Maximum(M)
------ -------------------- ---------- ---------- ----------
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-virtual machineMemory -virtual machineName c7 -MaximumBytes 1024MB
I use the Get-VM | Get-Virtual MachineMemory command to ensure that the change worked. It does, as shown here.
PS C:\> get-vm | Get-virtual machineMemory
virtual machineName DynamicMemoryEnabled Minimum(M) Startup(M) Maximum(M)
------ -------------------- ---------- ---------- ----------
c1 True 512 512 1024
C2 True 512 512 1024
c7 True 512 512 1024
DC1 True 512 512 1024
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