Differentiate 2012 from 2012 R2 OS for VMs in Powershell

I am assigning tags by OS so we can easily identify the version and have completed all except 2012.  The GuestIDs and GuestOS should be the same so i cannot use the same method as i did for others.  I suspect i'd need a WMI search added to this powershell but I'm unsure.  

Is there a way to modify this script to differentiate between 2012 and 2012 R2 in VMware?  I'm running vCenter 6.5 with ESXi 6.0/6.5 with Powershell 5.1.14409.1012 and PowerCLI

#Enter the Tag you want to search for or assign 
$tag= "Win 2012"

#Get the virtual machines of a department in your organization.
$vms = Get-VM | Where { ($_.GuestId -eq "windows8Server64Guest") }

#Assign the custom tag to the group of virtual machines.
$vms | New-TagAssignment -Tag $tag

#Get all virtual machines tagged with the tag.
Get-VM -Tag $tag

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't think VMWare cares if the guest is 2012 or 2012 R2, so they don't gather that information, leaving the route to get the information querying the guest directly.  Here's an example query:
# Have to supply credentials of account that is member of administrators group on target machine if running
# the command from an account that isn't
$cred = Get-Credential
Get-VM  | select Name,@{n="type";e={($_.guest -split ':')[-1]}},@{n="OS";e={If ($_.guest -match 'Windows')
{(Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption}Else{""}}}

Open in new window

jmachado81Author Commented:
I slightly modified the script to display the powered on and Server OSs only.  Is there a way to have this script output ONLY 2012 or 2012 R2 OS?

Get-VM | where { ($_.PowerState -eq 'poweredOn') -and ($_.guest -match 'Microsoft Windows Server') } | select Name,@{n="type";e={($_.guest -split ':')[-1]}},@{n="OS";e={If ($_.guest -match 'Microsoft Windows Server')
{(Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption}Else{""}}} | Sort Name | ft -AutoSize 

Open in new window

If i can attach this to a variable it would be helpful in scripting for a list of VMs.
$vms = above script
If you tried appending the pipe to Export-CSV after Format-Table, that would certainly not give good results, otherwise I see no possible way you could get more than the three properties with the Select command.

I wonder if there's a timeout happening with your larger system.  We could try breaking things up a little to test.  If $info doesn't contain output, then you know there's something amiss with the PowerCli session (Get-Vm isn't returning what you expect).
$cred = Get-Credential
$info = Get-VM | where { ($_.PowerState -eq 'poweredOn') -and ($_.guest -match 'Windows') } | select Name,@{n="type";e={($_.guest -split ':')[-1]}}
$info | Select Name,Type,@{n="OS";e={(Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption}} | ft -AutoSize

Open in new window

Cloud as a Security Delivery Platform for MSSPs

Every Managed Security Service Provider (MSSP) needs a platform to deliver effective and efficient security-as-a-service to their customers. Scale, elasticity and profitability are a few of the many features that a Cloud platform offers. View our on-demand webinar to learn more!

jmachado81Author Commented:
I was finally able to get it working with literally a little patience.  It took 30-45min to run and I've attached the script i put together with your help.

Now I'm taking those parts to output a list of VMs to tag them in powercli, but i can't seem to get either functioning as a one-liner.  Do you have any suggestions?

2012 R2
$vms = Get-VM | where { ($_.PowerState -eq 'poweredOn') -and ($_.guest -match 'Microsoft Windows Server 2012') }
$vms | Where-Object { ((Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption -match 'Microsoft Windows Server 2012 R2') } 

$vms = Get-VM | where { ($_.PowerState -eq 'poweredOn') -and ($_.guest -match 'Microsoft Windows Server 2012') }
$vms | Where-Object { ((Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption -eq 'Microsoft Windows Server 2012 Standard') -or ((Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption -eq 'Microsoft Windows Server 2012 Datacenter')  }

Open in new window

Perhaps something like this?
$vms = Get-VM | Where { ($_.PowerState -eq 'poweredOn') -and ($_.guest -match 'Microsoft Windows Server 2012') } |
 Select Name,@{n="Type";e={($_.guest -split ':')[-1]}},
        @{n="OS";e={(Get-WmiObject Win32_OperatingSystem -ComputerName $_.name -credential $cred).caption}} |
 Sort Name

$vms | ForEach `
    If ( $_.OS -match 'Microsoft Windows Server 2012 R2' )
        #do something with tags
    ElseIf ( ($_.OS -eq 'Microsoft Windows Server 2012 Standard') -or ($_.OS -eq 'Microsoft Windows Server 2012 Datacenter') )
        #do something else

Open in new window

I'm not bothering with trying to come up with a one-liner, just not performing the same action more than once.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jmachado81Author Commented:
That is great.  Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.