Set Operating System based on Integration Services information

“Why is the “Install Virtual Guest Services” greyed out on some VM’s?” Because you have not selected an Operating system in VMM. 🙂
The option is greyed out because VMM does not know the compatibility of the OS with intergration services.

We had a bunch of VM’s with operating system “Unknown” and in the Hyper-V manager I can see what OS is running on the VM but not in VMM.
Powershell to the rescue.

setos

This script will query all your hosts, and foreach VM running on this host it will check if it has an OS selected in VMM.
If that’s not the case it will try to read the current OS through Intergration Services which actually happens via WMI.

Notes:
-This only works for Windows VM’s
The naming convention in VMM of the operating systems failed 🙂 Intergration services shows “CentOS Linux release 6.0 (final)” and VMM “CentOS Linux 6 (64 bit)”
-Only running VM’s will be seen by the script (Intergration services must be running to read the OS)
-You will need the VMM powershell module
-You will need WMI acces to your hosts

$VMhosts = get-scvmhost

$VMhosts | foreach {
$VMhost = $_.name
$VMs = Get-SCVirtualMachine -vmhost $VMhost | where {$_.Operatingsystem -match "Unknown" -and $_.status -match "Running"}
$VMs | foreach {
$vmname = $_.name
$Operatingsystemname = $null
$OperatingSystem = $null
$vmobject = Get-WmiObject -Namespace root\virtualization\v2 -ComputerName $VMhost -Class Msvm_ComputerSystem -Filter "ElementName='$vmname'"
$GuestIntrinsicExchangeItems  = $vmobject.GetRelated("Msvm_KvpExchangeComponent").GuestIntrinsicExchangeItems

$GuestIntrinsicExchangeItems  | foreach {

       $GuestExchangeItemOSname = ([XML]$_).SelectNodes("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text()='OSName']")
       if ($GuestExchangeItemOSname -ne $null)
       {$OSname = $GuestExchangeItemOSname.SelectNodes("/INSTANCE/PROPERTY[@NAME='Data']/VALUE/child::text()").Value}  

       $GuestExchangeItemProcA = ([XML]$_).SelectNodes("/INSTANCE/PROPERTY[@NAME='Name']/VALUE[child::text()='ProcessorArchitecture']")
       if ($GuestExchangeItemProcA -ne $null)
       {$ProcessorArchitecture = $GuestExchangeItemProcA.SelectNodes("/INSTANCE/PROPERTY[@NAME='Data']/VALUE/child::text()").Value}    
}

# If architecture x86
If ($ProcessorArchitecture -eq 0) {
$text = "32-bit"
$Operatingsystemname = $OSname + ' ' + $text
}
# If architecture x64
If ($ProcessorArchitecture -eq 9) {
$text = "64-bit edition of"
$Operatingsystemname =  $text + ' ' + $OSname
}
$OperatingSystem = Get-SCOperatingSystem | where {$_.Name -eq $Operatingsystemname}
$OperatingSystem
#If OperatingSystem found, set on the VM.
if($OperatingSystem){
Set-SCVirtualMachine -VM $_ -OperatingSystem $OperatingSystem
}
}
}

Of course you can change the script to recognize other Operating systems, be my guest.
Use at your own risk!

Leave a Reply

Your email address will not be published. Required fields are marked *