Guest User

Untitled

a guest
Oct 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.46 KB | None | 0 0
  1. # Shutdown Script to Prepare Master Image for Recompose or Image Push
  2. # Chris Hildebrandt
  3. # 10-18-2018
  4. # Ver 1.5
  5. # Script will Run Disk Cleanup, Defrag, Run SEP scan force update, Run Optimizer, and shutdown services, clear DNS and IP, and Shutdown VM
  6.  
  7. #______________________________________________________________________________________
  8. # Check Version of OS
  9. $winVersion = "0"
  10. $OSVersion = [System.Environment]::OSVersion.Version
  11. if ($OSversion.Major -eq "10" -and $OSversion.Minor -eq "0"){ $winVersion = "10" }
  12. elseif ($OSversion.Major -eq "6" -and $OSversion.Minor -eq "2") { $winVersion = "8" }
  13. elseif ($OSversion.Major -eq "6" -and $OSversion.Minor -eq "1") { $winVersion = "7" }
  14. Write-host "Windows Version is $winVersion"
  15.  
  16. #______________________________________________________________________________________
  17. # Varibles
  18. $ScriptDate = Get-Date -Format MM-dd-yyyy
  19. $FQDN = (Get-WmiObject win32_computersystem).DNSHostName+"."+(Get-WmiObject win32_computersystem).Domain
  20. $LoginFile = "C:\VDI_Tools\Logs\$ScriptDate\$FQDN\ShutdownScript_$ScriptDate.txt"
  21. $defragbat = "C:\VDI_Tools\Scripts\dfrag.bat" #Temporary Location where it saves the Defrag.bat file.
  22.  
  23. #______________________________________________________________________________________
  24. # Optimizer Varibles
  25. $RunOptimizer = 1 # To run Optimizer enter 1, If you do not want to run enter 0
  26. $OptimizerLocation = "C:\VDI_Tools\CloneTools\VMwareOSOptimizationTool"
  27. $OptimizerTemplate = "C:\VDI_Tools\CloneTools\ARDx_Windows${winVersion}.xml"
  28. $OptimizerLogLocation = "C:\VDI_Tools\Logs\$ScriptDate\$FQDN\"
  29. $OptimizerErrorLogLocation = "C:\VDI_Tools\Logs\$ScriptDate\$FQDN\OptimizationErrorLog.txt"
  30. $OptimizerbatFile = "C:\VDI_Tools\Scripts\Optimizer.$fqdn.bat" #Temporary Location where it saves the Optimizer.bat file.
  31. $RunSEP = 1 # To run Optimizer enter 1, If you do not want to run enter 0
  32.  
  33. #______________________________________________________________________________________
  34. # Start the Debug Logging
  35. $ErrorActionPreference="SilentlyContinue"
  36. Stop-Transcript | out-null
  37. $ErrorActionPreference = "Continue"
  38. Start-Transcript -path $LoginFile
  39.  
  40. Write-host "Windows Version is $winVersion"
  41.  
  42. #______________________________________________________________________________________
  43. # Define functions
  44.  
  45. #______________________________________________________________________________________
  46. # Start Services Function
  47. function Start-VDIservices ($SVCname) {
  48. Set-Service -Name $svcname -StartupType Automatic
  49. Start-Service -Name $svcname
  50. Do {
  51. $svc = Get-Service -Name $svcname
  52. Start-Sleep 2
  53. } While ( $svc.Status -ne "Running" )
  54. }
  55.  
  56. #______________________________________________________________________________________
  57. # Stop Services Function
  58. function Stop-VDIservices ($SVCname) {
  59. Stop-Service -Name $svcname
  60. Do {
  61. $svc = Get-Service -Name $svcname
  62. Start-Sleep 2
  63. } While ( $svc.Status -ne "Stopped" )
  64. Set-Service -Name $svcname -StartupType disabled
  65. }
  66.  
  67. #______________________________________________________________________________________
  68. # Do while proc Like Function
  69. function DoWhile-LikeProcName ($Process) {
  70. Do
  71. {
  72. "Processes $Process is still running"
  73. $proc = Get-Process
  74. start-sleep 10
  75. } While ($proc.name -like "*$Process*")
  76. }
  77.  
  78. #______________________________________________________________________________________
  79. # Do while Proc Equals Function
  80. function DoWhile-EQProcName ($Process) {
  81. Do
  82. {
  83. "Processes $Process is still running"
  84. $proc = Get-Process
  85. start-sleep 10
  86. } While ($proc.name -eq "$Process")
  87. }
  88.  
  89. #______________________________________________________________________________________
  90. # Run Disk Cleanup to remove temp files, empty recycle bin and remove other unneeded files
  91. Start-Process -Filepath "c:\windows\system32\cleanmgr" -argumentlist '/sagerun:1'
  92. Write-host "Running Disk Cleanup"
  93.  
  94. #______________________________________________________________________________________
  95. # Check status of Disk Cleanup
  96. DoWhile-LikeProcName cleanmgr
  97.  
  98. #______________________________________________________________________________________
  99. # Check status of Dfrag Service if stopped start.
  100. $DfragStatus = get-service -name defragsvc | Select-Object Name,Status,Starttype
  101. if($DfragStatus.Status -eq 'Stopped')
  102. {
  103. #______________________________________________________________________________________
  104. # Start Defrag Service
  105. Start-VDIservices defragsvc
  106. Write-host "Start Defrag Service"
  107. }
  108.  
  109. #______________________________________________________________________________________
  110. # Create Temp Defrag BAT
  111. "defrag c: /U /V" | Set-Content $defragbat
  112. Write-host "Create Defrag Temp .bat file"
  113.  
  114. #______________________________________________________________________________________
  115. # Start Defrag BAT
  116. Start-Process $defragbat
  117. Write-host "Running Defrag Cleanup"
  118.  
  119. #______________________________________________________________________________________
  120. # Check status of Disk Defrag
  121. DoWhile-EQProcName Defrag
  122.  
  123. #______________________________________________________________________________________
  124. # Remove Temp Defrag BAT
  125. Remove-Item -Path $defragbat
  126. Write-host "Delete Defrag Temp .bat file"
  127.  
  128. #______________________________________________________________________________________
  129. # Stop Defrag Service
  130. Stop-VDIservices defragsvc
  131. Write-host "Stop Defrag Service"
  132.  
  133. #______________________________________________________________________________________
  134. # Pre-complile .NET framework Assemblies
  135. Start-Process -Filepath "C:\Windows\Microsoft.NET\Framework\v4.0.30319\ngen.exe" -argumentlist 'update','/force'
  136. New-ItemProperty -Name verbosestatus -Path "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System" -Force -PropertyType DWORD -Value "1"
  137. Write-host "Running a Pre-Compile of .NET Framework"
  138.  
  139. #______________________________________________________________________________________
  140. # Check status of the .NET recompile
  141. DoWhile-LikeProcName ngen
  142.  
  143.  
  144. #______________________________________________________________________________________
  145. # Run SEP update and Cleanup
  146. if($RunSEP -eq '1')
  147. {
  148. Write-host "Starting the SEP Update and Cleanup Process"
  149. #______________________________________________________________________________________
  150. # Check to see if SEP is installed and prep for Recompose
  151. #______________________________________________________________________________________
  152. # If Check for SEP install
  153. $CheckSEP = get-service -name SepMasterService | Select-Object Name,Status,Starttype
  154. if($CheckSEP.Name -eq 'SepMasterService')
  155. {
  156.  
  157. #______________________________________________________________________________________
  158. # Force SEP checking and Update
  159. Start-Process -Filepath "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\SepLiveUpdate.exe"
  160. Write-host "Liveupdate SEP Config"
  161.  
  162. #______________________________________________________________________________________
  163. # Check status of SEP Update
  164. DoWhile-LikeProcName SepLiveUpdate
  165.  
  166. #______________________________________________________________________________________
  167. # Run full SEP System scan
  168. Start-Process -Filepath "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\doScan.exe" -argumentlist '/scanname "Full System Scan"'
  169. Write-host "Run Full System SEP Scan"
  170.  
  171. #______________________________________________________________________________________
  172. # Check status of SEP Scan
  173. DoWhile-LikeProcName doscan
  174.  
  175. #______________________________________________________________________________________
  176. # Force SEP checking and Update
  177. Start-Process -Filepath "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\smc.exe" -argumentlist '-updateconfig'
  178. Write-host "Update SEP and force Checkin to report the recent completed Scan"
  179.  
  180. #______________________________________________________________________________________
  181. # Check status of SEP Update
  182. DoWhile-LikeProcName doscan
  183.  
  184. #______________________________________________________________________________________
  185. # Check status of Symantec Endpoint Protection Service.
  186. $SEPStatus = get-service -name SepMasterService | Select-Object Name,Status,Starttype
  187. if($SEPStatus.Status -eq 'Running')
  188. {
  189. #______________________________________________________________________________________
  190. # Stop SEP Service and Prep for Clone
  191. Start-Process -Filepath "C:\Program Files (x86)\Symantec\Symantec Endpoint Protection\smc.exe" -argumentlist '-stop'
  192. Write-host "Stopped SEP Service"
  193. }
  194.  
  195. $filesToClean = @("C:\sephwid.xml","C:\communicator.dat","C:\Program Files\Common Files\Symantec Shared\HWID\sephwid.xml","C:\Program Files\Common Files\Symantec Shared\HWID\communicator.dat", "C:\Windows\Temp\sephwid.xml","C:\Windows\Temp\communicator.dat","C:\Documents and Settings\*\Local Settings\Temp\sephwid.xml","CC:\Documents and Settings\*\Local Settings\Temp\communicator.dat","C:\Users\*\AppData\Local\Temp\sephwid.xml","C:\Users\*\AppData\Local\Temp\communicator.dat")
  196.  
  197. foreach($file in $filesToClean)
  198. {
  199. if(Test-Path $file) {Remove-Item -Path $file -Recurse -force}
  200. Write-Host "Removed File $File"
  201. }
  202.  
  203. #______________________________________________________________________________________
  204. # Remove SEP Reg Keys
  205.  
  206. #______________________________________________________________________________________
  207. # Remove 32bit SEP Reg Keys
  208. if (Test-Path -Path "HKLM:\SOFTWARE\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink")
  209. {
  210. #Remove-ItemProperty -Path "HKLM:\SOFTWARE\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink" -Name "ForceHardwareKey"
  211. Remove-ItemProperty -Path "HKLM:\SOFTWARE\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink" -Name "HardwareID"
  212. Remove-ItemProperty -Path "HKLM:\SOFTWARE\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink" -Name "HostGUID"
  213. Write-host "Removed SEP 32 Bit Reg Keys"
  214. }
  215. if (Test-Path -Path "HKLM:\SOFTWARE\WOW6432Node\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink")
  216. {
  217. #Remove-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink" -Name "ForceHardwareKey"
  218. Remove-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink" -Name "HardwareID"
  219. Remove-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Symantec\Symantec Endpoint Protection\SMC\SYLINK\SyLink" -Name "HostGUID"
  220. Write-host "Removed SEP 64 Bit Reg Keys"
  221. }
  222.  
  223. }
  224. Write-host "Finished SEP Cleanup and Prep for Cloneing"
  225. }
  226.  
  227. #______________________________________________________________________________________
  228. # Run VMware optimizer
  229. if($RunOptimizer -eq '1')
  230. {
  231. Write-host "Prepairing to run VMware Optimizer"
  232.  
  233. #______________________________________________________________________________________
  234. # Create Temp Bat file to optimize OS
  235. @"
  236. start /wait $OptimizerLocation -t $OptimizerTemplate -r $OptimizerLogLocation
  237. start /wait $OptimizerLocation -o recommended -t $OptimizerTemplate -v > $OptimizerErrorLogLocation 2>&1
  238. start /wait $OptimizerLocation -t $OptimizerTemplate -r $OptimizerLogLocation
  239. "@ | Set-Content $OptimizerbatFile
  240.  
  241. Write-host "Built Temporary VMware Optimizer .BAT file based on Template $OptimizerTemplate"
  242. #______________________________________________________________________________________
  243. # Run BAT file to optimize OS
  244. Start-Process $OptimizerbatFile
  245. Write-host "Running VMware Optimizer .BAT based on Template $OptimizerTemplate"
  246.  
  247. #______________________________________________________________________________________
  248. # Check status of the Optimize and pause
  249. DoWhile-EQProcName CMD
  250.  
  251. #______________________________________________________________________________________
  252. # Remove Temp BAT file
  253. Remove-Item -Path $OptimizerbatFile
  254. Write-host "Removing Temporary VMware Optimizer .BAT file"
  255.  
  256. }
  257. #______________________________________________________________________________________
  258. # Check if VM is managed by SCCM
  259. $CheckSCCM = get-wmiobject win32_Service | Where-Object {$_.Name -eq "CCMexec"} | Select-Object Name
  260. Write-host "Check if SCCM service is installed"
  261.  
  262. #______________________________________________________________________________________
  263. # If Else statement based on if VM is managed by SCCM
  264. if($CheckSCCM.Name -eq 'CCMexec')
  265. {
  266. Write-host "VM is managed by SCCM"
  267. #______________________________________________________________________________________
  268. # Check status of SCCM Service.
  269. $SCCMStatus = get-service -name "CCMexec" | Select-Object Status
  270. Write-host "Check if SCCM service CCMexec is running"
  271. if($SCCMStatus.Status -eq 'Running')
  272. {
  273. Stop-VDIservices CCMexec
  274. Write-host "Stop SCCM Service CCMexec"
  275. }
  276.  
  277. #______________________________________________________________________________________
  278. # Check status of SCCM Service if stopped start.
  279. $AdaptivaStatus = get-service -name "adaptivaclient" | Select-Object Status
  280. Write-host "Check if Adaptiva Client is installed"
  281. if($AdaptivaStatus.Status -eq 'Running')
  282. {
  283. Stop-VDIservices adaptivaclient
  284. Write-host "Stop Adaptiva Services"
  285. }
  286. }
  287.  
  288. #______________________________________________________________________________________
  289. # Check status of Windows Update Service.
  290. $WindowsUpdatateStatus = get-service -name wuauserv | Select-Object Name,Status,Starttype
  291. if($WindowsUpdatateStatus.Status -eq 'Running')
  292. {
  293. #______________________________________________________________________________________
  294. # Stop Windows Update Service
  295. Stop-VDIservices wuauserv
  296. Write-host "Stop Windows Update Services"
  297. }
  298.  
  299. #______________________________________________________________________________________
  300. # Clean out widows downloads cache
  301. if (Test-Path "C:\Windows\SoftwareDistribution\Download\")
  302. {
  303. Remove-Item -Path "C:\Windows\SoftwareDistribution\Download\*" -force -Recurse
  304. Write-host "Clean Out Windows Update Download Cache"
  305. }
  306.  
  307. #______________________________________________________________________________________
  308. # Check status of VSS Service if stopped start.
  309. $DfragStatus = get-service -name VSS | Select-Object Name,Status,Starttype
  310. if($DfragStatus.Status -eq 'Running')
  311. {
  312. #______________________________________________________________________________________
  313. # Clean out Volume Shadow Copys
  314. .\vssadmin.exe delete shadows /all
  315. Write-host "Clean out Volume Shadow Copys"
  316. }
  317.  
  318. #______________________________________________________________________________________
  319. # Clean out Windows PreFetch
  320. if (Test-Path "c:\Windows\Prefetch\")
  321. {
  322. Remove-item -Path "c:\Windows\Prefetch\*.*" -Force
  323. Write-host "Remove windows Prefetch files"
  324. }
  325.  
  326. #______________________________________________________________________________________
  327. # Check status if AppVolumes is installed and stop the service.
  328. $AppVStatus = get-service | Select-Object Name,Status,Starttype
  329. if($AppVStatus.Name -eq 'svservice')
  330. {
  331. Stop-Service -Name svservice
  332. Write-host "Stop AppVolumes Service"
  333. }
  334.  
  335. #______________________________________________________________________________________
  336. # Clear all event logs
  337. wevtutil el | Foreach-Object {wevtutil cl "$_"}
  338. Write-host "Clear Windows Event Logs"
  339.  
  340. #______________________________________________________________________________________
  341. # Stop Logging
  342. Stop-Transcript
  343.  
  344. #______________________________________________________________________________________
  345. # Release IP, Flush DNS and Shutdown VM
  346. ipconfig /release
  347. ipconfig /flushdns
  348. shutdown -s
Add Comment
Please, Sign In to add comment