Hyper-v 2016

Create a Virtual Switch with PowerShell

Use Get-NetAdapter to return a list of network adapters connected.
Select the network adapter to use with the Hyper-V switch and place an instance in a variable named $net.

$net = Get-NetAdapter -Name ‘Ethernet’

Execute the following command to create the new Hyper-V virtual switch.
New-VMSwitch -Name “External VM Switch” -AllowManagementOS $True -NetAdapterName $net.Name

VM deployment with Power-Shell

Configuration

# Set VM Name, Switch Name, and Installation Media Path.
$VMName = ‘TESTVM’
$Switch = ‘External VM Switch’
$InstallMedia = ‘C:\Users\Administrator\Desktop\en_windows_10_enterprise_x64_dvd_6851151.iso’

# Create New Virtual Machine
New-VM -Name $VMName -MemoryStartupBytes 2147483648 -Generation 2 -NewVHDPath “D:\Virtual Machines\$VMName\$VMName.vhdx” -NewVHDSizeBytes 53687091200 -Path “D:\Virtual Machines\$VMName” -SwitchName $Switch

# Add DVD Drive to Virtual Machine
Add-VMScsiController -VMName $VMName
Add-VMDvdDrive -VMName $VMName -ControllerNumber 1 -ControllerLocation 0 -Path $InstallMedia

# Mount Installation Media
$DVDDrive = Get-VMDvdDrive -VMName $VMName

# Configure Virtual Machine to Boot from DVD
Set-VMFirmware -VMName $VMName -FirstBootDevice $DVDDrive

Or
$VMName = “VMNAME”

$VM = @{
   Name = $VMName
   MemoryStartupBytes = 2147483648
   Generation = 2
   NewVHDPath = “C:\Virtual Machines\$VMName\$VMName.vhdx”
   NewVHDSizeBytes = 53687091200
   BootDevice = “VHD”
   Path = “C:\Virtual Machines\$VMName”
   SwitchName = (Get-VMSwitch).Name[0]
}
New-VM @VM

Generation 2 Virtual Machine Overview
Secure Boot
New-VM

Role or Feature Installation

Get-WindowsFeature | where {$_.Installed -eq $True}
Get-WindowsFeature | findstr -i Hyper*
[X] Hyper-V                                             Hyper-V                        Installed
        [X] Hyper-V Management Tools                    RSAT-Hyper-V-Tools             Installed
            [ ] Hyper-V GUI Management Tools            Hyper-V-Tools                          Removed
            [X] Hyper-V Module for Windows PowerShell   Hyper-V-PowerShell             Installed

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All
DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V
Install-WindowsFeature -Name Hyper-V
Install-WindowsFeature -Name Hyper-V-PowerShell

Get-WindowsFeature | findstr -i Failover*
[X] Failover Clustering                                 Failover-Clustering            Installed
        [X] Failover Clustering Tools                   RSAT-Clustering                Installed
            [ ] Failover Cluster Management Tools       RSAT-Clustering-Mgmt             Removed
            [X] Failover Cluster Module for Windows … RSAT-Clustering-Powe…        Installed
            [ ] Failover Cluster Automation Server      RSAT-Clustering-Auto…        Available
            [ ] Failover Cluster Command Interface      RSAT-Clustering-CmdI…        Available

Install-WindowsFeature -Name Failover-Clustering
Install-WindowsFeature -Name RSAT-clustering-PowerShell
Install-WindowsFeature -Name RSAT-clustering

PowerShell and Hyper-v

list of Hyper-V commands
Get-Command -Module hyper-v
Get-Help  _______

#Return a list of virtual machines

VM State {running}
Get-VM | where {$_.State -eq ‘Running’}
VM State {Off}
Get-VM | where {$_.State -eq ‘Off’}

#Start and shut down virtual machines
Start-VM -Name

#To start all currently powered off virtual machines
Get-VM | where {$_.State -eq ‘Off’} | Start-VM

#To shut down all running virtual machines, run this:
Get-VM | where {$_.State -eq ‘Running’} | Stop-VM

#VM checkpoint
Get-VM -Name <VM Name> | Checkpoint-VM -SnapshotName <name for snapshot>

Hyper-V Cmdlets in Windows PowerShell

Requirement

Although this document does not provide a complete list of Hyper-V compatible hardware, the following items are necessary:

64-bit Processor with Second Level Address Translation (SLAT).
CPU support for VM Monitor Mode Extension (VT-c on Intel CPU’s).
Minimum of 4 GB memory. As virtual machines share memory with the Hyper-V host, you will need to provide enough memory to handle the expected virtual workload.
The following items will need to be enabled in the system BIOS:

Virtualization Technology – may have a different label depending on motherboard manufacturer.
Hardware Enforced Data Execution Prevention.

systeminfo | findstr -i “Hyper-V Requirements”

Hyper-V Integration Services

Hypervisor Top-Level Functional Specification

Windows Server 2012 R2 (Revision B)
Windows Server 2012 R2
Windows Server 2012
Windows Server 2008 R2

Using checkpoints to revert virtual machines to a previous state

Set-VM -Name <vmname> -CheckpointType Standard
Set-VM -Name <vmname> -CheckpointType Production
Set-VM -Name <vmname> -CheckpointType ProductionOnly

Checkpoint-VM -Name <VMName>
Get-VMCheckpoint -VMName <VMName>
Restore-VMCheckpoint -Name <checkpoint name> -VMName <VMName> -Confirm:$false
Rename-VMCheckpoint -VMName <virtual machine name> -Name <checkpoint name> -NewName <new checkpoint name>
Remove-VMCheckpoint -VMName <virtual machine name> -Name <checkpoint name>
Export-VMCheckpoint -VMName <virtual machine name> -Name <checkpoint name> -Path <path for export>

The default location for storing checkpoint configuration files is: %systemroot%\ProgramData\Microsoft\Windows\Hyper-V\Snapshots.

Virtual Machine automation and management using PowerShell

Run Hyper-V in a Virtual Machine with Nested Virtualization
Set-VMProcessor -VMName <VMName> -ExposeVirtualizationExtensions $true

Advertisements