mikedopp

KeepVMtemplatesUpToDate

May 26th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################################
  2. YOU CAN RUN THESE SCRIPTS MANUALLY OR
  3. RUN THE START UPDATE SECTION AS A
  4. SCHEDULED TASK AND THE FINISH SECTION
  5. AS A SEPARATE TASK.
  6. ######################################
  7.  
  8. ### START UPDATE OF VM TEMPLATE ###
  9. #Import the PowerCLI module
  10. Add-PSSnapin VMware.VimAutomation.Core
  11.  
  12. #Host IP or vCenter Server IP/NAME
  13. $hostIP = '[vCenter Server]'
  14.  
  15. #Connect to vCenter
  16. Connect-VIServer $hostIP
  17.  
  18. #VM Templates to Update
  19. $VMTemplates = 'Win7ENTx64','WS2012StdR2'
  20.  
  21. Foreach($vm in $VMTemplates){
  22.  
  23.     #convert template to VM
  24.     Get-Template -Name $vm | Set-Template -ToVM
  25.  
  26.     #Power on VM
  27.     Start-VM -VM $vm
  28.  }
  29.  
  30. #Disconnect From vCenter Server
  31. Disconnect-VIServer $hostIP -Confirm:$false
  32.  
  33. ####################################
  34. ### FINISH UPDATE OF VM TEMPLATE ###
  35.  
  36. #Import the PowerCLI module
  37. Add-PSSnapin VMware.VimAutomation.Core
  38.  
  39. #Host IP or vCenter Server IP/NAME
  40. $hostIP = '[vCenter Server]'
  41.  
  42. #Connect to vCenter
  43. Connect-VIServer $hostIP
  44.  
  45. #VM Templates to Update
  46. $VMTemplates = 'Win7ENTx64','WS2012StdR2'
  47.  
  48.  foreach($vm in $VMTemplates){
  49.    
  50.     #check if VM is powered on
  51.     $ison = Get-VM -Name $vm
  52.    
  53.     if($ison.PowerState -eq 'PoweredOn'){
  54.        
  55.         #Shutdown VM
  56.         Get-VM -Name $vm | Shutdown-VMGuest -Confirm:$false
  57.  
  58.         #Wait for shutdown to complete
  59.         Start-Sleep -Seconds 300
  60.  
  61.         #Convert VM back to a Template
  62.         $tobeTemplate =  Get-VM $vm | Get-View
  63.         $tobeTemplate.MarkAsTemplate()
  64.  
  65.         }
  66.  
  67.     }
  68.  
  69. #Disconnect From vCenter Server
  70. Disconnect-VIServer $hostIP -Confirm:$false
Add Comment
Please, Sign In to add comment