Skip to main content

Microsoft Hyper-V

Defrag CSV

Steps required to defrag a Cluster Shared Volume:

Get-ClusterSharedVolume
Get-ClusterSharedVolume "Vol_Test"
Suspend-ClusterResource "Vol_Test" -RedirectedAccess -VolumeName "C:\ClusterStorage\Volume5"
Repair-ClusterSharedVolume "Vol_Test" -Defrag -Parameters "/A /U /V"
Resume-ClusterResource "Vol_Test" -VolumeName "C:\ClusterStorage\Volume5"

Integration Services Check

Command to check the status of the integration services on a Windows guest VM via Powershell:

Get-VM | ft Name, IntegrationServicesVersion

Metering

Enable metering for all VMs on host node:

Get-VM * | Enable-VMResourceMetering

Enable metering for single VM guest on host:

Get-VM -Name vmname | Enable-VMResourceMetering

Show specific metering resources for all VMs:

Get-VM * | Measure-VM | Select-Object VMname, AvgCPU, AvgRAM, AggregatedAverageNormalizedIOPS

Show metering resources for all VMs:

Get-VM * | Measure-VM | Select-Object *

Reset metering counters for all VMs:

Get-VM * | Reset-VMResourceMetering

Disable metering for all VMs:

Get-VM * | Disable-VMResourceMetering

Nested Virtualization

The command below needs to be executed at host level to enable nested virtualization for a specific VM:

Set-VMProcessor -VMName <VMName> -ExposeVirtualizationExtensions $true
info

Pre-requisites:

  • Must be running Windows 10 Build 10565 or later.
  • The host and nested VM must be running the same build of Win 10.
  • Min 4GB RAM on the host.
  • Dynamic RAM must be disabled on the nested VM.
  • No Checkpoints can be made on the nested VM. (desired to checkpoint hosted VM in the nested VM must be running version 8 of the VM)
  • MAC Address Spoofing has to be enable on the nest VM NIC or a NAT Virtual Switch has to be created.

Create Snapshot

Snapshot VM named test

Checkpoint-VM -Name <VMName> -SnapshotName <SnapName>

To run as scheduled task:

  1. Run program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
  2. Add the script to execute command as argument: C:\Users\user\Desktop\snapshot.ps1

VHD Compact for Linux Guest

Fill in all free space on disk with zeros:

dd if=/dev/zero of=test.file

...wait for the virtual disk to fill, then

rm test.file

Now on Hyper-V Manager, go to Edit Disk and compact.

VHD Create

This will create a 60GB dynamic disk(VHDX) file under C:\VHD\ called Mydrive.vhdx:

New-VHD -Dynamic C:\VHD\MyDrive.vhdx -SizeBytes 60GB

This will create a 5GB fixed size disk(VHDX) file under C:\VHD\ called FixedDisk.vhdx:

New-VHD -Fixed C:\VHD\FixedDisk.vhdx -SizeBytes 5GB

VHD Expand for Linux Guest

http://hubpages.com/technology/Expanding-the-Disk-Volume-on-a-Hyper-V-Linux-Guest

V-Switch Create VLAN

First step is to create a virtual switch on Hyper-V Manager named Hypervsw

Now add the network interfaces as below, you can use any name for it. Remember to tag VLANs accordingly on the switchport connected to the machine:

Add-VMNetworkAdapter -ManagementOS -Name VLAN123_PROD_TAG -SwitchName "Hypervsw"
Add-VMNetworkAdapter -ManagementOS -Name VLAN124_DEV_TAG -SwitchName "Hypervsw"

Associate each network interface created above to their respective VLAN:

Set-VMNetworkAdapterVlan -ManagementOS -Trunk -AllowedVlanIdList "1" -VMNetworkAdapterName "VLAN123_PROD_TAG" -NativeVlanId 123
Set-VMNetworkAdapterVlan -ManagementOS -Trunk -AllowedVlanIdList "1" -VMNetworkAdapterName "VLAN124_DEV_TAG" -NativeVlanId 124

Use the commands below for troubleshooting:

Get-VMNetworkAdapterVlan
Get-VMNetworkAdapter -ManagementOS
Get-VMNetworkAdapter -All

V-Switch NAT

The VM will require a static IP as Hyper-V won't act as a DHCP server:

New-VMSwitch -SwitchName “NATSwitch” -SwitchType Internal
New-NetIPAddress -IPAddress 192.168.0.1 -PrefixLength 24 -InterfaceAlias “NATSwitch”
New-NetNAT -Name “NATNetwork” -InternalIPInterfaceAddressPrefix 192.168.0.0/24
Get-NetNat

V-Switch Rename Virtual NICs

Retrieve list of network adapters for VM test:

Get-VMNetworkAdapter -VMName test

Store each adapter in an array:

$VMNetAdap = Get-VMNetworkAdapter -VMName test

Rename guest network adapters based on array position:

rename-VMNetworkAdapter -VMNetworkAdapter $VMNetAdap[0] -newname P_STAFF
rename-VMNetworkAdapter -VMNetworkAdapter $VMNetAdap[1] -newname P_MISC

Configure trunk details for each guest network adapter:

Set-VMNetworkAdapterVlan -Trunk -AllowedVlanIdList "1" -VMName "test" -VMNetworkAdapterName "P_STAFF" -NativeVlanId 0
SSet-VMNetworkAdapterVlan -Trunk -AllowedVlanIdList "10,11,12,13,14" -VMName "test" -VMNetworkAdapterName "P_MISC" -NativeVlanId 0

V-Switch Create Trunk

Set guest NIC "Network Adapter" to be trunk allowing VLAN tags 1-4094 and set the native untagged VLAN to be 10:

Set-VMNetworkAdapterVlan -Trunk -AllowedVlanIdList "1-4094" -VMName "test" -VMNetworkAdapterName "Network Adapter" -NativeVlanId 10

Verify configuration:

Get-VMNetworkAdapterVlan