Script to upgrade vm hardware for list of VM

hello guys..

Here is the script to update the vm hardware for list of VM’s

$vms=Get-Content C:\Temp\servers.txt
foreach($MyVM in $vms){
Write-Host "========Before Upgrading===================" -ForegroundColor Yellow
$MyVM = Get-VM -Name vm10
Write-Host $MyVM.Name $MyVM.Version -ForegroundColor White
if ($MyVM.PowerState -eq "PoweredOn") {
 Write-Host "Shutting Down" $MyVM
 Stop-VMGuest -VM $MyVM -Confirm:$false
 do {
 Start-Sleep -s 5
 $MyVMStat = Get-VM -Name $MyVM
 $status = $MyVMStat.PowerState
 }until($status -eq "PoweredOff")
 $snapTask=New-Snapshot -VM $MyVM -Name HardwareUpgrade -RunAsync
 Wait-Task $snapTask
 $snapState= Get-Task -Id $snaptask.Id |Select State,VM
 if($snapState.State -eq "Success"){
 
 Write-Host $MyVM "Snapshot is created and proceeding for Hardware Upgrade"
 #Get-Snapshot -VM $MyVM|Select Name,Created
 $vmversiontask=Set-VM -VM $MyVM -Version v10 -Confirm:$false -RunAsync
 $wtask=Wait-Task $vmversiontask 
 write-host $wtask.Id,$vmversiontask.Id
 $state=Get-Task |Select-Object * |where{($_.ObjectId -eq $wtask.Id) -and ($_.Name -eq "UpgradeVM_Task")}

}
 
}
Write-Host "================After the hardware Upgrade===========" -ForegroundColor Yellow
Write-Host $MyVM.Name $MyVM.Version -ForegroundColor White
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s