有没有办法使用powershell从Hyper-V获取VMs操作系统名称?

有没有办法使用powershell从Hyper-V获取VMs操作系统名称?,powershell,automation,virtual-machine,wmi,hyper-v,Powershell,Automation,Virtual Machine,Wmi,Hyper V,我正在HYPER-V主机上编写一个脚本,用于获取VMs来宾信息。有没有办法使用powershell从Hyper-V获取VMs操作系统名称 有几个例子使用了(Get-WmiObject Win32_OperatingSystem-ComputerName$vmName)。name,但由于域限制,我应该直接从Hyper-V获取此信息 此外,我正在使用powershell的hyper-v模块,但我看不到任何与操作系统相关的cmdlet。除非您使用的是SCVMM,否则无法通过hyper-v powers

我正在HYPER-V主机上编写一个脚本,用于获取VMs来宾信息。有没有办法使用powershell从Hyper-V获取VMs操作系统名称

有几个例子使用了
(Get-WmiObject Win32_OperatingSystem-ComputerName$vmName)。name
,但由于域限制,我应该直接从Hyper-V获取此信息


此外,我正在使用powershell的hyper-v模块,但我看不到任何与操作系统相关的cmdlet。

除非您使用的是SCVMM,否则无法通过hyper-v powershell cmdlet获得来宾操作系统详细信息


您必须像已经找到的那样查询来宾本身。

除非您使用的是SCVMM,否则来宾操作系统的详细信息无法通过Hyper-V PowerShell cmdlet获得


您必须像已经找到的那样查询来宾本身。

这可以从来宾内部交换项目中检索

# 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 "OSName"
}
catch {
    Write-Host "Not found"
}

从。

这可以从来宾内部交换项目中检索

# 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 "OSName"
}
catch {
    Write-Host "Not found"
}

来自。

您已经看过了吗?您已经看过了吗?这是一个非常好的答案。但若虚拟机停机,它将不起作用,因为事实上,若机器停机,任何解决方案都不会起作用+这真是个好答案。但若虚拟机停机,它将不起作用,因为事实上,若机器停机,任何解决方案都不会起作用+1.