Many administrators prefer PowerShell to automatically execute component functions such as user creation and folder permission management. However, virtualization technology can also be managed through command lines, including Microsoft Hyper-V.
Recommended topics: script technology in Windows-Windows Powershell
Although there are multiple ways to use PowerShell to manage Hyper-V, this article will focus on how to use Windows WMI (open-source tool from CodePlex) for free.
Before using the WMI script to manage Hyper-V, it is important to know which classes are available. Microsoft lists a large number of classes. Although quite complete, they are not necessarily easy to use and are always not intuitive. Therefore, Using WMI to manage Hyper-V is not suitable for people with weak psychological endurance.
One of the popular methods to manage Hyper-V using PowerShell is to use the PowerShell management library for Hyper-VPSHyperV. This is a free and open-source CodePlex project written by James O'Neil. This is the best choice so far. It provides a complete set of cmdlet for administrators to handle everything from VM storage management to network management. Let's take a look at some of them:
Get-VM -- return all VMS on a Hyper-V server, as shown in Figure 1 ).
Figure 1: Get-VM command
The following code demonstrates the Get-VM command:
Function Get-VM{# .ExternalHelp MAML-VM.XML param( [parameter(ValueFromPipeLine = $true)] [ValidateNotNullOrEmpty()][Alias("VMName")] $Name = "%", [parameter()][ValidateNotNullOrEmpty()] $Server = ".", #May need to look for VM(s) on Multiple servers [Switch]$Suspended, [switch]$Running, [switch]$Stopped ) Process { # In case people are used to the * as a wildcard... if ($Name.count -gt 1 ) {[Void]$PSBoundParameters.Remove("Name"); $Name | ForEach-object {Get-VM -Name $_ @PSBoundParameters}} if ($name -is [String]) { $Name = $Name.Replace("*","%") # Note in V1 the test was for caption like "Virtual%" whichdid not work in languages other than English. # Thanks to Ronald Beekelaar - we now test for a processID ,the host has a null process ID, stopped VMs have an ID of 0. $WQL = "SELECT * FROM MSVM_ComputerSystem WHERE ElementNameLIKE '$Name' AND ProcessID >= 0" if ($Running -or $Stopped -or $Suspended) { $state = "" if ($Running) {$State += " or enabledState = " +[int][VMState]::Running } if ($Stopped) {$State += " or enabledState = " +[int][VMState]::Stopped } if ($Suspended) {$State += " or enabledState = " +[int][VMState]::Suspended } $state = $state.substring(4) $WQL += " AND ($state)" } Get-WmiObject -computername $Server -NameSpace $HyperVNamespace -Query $WQL | Add-Member -MemberType ALIASPROPERTY -Name "VMElementName" -Value "ElementName" -PassThru } elseif ($name.__class) { Switch ($name.__class) { "Msvm_ComputerSystem" {$Name} "Msvm_VirtualSystemSettingData" {get-wmiobject -computername $Name.__SERVER -namespace $HyperVNamespace -Query"associators of {$($name.__path)} whereresultclass=Msvm_ComputerSystem"} Default get-wmiobject -computername $Name.__SERVER -namespace $HyperVNamespace -Query"associators of {$($Name.__path)} whereresultclass=Msvm_VirtualSystemSettingData" | ForEach-Object{$_.getRelated("Msvm_ComputerSystem")} | Select-object -unique } } } }}
As you can see, this Code contains the basic WMI class and helper logic and reports the results.
Get-VMSwitch -- returns all virtual switches on the Hyper-V server, as shown in figure 2 ).
Figure 2: Get-VMSwitch command
The following code demonstrates the Get-VMSwitch command:
Function Get-VMSwitch{# .ExternalHelp MAML-VMNetwork.XML param( [parameter(ValueFromPipeline = $true)][Alias("Name")] [String]$VirtualSwitchName="%", [parameter()][ValidateNotNullOrEmpty()] $Server = "." #Can query multiple servers for switches ) process { $VirtualSwitchName=$VirtualSwitchName.replace("*","%") Get-WmiObject -computerName $server -NameSpace $HyperVNamespace-query "Select * From MsVM_VirtualSwitch Where elementname like '$VirtualSwitchname' " }}
Get-VMSnapShot -- provides all snapshots on the Hyper-V server, as shown in figure 3 ).
Figure 3: Get-VMSnapShot command
The following statement shows the Get-VMSnapShot command:
Function Get-VMSnapshot{# .ExternalHelp MAML-VMSnapshot.XML Param( [parameter(Position=0 , ValueFromPipeline = $true)] $VM = "%", [String]$Name="%", [parameter()][ValidateNotNullOrEmpty()] $Server="." , [Switch]$Current, [Switch]$Newest, [Switch]$Root ) process{ if ($VM -is [String]) {$VM=(Get-VM -Name $VM -Server $server) } if ($VM.count -gt 1 ) {[Void]$PSBoundParameters.Remove("VM") ; $VM |ForEach-object { Get-VMSnapshot -VM $_ @PSBoundParameters}} if ($vm.__CLASS -eq 'Msvm_ComputerSystem') { if ($current) {Get-wmiobject -computerNam $vm.__server -Namespace $HyperVNamespace -q "associators of {$($vm.path)} where assocClass=MSvm_PreviousSettingData"} else {$Snaps=Get-WmiObject -computerName $vm.__server -NameSpace $HyperVNameSpace -Query "Select * From MsVM_VirtualSystemSettingData Where systemName='$($VM.name)' and instanceID <> 'Microsoft:$($VM.name)' and elementName like '$name' " if ($newest) {$Snaps | sort-object -property creationTime | select-object -last 1 } elseif ($root) {$snaps | where-object {$_.parent -eq $null} } else {$snaps} } } }}
You can find multiple additional features of PSHyperV on the CodePlex website to help administrators perform tasks such as searching, operating, and configuring different components of hypervisor.
Write the WMI package and use PSHyperV, but the Administrator uses PowerShell to manage Hyper-V. Note that the latest version of PSHyperV is not complete, so it is not as stable as other software.