3. Configuring virtual machines
Once you have created a virtual machine, you might need to
further configure its virtual hardware and management settings. You
can do this either by opening the virtual machine’s settings in
Hyper-V Manager or by using Windows PowerShell. For example, to view
or modify the settings for a virtual machine using Hyper-V Manager,
right-click on the virtual machine and select Settings. Doing this
displays a Settings dialog such as the one shown in Figure 2.
To view the settings of the same virtual machine using Windows
PowerShell, you can use the Get-VM cmdlet as shown here:
PS C:\> Get-VM -Name SRV-A | Format-List *
VMName : SRV-A
VMId : cabb9f25-1d4a-4ce0-884a-a04520ed0880
Id : cabb9f25-1d4a-4ce0-884a-a04520ed0880
Name : SRV-A
State : Off
OperationalStatus : {Ok}
PrimaryOperationalStatus : Ok
SecondaryOperationalStatus :
StatusDescriptions : {Operating normally}
PrimaryStatusDescription : Operating normally
SecondaryStatusDescription :
Status : Operating normally
Heartbeat :
ReplicationState : Disabled
ReplicationHealth : NotApplicable
ReplicationMode : None
CPUUsage : 0
MemoryAssigned : 0
MemoryDemand : 0
MemoryStatus :
SmartPagingFileInUse : False
Uptime : 00:00:00
IntegrationServicesVersion :
ResourceMeteringEnabled : False
ConfigurationLocation : C:\ProgramData\Microsoft\Windows\Hyper-V
SnapshotFileLocation : C:\ProgramData\Microsoft\Windows\Hyper-V
AutomaticStartAction : StartIfRunning
AutomaticStopAction : Save
AutomaticStartDelay : 0
SmartPagingFilePath : C:\ProgramData\Microsoft\Windows\Hyper-V
NumaAligned :
NumaNodesCount : 1
NumaSocketCount : 1
IsDeleted : False
ComputerName : HOST4
Notes :
Path : C:\ProgramData\Microsoft\Windows\Hyper-V
CreationTime : 8/13/2012 8:16:47 AM
IsClustered : False
SizeOfSystemFiles : 28464
ParentSnapshotId :
ParentSnapshotName :
MemoryStartup : 1073741824
DynamicMemoryEnabled : False
MemoryMinimum : 536870912
MemoryMaximum : 1099511627776
ProcessorCount : 1
RemoteFxAdapter :
NetworkAdapters : {Network Adapter}
FibreChannelHostBusAdapters : {}
ComPort1 : Microsoft.HyperV.PowerShell.VMComPort
ComPort2 : Microsoft.HyperV.PowerShell.VMComPort
FloppyDrive : Microsoft.HyperV.PowerShell.VMFloppyDiskDrive
DVDDrives : {DVD Drive on IDE controller number 1 at location 0}
HardDrives : {Hard Drive on IDE controller number 0 at location 0}
VMIntegrationService : {Time Synchronization, Heartbeat, Key-Value Pair Exchange, Shutdown...}
To modify these virtual machine settings, you can use the Set-VM
cmdlet and other Hyper-V cmdlets. In the sections that follow, we will
examine only a few of these different virtual machine settings and how
to configure them. For additional information on configuring virtual
machine settings, search the TechNet Library for the appropriate
topic.
You can use the IDE Controller 0, IDE Controller 1, and SCSI
Controller pages of the virtual machine settings dialog box in
Hyper-V Manager to add new virtual disks to a virtual machine. You
can also use subpages of these pages to inspect, browse, or edit an
existing virtual hard disk; remove a virtual disk that is attached
to the virtual machine; or attach a physical disk on the host’s
storage system to the virtual machine.
When you add a virtual disk using the New Virtual Hard Disk
Wizard, you have a choice of using either the VHDX or VHD format for
the new disk. (The default is VHDX.) The new Virtual Hard Disk
Wizard also lets you configure a name for the new disk, the disk
type (with the default being dynamically expanding), the maximum
size for the disk (with the default being 127 GBs), and whether the
new disk should be blank or contain the data on a physical disk on
the host or on an existing virtual disk, as shown in Figure 3.
You can also use the Add Hardware page of the virtual machine
settings dialog box to add additional SCSI controllers to your
virtual machine so that you can connect more virtual disks to the
virtual machine.
You can also use Windows PowerShell to create new virtual
disks and add them to your virtual machines. For example, let’s say
you want to create and attach a 500-GB dynamically expanding data
disk to virtual machine SRV-A on HOST4. You can begin by using the
Get-VHD command to display a list of disks attached to SRV-A as
follows:
PS C:\> Get-VM -VMName SRV-A | Select-Object VMId | Get-VHD | `
Format-List Path,VhdFormat,VhdType,Size
Path : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A.vhdx
VhdFormat : VHDX
VhdType : Dynamic
Size : 536870912000
The preceding command takes advantage of the pipeline
capabilities of Windows PowerShell and works like this:
-
The command Get-VM –VMName SRV-A returns an object
representing virtual machine SRV-A.
-
The output of the preceding command is then piped into the
command Select-Object VMId, which returns an object representing
the GUID for SRV-A.
-
The GUID for SRV-A is then piped into the Get-VHD command
to indicate which virtual machine is to be queried for its
virtual disks.
-
The output of the Get-VHD command is then formatted as a
list to display only those properties of interest—namely, the
path and file name of the virtual disk file, the format it uses,
the type of the disk, and the disk’s size.
Next you can use the New-VHD cmdlet to create the new data
disk as follows:
PS C:\> New-VHD -SizeBytes 500GB `
-Path "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A-data.vhdx"
ComputerName : HOST4
Path : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A-data.vhdx
VhdFormat : VHDX
VhdType : Dynamic
FileSize : 4194304
Size : 536870912000
...
Next you can use the Add-VMHardDiskDrive cmdlet to attach the
new data disk to location 1 on IDE controller 0 as follows:
PS C:\> Add-VMHardDiskDrive -VMName SRV-A `
-Path "C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A-data.vhdx" `
-ControllerType IDE -ControllerNumber 0 -ControllerLocation 1
You can then use the Get-VHD cmdlet as before to verify the
result:
PS C:\> Get-VM -VMName SRV-A | Select-Object VMId | Get-VHD | `
Format-List Path,VhdFormat,VhdType,Size
Path : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A.vhdx
VhdFormat : VHDX
VhdType : Dynamic
Size : 536870912000
Path : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A-data.vhdx
VhdFormat : VHDX
VhdType : Dynamic
Size : 536870912000
Alternatively, you can use the Get-VMHardDiskDrive cmdlet to
display all disks connected to the IDE controllers on the virtual
machine:
PS C:\> Get-VMHardDiskDrive -VMName SRV-A | `
Format-List ControllerNumber,ControllerLocation,Path
ControllerNumber : 0
ControllerLocation : 0
Path : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A.vhdx
ControllerNumber : 0
ControllerLocation : 1
Path : C:\Users\Public\Documents\Hyper-V\Virtual Hard Disks\SRV-A-
data.vhdx
Viewing the IDE 0 Controller page and its subpages in the
Settings dialog box for the virtual machine also confirms that the
procedure was successful.