Advertisement
JakeBentz

Patch_Template

Apr 24th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # /=======================================================================
  2. # /=
  3. # /=  Patch_Template.ps1
  4. # /=
  5. # /=  AUTHOR: Jake Bentz
  6. # /=  DATE: 10/11/2016
  7. # /=
  8. # /=  REQUIREMENTS: Server 2008/2012 Templates need to be running PowerShell v.3 minimum but v.4 is highly recommended.
  9. # /=  The PSWindowsUpdate Module directory needs to exist in the PSModulePath on the template. https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc
  10. # /=  Encrypted password file for local administrator account
  11. # /=  Exectuion Policy on template needs to be Remote-Signed
  12. # /=
  13. # /=  DESCRIPTION: This script patches the specified templates
  14. # /=
  15. # /=  REVISION HISTORY
  16. # /=   VER  DATE        AUTHOR/EDITOR   COMMENT
  17. # /=   1.0  10/11/2016  Jake Bentz      Created script
  18. # /=
  19. # /=======================================================================#
  20. #
  21. #Create variable for list of templates
  22. $Template = "WindowsTemplate"
  23. #
  24. # Create log folder if it doesn't exist
  25. $Logpath = ".\output"
  26. If ((Test-Path -Path $Logpath) -ne $true) { New-Item -ItemType directory -Path $Logpath}
  27. #
  28. # Create log file
  29. $Logfile = ".\output\Patch_Template"+ (get-date -format '_ddMMMyyyy_hhmm') +".txt"
  30. Write "================================================================" > $Logfile
  31. Write "Patch Template Output File" >> $Logfile
  32. Write "================================================================" >> $Logfile
  33. #
  34. # Convert template to VM
  35. Write "============================" >> $Logfile
  36. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  37. $Output = $Output += " Converting to VM "
  38. $Output = $Output += $Template
  39. Write $Output >> $Logfile
  40. Set-Template -Template $Template -ToVM -Confirm:$false >> $Logfile
  41. Start-sleep -s 60
  42. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  43. $Output = $Output += " Complete"
  44. Write $Output >> $Logfile
  45. Write "============================" >> $Logfile
  46. #
  47. # Start VM
  48. Write "============================" >> $Logfile
  49. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  50. $Output = $Output += " Starting VM "
  51. $Output = $Output += $Template
  52. Write $Output >> $Logfile
  53. Start-VM -VM $Template | Get-VMQuestion | Set-VMQuestion -DefaultOption -Confirm:$false >> $Logfile
  54. Start-sleep -s 120
  55. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  56. $Output = $Output += " Complete"
  57. Write $Output >> $Logfile
  58. Write "============================" >> $Logfile
  59. #
  60. # Create variables for Guest OS credentials - This is needed for the Invoke-VMScript cmdlet to be able to execute actions inside the Guest.
  61. $Username = "administrator"
  62. $OSPwd = cat .\OSPwd.txt | convertto-securestring
  63. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $OSPwd
  64. #
  65. # The following is the cmdlet that will invoke the Get-WUInstall inside the GuestVM to install all available Windows updates; optionally results can be exported to a log file to see the patches installed and related results.
  66. Write "============================" >> $Logfile
  67. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  68. $Output = $Output += " Patching VM "
  69. $Output = $Output += $Template
  70. Write $Output >> $Logfile
  71. Invoke-VMScript -ScriptType PowerShell -ScriptText "ipmo PSWindowsUpdate; Get-WUInstall –WindowsUpdate –AcceptAll –AutoReboot" -VM $Template -GuestCredential $Cred >> $Logfile
  72. Start-sleep -s 3600
  73. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  74. $Output = $Output += " Complete"
  75. Write $Output >> $Logfile
  76. Write "============================" >> $Logfile
  77. #
  78. # Optionally restart VMGuest one more time in case Windows Update requires it and for whatever reason the –AutoReboot switch didn’t complete it.
  79. Write "============================" >> $Logfile
  80. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  81. $Output = $Output += " Restarting VM "
  82. $Output = $Output += $Template
  83. Write $Output >> $Logfile
  84. Restart-VMGuest -VM $Template -Confirm:$false >> $Logfile
  85. Start-sleep -s 120
  86. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  87. $Output = $Output += " Complete"
  88. Write $Output >> $Logfile
  89. Write "============================" >> $Logfile
  90. #
  91. # After a desired wait period, Shutdown the server
  92. Write "============================" >> $Logfile
  93. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  94. $Output = $Output += " Shutdown VM "
  95. $Output = $Output += $Template
  96. Write $Output >> $Logfile
  97. Shutdown-VMGuest –VM $Template -Confirm:$false >> $Logfile
  98. Start-sleep -s 120
  99. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  100. $Output = $Output += " Complete"
  101. Write $Output >> $Logfile
  102. Write "============================" >> $Logfile
  103. #
  104. # Convert VM to Template
  105. Write "============================" >> $Logfile
  106. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  107. $Output = $Output += " Convert VM to Template"
  108. $Output = $Output += $Template
  109. Write $Output >> $Logfile
  110. Set-VM –VM $Template -ToTemplate -Confirm:$false >> $Logfile
  111. $Output = (get-date -format 'dd-MMM-yyyy hh:mm')
  112. $Output = $Output += " Complete"
  113. Write $Output >> $Logfile
  114. Write "============================" >> $Logfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement