locked
How to get VM' operating system name using powershell RRS feed

  • General discussion

  • Hi

    Using Cluster Managements mmc I can see for example VM's operating system type: Windows XP, windows 7. How can I get this information using powershell.


    Kind Regards Tomasz


    • Edited by Yukio Seki Wednesday, August 12, 2015 8:15 PM bug
    Wednesday, August 12, 2015 8:14 PM

All replies

  • I don't believe there is a way to do this without querying the the guest VM for the information.  

    For example, you can use the Get-CimInstance cmdlet to remotely query this information:

    Get-CimInstance -ComputerName MYSERVERNAME Win32_OperatingSystem | Select Caption
    • Edited by Chris-Jones Thursday, August 13, 2015 12:09 AM
    Thursday, August 13, 2015 12:09 AM
  • If running VMM you can do the below, not aware of a method native to Hyper-V though, you would have to hit the VM via WMI as Chris states above.

    Get-SCVirtualMachine -name HTS020 | select operatingsystem

    OperatingSystem
    ---------------
    Windows Server 2003 Standard x64 Edition

    Thursday, August 13, 2015 1:57 PM
  • Hi

    I don't use System Center so in powershell i don't have this command: Get-SCVirtualMachine

    Using Get-VM I can get only VM name no Bios name so I can't use Get-CimInstance -ComputerName MYSERVERNAME Win32_OperatingSystem | Select Caption.

    I want to use this metod in scirpt so I can put -ComputerName manually.

    First i need to get real computer name (not VM name) and then i can use it " Get-CimInstance -ComputerName MYSERVERNAME Win32_OperatingSystem | Select Caption." But how can I get real computer name? Which powershell command for VM will give me this value?


    Kind Regards Tomasz

    Thursday, August 13, 2015 2:14 PM
  • This is the only information you have available to you i'm afraid:

    get-vm -ComputerName HyperVServerName -Name VMName | fl *
    VMName                      : TS004
    VMId                        : 119549c2-e202-42d5-97bc-0a29ce2c9873
    Id                          : 119549c2-e202-42d5-97bc-0a29ce2c9873
    Name                        : TS004
    State                       : Running
    IntegrationServicesState    : Up to date
    OperationalStatus           : {Ok}
    PrimaryOperationalStatus    : Ok
    SecondaryOperationalStatus  :
    StatusDescriptions          : {Operating normally}
    PrimaryStatusDescription    : Operating normally
    SecondaryStatusDescription  :
    Status                      : Operating normally
    Heartbeat                   : OkApplicationsHealthy
    ReplicationState            : Disabled
    ReplicationHealth           : NotApplicable
    ReplicationMode             : None
    CPUUsage                    : 0
    MemoryAssigned              : 4294967296
    MemoryDemand                : 0
    MemoryStatus                :
    SmartPagingFileInUse        : False
    Uptime                      : 01:05:53
    IntegrationServicesVersion  : 6.3.9600.16384
    ResourceMeteringEnabled     : False
    ConfigurationLocation       : C:\ClusterStorage\Volume1\TS004
    SnapshotFileLocation        : C:\ClusterStorage\Volume1\TS004\
    AutomaticStartAction        : Nothing
    AutomaticStopAction         : Save
    AutomaticStartDelay         : 0
    SmartPagingFilePath         : C:\ClusterStorage\Volume1\TS004\
    NumaAligned                 : True
    NumaNodesCount              : 1
    NumaSocketCount             : 1
    Key                         : Microsoft.HyperV.PowerShell.VirtualMachineObjectKey
    IsDeleted                   : False
    ComputerName                : HyperVServerName
    Version                     : 5.0
    Notes                       : #CLUSTER-INVARIANT#:{732dadf7-6fea-4266-9f36-929e53d928f0}
    Generation                  : 1
    Path                        : C:\ClusterStorage\Volume1\TS004
    CreationTime                : 16/05/2014 16:33:29
    IsClustered                 : True
    SizeOfSystemFiles           : 54696
    ParentSnapshotId            :
    ParentSnapshotName          :
    MemoryStartup               : 4294967296
    DynamicMemoryEnabled        : False
    MemoryMinimum               : 536870912
    MemoryMaximum               : 1099511627776
    ProcessorCount              : 2
    RemoteFxAdapter             :
    NetworkAdapters             : {TS004}
    FibreChannelHostBusAdapters : {}
    ComPort1                    : Microsoft.HyperV.PowerShell.VMComPort
    ComPort2                    : Microsoft.HyperV.PowerShell.VMComPort
    FloppyDrive                 : Microsoft.HyperV.PowerShell.VMFloppyDiskDrive
    DVDDrives                   : {DVD Drive on IDE controller number 1 at location 0}
    HardDrives                  : {Hard Drive on IDE controller number 0 at location 0}
    VMIntegrationService        : {Time Synchronization, Heartbeat, Key-Value Pair Exchange, Shutdown...

    now you have the NIC you can lookup the NICs IP

     Get-VMNetworkAdapter -ComputerName HyperVServerName -VMName TS004

    Name   IsManagementOs VMName SwitchName                 MacAddress   Status IPAddresses
    ----   -------------- ------ ----------                 ----------   ------ -----------
    TS004 False          TS004 VM Logical Switch 00155DC804A5 {Ok}   {10.250.111.34}

    From there do a reverse DNS lookup. Job Done.


    Thursday, August 13, 2015 2:21 PM
  • Anything else we can help with?


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember, if you see a post that helped you please click "Vote as Helpful", and if it answered your question, please click "Mark as Answer". I do not work for Microsoft, I manage a large estate in the public sector, my views are generally first hand production experiences.

    Thursday, August 27, 2015 8:47 AM
  • Hi

    Using this metod I get computer DNS name., but i need  Windows OS version, for example Windows XP, Windows 2008R2...


    Kind Regards Tomasz

    Thursday, August 27, 2015 7:19 PM
  • add the following into the script...

    $computer = "ServerName"
    $query = Get-WmiObject -comp $computer -class Win32_OperatingSystem
    $query.caption


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember, if you see a post that helped you please click "Vote as Helpful", and if it answered your question, please click "Mark as Answer". I do not work for Microsoft, I manage a large estate in the public sector, my views are generally first hand production experiences.

    Friday, August 28, 2015 9:58 AM
  • cls $HyperVServer = "HyperV920a01" Foreach ($VMGuest in get-vm -ComputerName $HyperVServer) { $NetworkConfig = Get-VMNetworkAdapter -ComputerName $HyperVServer -VMName $VMGuest.Name $serverName = $VMGuest.Name $IP = $NetworkConfig.ipaddresses | where {$_ -notlike "*:*"} if (Test-connection $IP ) { $query = Get-WmiObject -comp $ip -class Win32_OperatingSystem $OSver = $query.caption Write-Host "$servername IP Address is $ip Running $OSVer" } }

    Try this, will check all windows servers on a hyperV host, assuming it has an IPv4 address. You will need to sort out some error handling though. Output will be similar to below:

    SomeRandomTestServer.MyDomain.Local IP Address is 10.10.25.27 Running Microsoft Windows Server 2012 R2 Standard


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember, if you see a post that helped you please click "Vote as Helpful", and if it answered your question, please click "Mark as Answer". I do not work for Microsoft, I manage a large estate in the public sector, my views are generally first hand production experiences.

    Friday, August 28, 2015 10:33 AM
  • For each VM's computername, query AD for Windows version. Inventorying Computers with AD PowerShell | Ask the Directory Services Team. 
    Thursday, February 18, 2016 7:15 PM
  • Hi

    Using Cluster Managements mmc I can see for example VM's operating system type: Windows XP, windows 7. How can I get this information using powershell.


    Kind Regards Tomasz


    The information that you see in Failover Cluster Manager is populated from the intrinsic KVP information passed up to the host from the Hyper-V Data Exchange Service running in the guest. Tapping into it isn't the easiest thing in the world, but I wrote some modules that can help you out. If all you want is this intrinsic information, all you need is the host-side module. Modules: http://www.altaro.com/hyper-v/hyper-v-key-value-pair-data-exchange-part-2-implementation/

    Once you have the module installed, getting this information, and everything else in the intrinsic KVP, is a short one-liner:

    Get-VMKvpGuestToHostInstrinsic <vmname>
    No need to go network-hopping or directory-surfing.


    Eric Siron
    Altaro Hyper-V Blog
    I am an independent blog contributor, not an Altaro employee. I am solely responsible for the content of my posts.

    Thursday, February 18, 2016 8:13 PM
  • get-vm VMName | % {write-host $_.Guest.OSFullName}
    Thursday, March 9, 2017 2:50 PM
  • This function worked for me.

    function Get-VMGuestInfo
    {
    <#
        .SYNOPSIS
     
            Gets virtual machine guest information
     
        .EXAMPLE
     
            Get-VMGuestInfo -VMName Test01
     
        .EXAMPLE
     
            Get-VMGuestInfo -VMName Test01 -HyperVHost Host01
     
        .NOTES
     
            Author: Yusuf Ozturk
            Website: http://www.yusufozturk.info
            Email: ysfozy[at]gmail.com
     
    #>
     
    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
     
        [Parameter(
            Mandatory = $true,
            HelpMessage = 'Virtual Machine Name')]
        $VMName,
     
        [Parameter(
            Mandatory = $false,
            HelpMessage = 'Hyper-V Host Name')]
        $HyperVHost = "localhost",
     
    	[Parameter(
            Mandatory = $false,
            HelpMessage = 'Debug Mode')]
        [switch]$DebugMode = $false
    )
    	# Enable Debug Mode
    	if ($DebugMode)
    	{
    		$DebugPreference = "Continue"
    	}
    	else
    	{
    		$ErrorActionPreference = "silentlycontinue"
    	}
     
    	$VMState = (Get-VM -ComputerName $HyperVHost -Name $VMName).State
     
    	if ($VMState -eq "Running")
    	{
    		filter Import-CimXml
    		{
    			$CimXml = [Xml]$_
    			$CimObj = New-Object -TypeName System.Object
    			foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY"))
    			{
    				if ($CimProperty.Name -eq "Name" -or $CimProperty.Name -eq "Data")
    				{
    					$CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
    				}
    			}
    			$CimObj
    		}
     
    		$VMConf = Get-WmiObject -ComputerName $HyperVHost -Namespace "root\virtualization\v2" -Query "SELECT * FROM Msvm_ComputerSystem WHERE ElementName like '$VMName' AND caption like 'Virtual%' "
    		$KVPData = Get-WmiObject -ComputerName $HyperVHost -Namespace "root\virtualization\v2" -Query "Associators of {$VMConf} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
    		$KVPExport = $KVPData.GuestIntrinsicExchangeItems
     
    		if ($KVPExport)
    		{
    			# Get KVP Data
    			$KVPExport = $KVPExport | Import-CimXml
     
    			# Get Guest Information
    			$VMOSName = ($KVPExport | where {$_.Name -eq "OSName"}).Data
    			$VMOSVersion = ($KVPExport | where {$_.Name -eq "OSVersion"}).Data
    			$VMHostname = ($KVPExport | where {$_.Name -eq "FullyQualifiedDomainName"}).Data
    		}
    		else
    		{
    			$VMOSName = "Unknown"
    			$VMOSVersion = "Unknown"
    			$VMHostname = "Unknown"
    		}
    	}
    	else
    	{
    		$VMOSName = "Unknown"
    		$VMOSVersion = "Unknown"
    		$VMHostname = "Unknown"
    	}
     
    	$Properties = New-Object Psobject
    	$Properties | Add-Member Noteproperty VMName $VMName
    	$Properties | Add-Member Noteproperty VMHost $HyperVHost
    	$Properties | Add-Member Noteproperty VMState $VMState
    	$Properties | Add-Member Noteproperty VMOSName $VMOSName
    	$Properties | Add-Member Noteproperty VMOSVersion $VMOSVersion
    	$Properties | Add-Member Noteproperty VMHostname $VMHostname
    	Write-Output $Properties
    }

    Monday, May 20, 2019 10:18 AM