如何使用PowerShell管理微软Hyper-V

系统 Windows
许多管理员喜欢使用PowerShell来自动执行用户创建和文件夹权限管理这类组件功能,但是,虚拟化技术也可以通过命令行管理,包括微软Hyper-V。虽然有多种方法可以用PowerShell来管理Hyper-V,但本文将重点介绍如何免费使用Windows管理规范(WMI)脚本(来自CodePlex的开源工具)的方法。

许多管理员喜欢使用PowerShell来自动执行用户创建和文件夹权限管理这类组件功能,但是,虚拟化技术也可以通过命令行管理,包括微软Hyper-V。

 

推荐专题:Windows中的脚本技术-Windows Powershell

虽然有多种方法可以用PowerShell来管理Hyper-V,但本文将重点介绍如何免费使用Windows管理规范(WMI)脚本(来自CodePlex的开源工具)的方法。

在使用WMI脚本来管理Hyper-V之前,了解哪些类可用很重要。微软列出了大量的类。虽然相当完整,但他们不一定易于使用,并且总是不直观。因此,使用WMI来管理Hyper-V不适合心理承受能力弱的人。

使用PowerShell管理Hyper-V的比较流行方法之一是使用针对Hyper-V(PSHyperV)的PowerShell管理库。这是由James O’Neil所写的免费且开源的CodePlex项目。这是迄今为止***的选择。它提供一个完整cmdlet集给管理员使用,可以处理从虚拟机存储管理到网络管理的所有事情。让我们来了解其中的一些:

Get-VM——返回一个Hyper-V服务器上所有的虚拟机(见图1)。

PowerShell,Hyper-V
图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逻辑并报告了结果。

Get-VMSwitch——返回所有在Hyper-V服务器上的虚拟交换(见图2)。

PowerShell,Hyper-V
图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服务器上的快照(见图3)。

PowerShell,Hyper-V
图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的网站上找到PSHyperV的多种附加功能来帮助管理员执行查找、操作和配置hypervisor的不同的组件等相关任务。

编写WMI包装器和使用PSHyperV,只是管理员用PowerShell来管理Hyper-V的一些方式。请注意,PSHyperV的***版本并不是完整的版本,因此,它不像其他软件那么稳定。

 

【编辑推荐】

  1. PowerShell函数的基本指南与特性
  2. 利用PowerShell缩短SharePoint备份时间
  3. PowerShell与.NET框架之间的点连接
  4. WMI中的Windows PowerShell脚本使用方法
  5. 有关PowerShell脚本你必须知道的十个基本概念
责任编辑:张浩 来源: TechTarget中国
相关推荐

2009-06-30 18:05:12

Hyper-VVBScriptPowerShell

2014-04-24 11:02:12

Hyper-V虚拟化主机

2011-11-21 10:27:43

虚拟化PowerShellHyper-V

2011-01-25 10:17:24

微软Hyper-V c

2011-04-01 11:15:19

微软Hyper-V

2012-06-26 09:45:05

微软

2014-01-03 09:58:46

PowerShellHyper-V

2011-01-25 10:06:42

2009-09-09 08:57:39

Hyper-V

2009-07-08 13:04:36

Hyper-V主机服务器配置

2011-11-01 08:53:58

虚拟化Hyper-V微软

2011-11-01 09:17:35

微软虚拟化Hyper-V

2009-03-16 19:17:40

微软Hyper-VVmware

2014-04-24 10:53:01

Hyper-VHyper-V Ser

2012-06-29 16:52:21

微软OpenStackHyper-V

2011-08-02 10:13:41

Hyper-V红帽Linux

2013-11-20 14:46:43

PowerShellPowerShell Windows Ser

2009-07-22 18:44:17

Hyper-V快照备份

2011-02-16 14:16:13

Hyper-V clo虚拟资源

2013-11-20 14:41:08

PowerShellPowerShell Windows Ser
点赞
收藏

51CTO技术栈公众号