有没有办法使用Powershell从Hyper-V获取VM操作系统名称? [英] Is there a way to get VMs Operating System name from Hyper-V using powershell?
问题描述
我正在HYPER-V主机上编写脚本,以获取VM来宾信息.有没有办法使用Powershell从Hyper-V获取VM操作系统名称?
I'm writing a script on HYPER-V host for getting VMs guest informations. Is there a way to get VMs Operating System name from Hyper-V using powershell?
有几个使用(Get-WmiObject Win32_OperatingSystem -ComputerName $ vmName).name
的示例,但是由于域限制,我应该直接从Hyper-V获取此信息.
There are several examples using (Get-WmiObject Win32_OperatingSystem -ComputerName $vmName).name
but i should get this information directly from Hyper-V because of domain restrictions.
我也正在使用powershell的hyper-v模块,但是我看不到任何与OS相关的cmdlet.
Also i'm using hyper-v module of powershell but i couldn't see any cmdlets related to OS.
推荐答案
可以从访客固有交换项中检索该信息.
This could be retrieved from guest intrinsic exchange items.
# 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"
}
这篇关于有没有办法使用Powershell从Hyper-V获取VM操作系统名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!