Weekend Scripter: Configure a New Virtual Machine with PowerShell

Doctor Scripto

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-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.

Image of command output

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

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-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.

Image of command output

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

VMName 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-VMMemory -VMName c7 -MaximumBytes 1024MB

I use the Get-VM | Get-VMMemory command to ensure that the change worked. It does, as shown here.

PS C:\> get-vm | Get-VMMemory

VMName 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 

0 comments

Discussion is closed.

Feedback usabilla icon