PowerShell Script for Automated Install of Hyper-V Integration Services in a VM running on Windows Server 2012 with Hyper-V V 3.0 Role using PowerShell Remoting
Hi Guys i stumbled upon Something Super Exciting i was able to do With Hyper-V V3 Installed on Windows Server 2012 server, I was fully able to automate the installation of integration services in a Virtual Machine using the New Hyper-V Powershell Cmdlets bundled with Windows Server 2012.
I created an advanced function to automate installation of Integration Services on a VM.
Here are some script sceenshots which illustrate the entire script run process from start to finish :) wohooo :) its a cool achievement
Now if i Run the script again that is after the integration services are upgraded it gives me this output :),
Neat!!, it shows that we are running on latest version of Integration Services as Hyper-V Version 3.0
First i went through the Hyper-V Module and i was amazed to see that now we are being shipped with 164 :) set of New Cmdlets, plain Awesome!!, and the new cmdlets were much feature packed as compared to the CodePlex edition of Hyper-V Modules.
In The Script i'm using a new cmdlet being shipped with Hyper-V v3 which can be used to
associate an .iso image with a Virtual Machine.
Set-VMDvdDrive -VMName $vm -Path "C:\Windows\System32\vmguest.iso"
After This i extracted the DVD Drive letter for a VM using the new cmdlet Get-VMDvdDrive.
$DVDriveLetter = Get-VMDvdDrive -VMName $vm | select -ExpandProperty id | Split-Path -Leaf
I was able to get a little help from Adam in his blog for Hyper-V http://csharpening.net/?p=1052 which shows on how to extract the DVD drive letter using split-path and its parameter -Leaf.
$HostICversion= Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestInstaller\Version" | select -ExpandProperty Microsoft-Hyper-V-Guest-Installer
$VMICversion = Invoke-Command -ScriptBlock {Get-ItemProperty "HKLM:\software\microsoft\virtual machine\auto" | select -ExpandProperty integrationservicesversion } -ComputerName $comp -Credential $cred
Then i made a compare between them in an if-else loop, The If condition evaluated out as below and streamed the output to the pipeline with the Integration Service Version on Both the VM and Hyper-V Host.
if($HostICversion -eq $VMICversion) {
Write-Verbose "Hyper-V Host IC Version and the VM $vm IC Version are the same" -Verbose
$obj = New-Object psobject -Property @{
'HostIntegration Services Version' = $HostICversion
'VMIntegration Services Version' = $VMICversion
'Hyper-V Host Name' = hostname
'VirtualMachine Name'= $vm
}
Write-Output $obj
Once the script see's that Integration Services Version is the same on both the VM and The Hyper-V Host, i used the Set-VMDVDdrive cmdlet to eject the vmguest.iso attached to the VM
Set-VMDvdDrive -VMName $vm -ControllerNumber 1 -ControllerLocation 0 -Path $null
In the Else Loop i used Invoke-Wmi Method to Install the integration services on the Virtual Machine with out a restart.
Invoke-WmiMethod -ComputerName $comp -Class Win32_Process -Name Create -ArgumentList "$($DVDriveLetter):\support\x86\setup.exe /quiet /norestart" -Credential $cred
Once the wmi command got completed i put a while loop in the script to check if the setup service got started and waited for it to finish, which implicates wait for the integration service to get installed.
start-sleep 3
while (@(Get-Process setup -computername $comp -ErrorAction SilentlyContinue).Count -ne 0) {
Start-Sleep 3
Write-verbose "Waiting for Integration Service Install to Finish on $comp ..." -Verbose
}
write-verbose "Completed the Installation of Integration Services" -Verbose
write-verbose "Restarting Computer for Changes to Take Place" -Verbose
Once the installation was done i invoked a Restart-Computer with a wait for WinRM service to come up :), now thats a super neat feature in New PowerShell v3 :)
Restart-Computer -ComputerName $comp -Wait -For WinRM -Force -Credential $cred
Once the Server Got Restarted i was able to see this pretty cool output on my powershell prompt that the integration services version did get upgraded to latest as per Hyper-V 3 :)
write-verbose "$vm Is Online Now" -Verbose
$VMICversion = Invoke-Command -ScriptBlock {Get-ItemProperty "HKLM:\software\microsoft\virtual machine\auto" | select -ExpandProperty integrationservicesversion } -ComputerName $comp -Credential $cred
write-verbose "$vm New Integration Services Version $VMICversion" -Verbose
You can Find the Entire Script For Your Reference at
http://gallery.technet.microsoft.com/scriptcenter/Automated-Install-of-Hyper-edc278ef
http://powershell-enthusiast.blogspot.in/2012/10/powershell-script-for-automated-install.html
Thanks & Regards
Vinith
Then i went on and extracted the Hyper-V Host Integration Service Version and The Virtual Machine Integration Service version using the below commands.