Modifying the Assigned Memory
The command used to modify the amount of memory that has been allocated to a virtual machine is Set-VMMemory. The command assumes that you are going to specify the name of the virtual machine whose memory allocation you want to modify, but you do have the option of using the pipeline to specify multiple virtual machine names (or even using wildcards.
The basic command usage looks like this:
Set-VMMemory –VM "<virtual machine name"> -Memory <memory amount> -Server <host server name>
To give you a more concrete example of how this command works, let's pretend that I wanted to allocate two gigabytes of data to a virtual machine named Lab-W7 on a host server named Hyper-V. To do so, I would use the following command:
Set-VMMemory –VM "Lab-W7" –Memory 2.0GB –Server Hyper-V
You can see what this command looks like in action in Figure A.
Figure A: It is possible to allocate memory to a virtual machine from the command line.
Obviously it's handy to be able to modify the virtual machine memory allocation from the command line, but you have to admit that this process could become quite tedious if I had to manually allocate memory to each individual virtual machine. Fortunately, it is possible to use wildcard memory allocations.
On my lab server I have a series of virtual machines that are running Windows 7. Each of these virtual machines' names start with Lab-W7. Since all of my virtual Windows 7 machines adhere to a common naming convention, it is possible to allocate memory to all of them at the same time. The first step in doing so is to retrieve a list of the virtual machines whose memory I want to modify. This can be done by using the following command:
Get-VM –Name "Lab-W7%"
This command returns a list of every virtual machine with a name beginning with Lab-W7, as shown in Figure B.
Figure B: You can use the Get-VM command to return a list of every virtual machine that adheres to a naming convention.
Now, we can pipeline the two commands together to allocate memory to each of these virtual machines simultaneously. The command for doing so is:
Get-VM "Lab-W7%" | Set-VMMemory –Memory 2.0GB
You can see what this looks like in Figure C.
Figure C: You can simultaneously allocate memory to multiple virtual machines.
You might have noticed that the memory size is expressed as GB, but you can express the memory as bytes simply by omitting the GB notation.
Creating a New Virtual Machine
Although there are still a lot of topics that I would like to eventually discuss with regard to virtual machine resource allocation, I need to create a new virtual machine for use with another article that I am writing for this site. That being the case, I thought that this might be a good opportunity to demonstrate the New-VM command.
You can use the New-VM command to create a new virtual machine from the command line. Unfortunately, this command does not support a lot of parameters, so you will have to use other commands to create resources for the new virtual machine.
The syntax for the New-VM command is as follows:
New-VM –Name "<virtual machine name>" –Path <the virtual machine path> -Server <your host server name>
Technically the path is not required, but most Hyper-V admins have a specific location where they like to store virtual machine files rather than using Hyper-V's default location.
If you want to see this command in action, check out Figure D. Here I am using the command to create a new virtual machine named Windows Server 8 Beta.
Figure D: The New-VM command is used to create a new virtual machine.
In this particular case I enclosed the path in quotation marks even though the syntax does not specifically require it, because I wanted to use a path that had spaces in the folder name.
So now that a virtual machine has been created, you might be wondering what resources have been allocated to it. If you look at Figure E, you can see that the new virtual machine has been provisioned with 512 MB of memory, no virtual hard disk, and 1 virtual processor. Fortunately, we can easily add resources to the virtual machine directly from the command line.
Figure E: Few resources are allocated to the new virtual machine.
For starters, 512 MB of memory really isn't enough to run Windows Server 8 on, so let's assign a couple of gigabytes of memory to the newly created virtual machine. The command that I would use in this case is:
Set-VMMemory –VM "Windows Server 8 Beta" –Memory 2.0GB –Server Hyper-V
You can see the actual command in Figure F, and if you look at Figure G, you can verify that the memory was immediately assigned to the newly created virtual machine.
Figure F: This command was used to assign memory to the new virtual machine.
Figure F: You can verify through the Hyper-V Manager that the memory was indeed assigned.
Taking a Shortcut
I created the new virtual machine in the way that I did for two reasons. First, I wanted to be able to show you what resources are and are not automatically assigned to a newly created virtual machine. Second, I wanted to be able to spend the next article in the series showing you how to allocate various resources to a virtual machine.
While there is nothing wrong with using these techniques, there is a shortcut that can be used to make the process more efficient. By using pipelining it is possible to create a virtual machine and assign resources to it all at once. If you look at Figure H, you can see that I have created another virtual machine and I combined the New-VM command with the Set-VMMemory command so that I wouldn't have to go back and add memory to the new virtual machine later on. This technique can be used with any of the resource allocation cmdlets that I am going to be showing you later in this series.
Figure H: It is possible to allocate resources to a virtual machine when you create it.
No comments:
Post a Comment