从Get-VM获取操作系统名称

时间:2015-07-27 14:25:42

标签: powershell operating-system virtual-machine hyper-v

如何使用PowerShell从Hyper-V获取虚拟机的操作系统名称?

我试过

get-vm

但是这个领域不见了。

2 个答案:

答案 0 :(得分:0)

如果您使用Virtual Machine Manager,则可以运行类似的操作     get-vm | ft Name,OperatingSystem VMM for operating system中的数据通常是正确的,但在极少数情况下可能会显示为缺失或不正确。

答案 1 :(得分:0)

这可以从访客内在交换项中检索。

# Filter for parsing XML data
filter Import-CimXml 
{ 
   # Create new XML object from input
   $CimXml = [Xml]$_ 
   $CimObj = New-Object -TypeName System.Object 

   # Iterate over the data and pull out just the value name and data for each entry
   foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Name']")) 
      { 
         $CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE 
      } 

   foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY[@NAME='Data']")) 
      { 
         $CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE 
      } 

   # Display output
   $CimObj 
} 

# Prompt for the Hyper-V Server to use
$HyperVServer = Read-Host "Specify the Hyper-V Server to use (enter '.' for the local computer)"

# Prompt for the virtual machine to use
$VMName = Read-Host "Specify the name of the virtual machine"

# Get the virtual machine object
$query = "Select * From Msvm_ComputerSystem Where ElementName='" + $VMName + "'"
$Vm = gwmi -namespace root\virtualization\v2 -query $query -computername $HyperVServer

# Get the KVP Object
$query = "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
$Kvp = gwmi -namespace root\virtualization\v2 -query $query -computername $HyperVServer

Write-Host
Write-Host "Guest KVP information for" $VMName

# Filter the results
try {
    $Kvp.GuestIntrinsicExchangeItems | Import-CimXml | where Name -eq "FullyQualifiedDomainName"
}
catch {
    Write-Host "Not found"
}

来自Ben Armstrong’s Virtualization Blog

相关问题