Reference notes for
Windows Server 2012 Hyper-V PowerShell. The powershell commands are in bold, with user input in red.
Credit: Brien Posey,
Pete Zerger, and Chris Henley – download the “Hands-on Guide: Understanding
Hyper-V in Windows Server 2012” from Veeam.com here!
Test Cluster
Test-Cluster –List
Test-Cluster
–Ignore Storage
Note: Do not run the
storage cluster validation check on a production cluster – this may cause VMs
to crash!
Test-Cluster
–Include {option}
Test-Cluster –Node
HVHOST01, HVHOST02,
HVHOST03
Get-ClusterResource
Test-ClusterResourceFailure
“VM01”
Test-ClusterResourceFailure
–Name “Cluster Disk 1”
Anti-affinity rules
Note: Anti-affinity
rules cannot be done in the GUI.
Get-ClusterGroup {group name} | Select-Object *
Download: PowerShell
Module for Configuring AntiAffinityClassNames in Failover Clustering and extract
the ZIP to C:\Modules.
Set-ExecutionPolicy
Unrestricted
Import-Module
C:\Modules\AntiAffinityClassNames
Get-Command
–Module AntiAffinityClassNames
Set-AntiAffinityClassNames
–Cluster HyperVCluster –ClusterGroup DC01VM
–Value “Is a domain controller”
Get-AntiAffinityClassNames
Syntax simplification
Pre 2012:> Get-Process | ForEach {$_.Name}
In 2012:> Get-Process | ForEach Name
Pre 2012:> Get-Process | where {$_.Handles –GT 500}
In 2012:> Get-Process
| Where Handles –GT 500
Attaching to a Hyper-V server
Enter-PSSession HVHOST02
Get-VM
ExitPSSession
Invoke-command
–ComputerName HVHOST01, HVHOST02,
HVHOST03 {Get-VM}
Querying virtual machines
Get-VM
Get-VM –ComputerName ProdVM01
Get-VM LABVM01
Get-VMHost LABVM1, LABVM2, LABVM3
Filtering the output
-EQ = Equal To
-OR = Or
-AND = And
-Like = Like
-GT = Greater Than
-LT = Less Than
Get-VM | Where
Name –Like ‘VMLAB07*’
Get-VM | Where State –EQ ‘Off’
Get-VM –ComputerName
LABVM1, LABVM2, LABVM3 | Where State –EQ ‘Running’
Get-ClusterGroup |
Where GroupType –EQ ‘VirtualMachine’ | Get-VM
Get-ClusterGroup |
Where GroupType –EQ ‘VirtualMachine’ | Get-VM |
Where State –EQ ‘Off’
Get-ClusterGroup |
Where GroupType –EQ ‘VirtualMachine’ | Get-VM |
Where MemoryAssigned –GT ‘1073741825’
Getting more information
Get-VM Lab-DC | FL
*
Get-VMHost | FL *
Get-VM | FT
VMName, ComputerName, IsClustered
Reporting
Get-VM | FL
Get-VM | FT
Get-VM |
Select-Object VMName, State, ComputerName, IsClustered, UpTime, Status
Get-VM |
Select-Object VMName, State, ComputerName, IsClustered, UpTime, Status |
Export-CSV C:\Data\VMs.csv
Get-VM | FT
VMName, State, ComputerName, IsClustered, UpTime, Status | Out-File C:\Data\VMs.txt
Get-VM |
Select-Object VMName, State, ComputerName, IsClustered, UpTime, Status |
ConvertTo-HTML | Out-File C:\Data\VMs.htm
Get-VM |
Select-Object VMName, State, ComputerName, IsClustered, UpTime, Status |
ConvertTo-HTML | Out-File CL\Data\VMs.htm | Invoke-Expression C:\Data\VMs.htm
Enter the below
lines together to create a styled HTML report:
Changing a virtual machine’s state
Stop-VM LabVM01
Start-VM LabVM01
Creating a virtual machine
With a default
configuration of 512MB, DVD drive, no hard disk, no network, 1 vCPU:
New-VM –Name “NewVM01” –Path “C:\VMs”
–ComputerName NewVM01
Modifying a virtual machine
Get-VMMemory LABVM01
Set-VMMemory LABVM01 –Startup 2.0GB
Set-VMMemory LABVM01 –DynamicMemoryEnabled $True
–MinimumBytes 512MB –StartupBytes 1GB –MaximumBytes 2GB –Priority
80 –Buffer 25
Set-VMMemory NewVM1, NewVM2, NewVM3 –Startup 2.0GB
Creating a virtual machine with a defined memory
allocation
New-VM –Name “NewVM01” –Path “C:\VMs” –ComputerName NewVM01 | Set-VMMemory –Startup 4GB
Virtual network adapters
Get-VMNetworkAdapter
LABVM01
$VMNic =
Get-VMNetworkAdapter –VMName NewVM01
Get-VMSwitch |
Select-Object Name
(Or Get-VMSwitch –SwitchType External |
Select-Object Name if you have multiple virtual switches)
Connect-VMNetworkAdapter
–VMNetworkAdapter $VMNic –SwitchName “Intel(R) Gigabit
CT Desktop Adapter #2 – Virtual Switch”
Get-VMNetworkAdapter
–VMName NewVM01 | Select-Object VMName, Name,
SwitchName
CPU cores
Get-VM NewVM01 | Select-Object VMName, ProcessorCount
Set-VMProcessor NewVM01 –Count 2
Other options:
Set-VMProcessor TestVM01
-Count 2 –Reserve 10 – Maximum 75 – RelativeWeight 200
Set-VMProcessor LegacyVM01
-CompatibilityForOlderOperatingSystemsEnabled $true
Building a virtual machine from scratch
New-VM –Name NewVM01 –NewVHDPath F:\NewVM01\disk1.VHDX
–NewVHDSize 50GB –Path F:\NewVM01
With more than one
disk, memory, processor, and network settings:
New-VM –Name NewVM02 –NewVHDPath F:\NewVM02\disk1.VHDX
–NewVHDSize 50GB –Path F:\NewVM02
New-VHD F:\NewVM02\Disk2.VHDX –Size 50GB
Get-VMHardDiskDrive
NewVM02
Add-VMHardDiskDrive
NewMV02 IDE 0 1 –Path F:\NewVM02\Disk2.vhdx
Get-VMHardDiskDrive NewVM02
Set-VMMemory NewVM02 –Startup 4GB
Set-VMProcessor NewVM02 –Count 4
$VMNic =
Get-VMNetworkAdapter –VMName NewVM02
Get-VMSwitch |
Select-Object Name
Connect-VMNetworkAdapter
–VMNetworkAdapter $VMNic –SwitchName “”
Get-VMNetworkAdapter
–VMName NewVM02 | Select-Object VMName, Name,
SwitchName
Deleting a virtual machine
Remove-VM NewVM03
Working with virtual machine snapshots
To create a
snapshot:
Get-VM LabVM01 | Checkpoint-VM
Get-VMSnapshot
–VMName LabVM01
Restore-VMSnapshot
–Name (Get-VMSnapshot –VMName “LabVM01”).Name
–VMName “LabVM01”
Remove-VMSnapshot
–Name (Get-VMSnapshot –VMName “LabVM01”).Name
–VMName “LabVM01”
Live migration of a virtual machine
Move-VM “LabVM03” HVHOST02
Move-VM “LabVM03” HVHOST02 –DestinationStoragePath F:\VMs
Getting help
Get-VM -?
Get-Help *-VM
Get-Help Suspend-*
New-VM -Whatif
Get-Command |
Where Definition –Like *whatif*
And check out:
Windows
PowerShellISE (IntelliSense)
Comments
Post a Comment