如何使用PowerShell管理微軟Hyper-V
許多管理員喜歡使用PowerShell來自動執(zhí)行用戶創(chuàng)建和文件夾權(quán)限管理這類組件功能,但是,虛擬化技術(shù)也可以通過命令行管理,包括微軟Hyper-V。
推薦專題:Windows中的腳本技術(shù)-Windows Powershell
雖然有多種方法可以用PowerShell來管理Hyper-V,但本文將重點(diǎn)介紹如何免費(fèi)使用Windows管理規(guī)范(WMI)腳本(來自CodePlex的開源工具)的方法。
在使用WMI腳本來管理Hyper-V之前,了解哪些類可用很重要。微軟列出了大量的類。雖然相當(dāng)完整,但他們不一定易于使用,并且總是不直觀。因此,使用WMI來管理Hyper-V不適合心理承受能力弱的人。
使用PowerShell管理Hyper-V的比較流行方法之一是使用針對Hyper-V(PSHyperV)的PowerShell管理庫。這是由James O’Neil所寫的免費(fèi)且開源的CodePlex項(xiàng)目。這是迄今為止***的選擇。它提供一個(gè)完整cmdlet集給管理員使用,可以處理從虛擬機(jī)存儲管理到網(wǎng)絡(luò)管理的所有事情。讓我們來了解其中的一些:
Get-VM——返回一個(gè)Hyper-V服務(wù)器上所有的虛擬機(jī)(見圖1)。
圖1: Get-VM命令
下面的代碼展示了Get-VM命令:
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%" which did 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 ElementName LIKE '$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)} where resultclass=Msvm_ComputerSystem"} Default get-wmiobject - computername $Name.__SERVER -namespace $HyperVNamespace -Query "associators of {$($Name.__path)} where resultclass=Msvm_VirtualSystemSettingData" | ForEach-Object {$_.getRelated("Msvm_ComputerSystem")} | Select-object -unique } } } } }
如您所見,這段代碼包含了WMI基本類和helper邏輯并報(bào)告了結(jié)果。
Get-VMSwitch——返回所有在Hyper-V服務(wù)器上的虛擬交換(見圖2)。
圖2: Get-VMSwitch命令
下面的代碼展示了Get-VMSwitch的命令:
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——提供所有在Hyper-V服務(wù)器上的快照(見圖3)。
圖3:Get-VMSnapShot命令
下面的語句展示了Get-VMSnapShot命令:
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} } } } }
可以從CodePlex的網(wǎng)站上找到PSHyperV的多種附加功能來幫助管理員執(zhí)行查找、操作和配置hypervisor的不同的組件等相關(guān)任務(wù)。
編寫WMI包裝器和使用PSHyperV,只是管理員用PowerShell來管理Hyper-V的一些方式。請注意,PSHyperV的***版本并不是完整的版本,因此,它不像其他軟件那么穩(wěn)定。
【編輯推薦】