Hyper-V and PowerShell : VMName to OSName mapping
I have been working on creating a Hyper-V app for Splunk that will allow Splunk users to associate Hypervisor data with products like XenDesktop, XenApp, and Exchange. One of the challenges I came across is mapping the Hypervisor VM name to the OS FQDN.
Thanks to Artem
Function
{
Param(
[Parameter()]
$ComputerName = $Env:ComputerName,
[Parameter()]
$VMName
)
# Creating HASH Table for object creation
$MyObj = @{}
# Getting VM Object
$Vm = Get-WmiObject -Namespace root\virtualization -Query "Select * From Msvm_ComputerSystem Where ElementName=’$VMName’" -ComputerName $ComputerName
# Getting VM Details
$Kvp = Get-WmiObject -Namespace root\virtualization -Query "Associators of {$Vm} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent" -ComputerName $ComputerName
# Converting XML to Object
foreach($CimXml in $Kvp.GuestIntrinsicExchangeItems)
{
$XML = [xml+site:msdn.microsoft.com”>XML]$CimXml
if($XML)
{
foreach ($CimProperty in $XML.SelectNodes("/INSTANCE/PROPERTY"))
{
switch -exact ($CimProperty.Name)
{
"Data" { $Value = $CimProperty.VALUE }
"Name" { $Name = $CimProperty.VALUE }
}
}
$MyObj.add($Name,$Value)
}
}
# Outputting Object
New-Object -TypeName PSCustomObject -Property $MyObj
}
Output looks like
OSName : Windows Server 2008 R2
EnterpriseOSMajorVersion : 6
ServicePackMinor : 0
ProductType : 3
OSPlatformId : 2
SuiteMask : 18
CSDVersion : Service Pack 1
OSVersion : 6.1.7601
FullyQualifiedDomainName : bd-xa60-01.home.lab
OSMinorVersion : 1
NetworkAddressIPv6 : fe80::78e4:e94b:b5c0:be0b%12
OSBuildNumber : 7601
ProcessorArchitecture : 9
RDPAddressIPv4 :
ServicePackMajor : 1
NetworkAddressIPv4 : 192.168.0.105
OSEditionId : 10
IntegrationServicesVersion : 6.1.7601.17514
2 Responses to “Hyper-V and PowerShell : VMName to OSName mapping”
Leave a Reply
You must be logged in to post a comment.


nice
indeed!