As you saw in the previous section, you may need to modify a virtual machine after creating it through PowerShell. Likewise, you may occasionally need to modify an existing virtual machine’s configuration. For instance, you might need to add more memory or additional CPU cores to a virtual machine.
This section discusses some techniques for modifying virtual machine hardware allocation.
Memory
The resource that you will likely have to adjust more often than any other is memory. You can adjust memory by using the Set-VMMemory cmdlet.
Start the process by entering the Get-VMMemory cmdlet, followed by
the name of the virtual machine (Figure 5.32). This causes PowerShell to display the virtual machine’s current memory configuration.
Figure 5.32
You can use the Get-VMMemory cmdlet to see a virtual machine’s memory assignment.
The easiest way to reallocate a virtual machine’s memory is to assign a static amount of memory to the virtual machine by entering the following command:
Figure 5.33
You can use the Set-VMMemory cmdlet to allocate memory to a virtual machine.
This command sets the startup memory to 2 GB. You can verify the operation’s success by using the Get-VMMemory cmdlet.
If you want to configure a virtual machine to use dynamic memory, things become a bit more complicated. You still use the Set-VMMemory cmdlet, but you have to include a specification to enable dynamic memory. You will also have to provide values for the minimum, startup and maximum memory.
You can optionally set a priority and a buffer value for the virtual machine as well by using the following command:
Set-VMMemory <virtual machine name> -DynamicMemoryEnabled $True –MinimumBytes <minimum memory> -StartupBytes <startup memory>
-MaximumBytes <maximum memory> -Priority <priority> -Buffer
<buffer value>
For example, suppose you want to configure the virtual machine PowerShellVM to use a 1 GB of startup memory and you want to set the minimum memory to 512 MB and the maximum memory to 2 GB. Let’s also assume that you want to set the priority to 80 and the buffer to 25 (Figure 5.34). You can do this with the following command:
Set-VMMemory PowerShellVM –DynamicMemoryEnabled $True –
MinimumBytes 512MB -StartupBytes 1GB –MaximumBytes 2GB –Priority 80 –Buffer 25
Figure 5.34
You can also use the Set-VMMemory for configuring dynamic memory.
Now that you know how to allocate memory to a virtual machine, here are a couple of shortcuts that you can use. Keep in mind that you can use these shortcuts with any type of hardware allocation. The following example uses only memory allocation.
Allocating memory to multiple virtual machines
The first shortcut allows you to allocate memory to multiple virtual machines simultaneously. The easiest way to accomplish this is to specify multiple virtual machine names within the Set-VMMemory cmdlet. For example, to allocate 2 GB of memory to virtual machines named NewVM1, NewVM2 and NewVM3 on a server named Lab1 (Figure 5.35), you can use the following command:
Set-VMMemory NewVM1,NewVM2,NewVM3 –Startup 2.0GB Figure 5.35
You can assign memory to multiple virtual machines.
When the operation has completed, you can verify its success by specifying all three virtual machine names within the Get-VMMemory cmdlet. For example, in this situation you would enter:
Get-VMMemory NewVM1,NewVM2,NewVM3
Keep in mind that this is not the only way to allocate memory to multiple virtual machines. You can also specify the virtual machines to which you want to allocate memory using filtering. For example, you can create a filter based on the virtual machine’s name or on the amount of memory that is currently allocated to the virtual machine.
Pipelining hardware allocations
The other shortcut that is worth knowing is that you can actually allocate memory to a virtual machine while the virtual machine is being created. This is done by pipelining the New-VM and the Set-VMMemory cmdlets together.
Suppose, for example, that you want to create a virtual machine named NewVM4 and allocate 4 GB of RAM to it (Figure 5.36). You can accomplish this by using the following command:
New-VM –Name “NewVM4” –Path “C:\VMs” –ComputerName Lab1 | Set-VMMemory –Startup 4GB
Figure 5.36
Virtual network adapters
Just as you may need to modify a virtual machine’s memory allocation, you may also need to provision a virtual machine with a virtual NIC. It’s best to start by viewing a virtual machine’s current virtual network adapter usage.
To do this, enter the Get-VMNetworkAdapter cmdlet, followed by the virtual machine name (Figure 5.37).
Figure 5.37
You can use the Get-VMNetworkAdapter cmdlet to access virtual network adapter information for a virtual machine.
In many cases, you may find that although a network adapter has been assigned to a virtual machine, you must connect that network adapter
to a virtual switch. Fortunately, this is relatively easy to do. While it is possible to use a single command to attach a virtual network adapter to a virtual switch, it’s better to use variables because the virtual network adapter name and the virtual switch name may be long. Using variables reduces the chances of making a mistake by mistyping one of the names.
Therefore, the first step in the process is to retrieve the name of the virtual machine’s virtual network adapter and assign this name to a variable named
$VMNic using the following command:
$VMNic = Get-VMNetworkAdapter –VMName <virtual machine name>
The next step is to retrieve the name of the virtual switch by using the following command:
Get-VMSwitch | Select-Object Name
If you have multiple virtual switches, you can narrow down the results by specifying the virtual switch’s connectivity. For example, if you only want to use an external virtual switch, you can use the following cmdlet:
Get-VMSwitch –SwitchType External | Select-Object Name
Now you just need to connect the virtual network adapter to the virtual switch.
You should be able to do this by using the Connect-VMNetworkAdapter cmdlet; however, PowerShell apparently doesn’t allow you to add the virtual switch name to a variable and use the variable to connect the virtual switch to a virtual network adapter. Instead, you have to enter the virtual switch name in long form. The command looks something like this:
Connect-VMNetworkAdapter –VMNetworkAdapter $VMNic –SwitchName
“<virtual switch name>”
As a more concrete example, for the virtual switch on a test server named Intel(R) Gigabit CT Desktop Adapter #2 – Virtual Switch (Figure 5.38), you would use the following command:
Connect-VMNetworkAdapter –VMNetworkAdapter $VMNic –SwitchName
“Intel(R) Gigabit CT Desktop Adapter #2 – Virtual Switch”
Figure 5.38
You must connect a virtual network adapter to a virtual switch.
Once you have connected the virtual network adapter to the virtual switch, it is a good idea to verify the connection (Figure 5.39). You can do by entering the following command:
Get-VMNetworkAdapter –VMName <your virtual machine name> | Select-Object VMName, Name, SwitchName
Figure 5.39
You can use the Get-VMNetworkAdapter cmdlet to verify the connection.
CPU cores
When you create a new virtual machine through PowerShell, Windows
automatically assigns a single virtual processor to the virtual machine. In some cases, however, a single virtual processor might not be sufficient. Fortunately, PowerShell makes it possible to add virtual processors to a virtual machine.
Before you attempt to modify a virtual machine’s virtual processor allocation, it is a good idea to verify the number of virtual processors currently
assigned to the virtual machine (Figure 5.40). You can do this by using the following command:
Get-VM <virtual machine name>| Select-Object VMName, ProcessorCount Figure 5.40
It’s a good idea to verify a virtual machine’s current virtual CPU count.
For example, to assign two virtual processors to a virtual machine named NewVM1 (Figure 5.41), you can use this command:
Set-VMProcessor NewVM1 –Count 2 Figure 5.41
You can use the Set-VMProcessor cmdlet to configure a virtual machine’s virtual CPU usage.
Of course there are also some other switches that you can use with the Set-VMProcessor cmdlet. For instance, if you want to set a reserve, maximum and relative weight value, you can use a command like this:
Set-VMProcessor <virtual machine name> -Count 2 –Reserve 10 – Maximum 75 – RelativeWeight 200
In case you are not familiar with these particular values, here are their meanings:
• Limit –The maximum amount of time that a virtual machine is allowed to use a physical CPU. The default limit is 100% usage.
• Reservation – A percentage of CPU time solely for a specific virtual machine. By default the reservation is set at 0%.
• Weight – A relative weight that affects how much CPU time a virtual machine will receive. The default weight is 100.
You can also use the Set-VMProcessor cmdlet to enable compatibility for older operating systems by including the –Compatibility For Older Operating Systems Enabled switch in the command, as in the following example:
Set-VMProcessor <virtual machine name>
-CompatibilityForOlderOperatingSystemsEnabled $true