Link to home
Start Free TrialLog in
Avatar of jmachado81
jmachado81

asked on

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 10.1.0.8403314.

#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

Avatar of footech
footech
Flag of United States of America image

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

Avatar of jmachado81
jmachado81

ASKER

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

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') } 

2012
$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

List_VM_by_OS_WmiObject.txt
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is great.  Thank you!