自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

系統(tǒng) Windows
許多管理員喜歡使用PowerShell來自動執(zhí)行用戶創(chuàng)建和文件夾權(quán)限管理這類組件功能,但是,虛擬化技術(shù)也可以通過命令行管理,包括微軟Hyper-V。雖然有多種方法可以用PowerShell來管理Hyper-V,但本文將重點(diǎn)介紹如何免費(fèi)使用Windows管理規(guī)范(WMI)腳本(來自CodePlex的開源工具)的方法。

許多管理員喜歡使用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)。

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邏輯并報(bào)告了結(jié)果。

Get-VMSwitch——返回所有在Hyper-V服務(wù)器上的虛擬交換(見圖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服務(wù)器上的快照(見圖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的網(wǎng)站上找到PSHyperV的多種附加功能來幫助管理員執(zhí)行查找、操作和配置hypervisor的不同的組件等相關(guān)任務(wù)。

編寫WMI包裝器和使用PSHyperV,只是管理員用PowerShell來管理Hyper-V的一些方式。請注意,PSHyperV的***版本并不是完整的版本,因此,它不像其他軟件那么穩(wěn)定。

 

【編輯推薦】

  1. PowerShell函數(shù)的基本指南與特性
  2. 利用PowerShell縮短SharePoint備份時(shí)間
  3. PowerShell與.NET框架之間的點(diǎn)連接
  4. WMI中的Windows PowerShell腳本使用方法
  5. 有關(guān)PowerShell腳本你必須知道的十個(gè)基本概念
責(zé)任編輯:張浩 來源: TechTarget中國
相關(guān)推薦

2011-11-21 10:27:43

虛擬化PowerShellHyper-V

2014-04-24 11:02:12

Hyper-V虛擬化主機(jī)

2009-06-30 18:05:12

Hyper-VVBScriptPowerShell

2011-01-25 10:17:24

微軟Hyper-V c

2011-04-01 11:15:19

微軟Hyper-V

2014-01-03 09:58:46

PowerShellHyper-V

2012-06-26 09:45:05

微軟

2011-11-01 08:53:58

虛擬化Hyper-V微軟

2011-11-01 09:17:35

微軟虛擬化Hyper-V

2009-09-09 08:57:39

Hyper-V

2011-01-25 10:06:42

2009-07-08 13:04:36

Hyper-V主機(jī)服務(wù)器配置

2013-11-20 14:41:08

PowerShellPowerShell Windows Ser

2013-11-20 14:46:43

PowerShellPowerShell Windows Ser

2009-08-13 09:58:12

微軟Hyper-V

2009-05-20 19:04:07

微軟Hyper-V虛擬化

2010-06-01 11:08:39

Hyper-VVMware

2009-03-16 19:17:40

微軟Hyper-VVmware

2014-04-24 10:53:01

Hyper-VHyper-V Ser

2011-08-02 10:13:41

Hyper-V紅帽Linux
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號