1+ <#
2+ . SYNOPSIS
3+ Prepares the vDisk to be deployed via MCS.
4+
5+ . DESCRIPTION
6+ Prepares the current vDisk to be deployed via MCS.
7+
8+ . INPUTS
9+ None. You cannot pipe objects to this script.
10+
11+ . EXAMPLE
12+ C:\PS> Start-XenPrep.ps1 -Mode Seal -CleanupProfiles -CleanupEventlogs -Optimize -Shutdown
13+ This examples show how Start-XenPrep can be used to generalize and seal a vDisk when using MCS.
14+
15+ C:\PS> Start-XenPrep.ps1 -Mode Seal -CleanupProfiles -CleanupEventlogs -Optimize -VMware -Appsense -Shutdown
16+ This example show how Start-XenPrep is used in Seal (Rearm) Mode with additional Optimizations for VMware, TrendMicro and Appsense.
17+
18+ . NOTES
19+ Tim Arenz, tarenz@cema.de, cema.de, blog.cema.de
20+ Claus Jan Harms, mail@cjharms.info, cjharms.info, blog.cjharms.info
21+ #>
22+
23+ [CmdletBinding ()]
24+ Param (
25+ [parameter (Mandatory = $true , HelpMessage = " Specifies if this script is used to seal (Seal) or start up (Startup)" )]
26+ [string ][ValidateSet (" Seal" , " Startup" )]$Mode = " Seal" ,
27+
28+ [parameter (Mandatory = $false , HelpMessage = " Clean up Profiles" )]
29+ [Switch ]$CleanupProfiles ,
30+
31+ [parameter (Mandatory = $false , HelpMessage = " Clear Temp Files, Memory Dumps, Windows Installer, etc via cleanmgr.exe" )]
32+ [Switch ]$CleanupWindows ,
33+
34+ [parameter (Mandatory = $false , HelpMessage = " Clear Eventlogs (Application, Security and System)" )]
35+ [Switch ]$CleanupEventlog ,
36+
37+ [parameter (Mandatory = $false , HelpMessage = " Generalize Appsense Agent" )]
38+ [Switch ]$AppSense ,
39+
40+ [parameter (Mandatory = $false , HelpMessage = " Generalize TrendMicro Anti Virus Client" )]
41+ [Switch ]$TrendMicro ,
42+
43+ [parameter (Mandatory = $false , HelpMessage = " Run VMware specific Optimizations" )]
44+ [Switch ]$VMware ,
45+
46+ [parameter (Mandatory = $false , HelpMessage = " Optimize vDisk and Disk Space" )]
47+ [Switch ]$Optimize ,
48+
49+ [parameter (Mandatory = $false , HelpMessage = " Forces the Script to run with all the First Run Actions" )]
50+ [Switch ]$ForceFirstRun ,
51+
52+ [parameter (Mandatory = $false , HelpMessage = " Shuts down the System after running the Script" )]
53+ [Switch ]$Shutdown ,
54+
55+ [parameter (Mandatory = $false , HelpMessage = " Runs this Script silent and without any User Interaction" )]
56+ [Switch ]$Silent
57+
58+ )
59+
60+ $ScriptFolder = Split-Path - Parent $MyInvocation.MyCommand.Path
61+ $AddonFolder = " $ScriptFolder \Addons"
62+ $ErrorActionPreference = " Stop"
63+
64+ Clear-Host
65+ Write-Host " ------------------------------------------------------------------------------"
66+ Write-Host " -- XenPrep Script"
67+ Write-Host " -- Original Development by Tim Arenz, tarenz@cema.de, cema.de, blog.cema.de"
68+ Write-Host " -- Changes by Claus Jan Harms, mail@cjharms.info, cjharms.info"
69+ Write-Host " ------------------------------------------------------------------------------"
70+
71+ # ##
72+ # ## Functions
73+ # ##
74+
75+ function Test-Admin {
76+ $currentUser = New-Object Security.Principal.WindowsPrincipal $ ([Security.Principal.WindowsIdentity ]::GetCurrent())
77+ $currentUser.IsInRole ([Security.Principal.WindowsBuiltinRole ]::Administrator)
78+ }
79+
80+ # ##
81+ # ## Pre check
82+ # ##
83+
84+ # Admin User?
85+ If ((Test-Admin ) -eq $false ) {
86+ Write-Host " "
87+ Write-Warning " You do not have the needed administrative rights to run this script!"
88+ Write-Warning " Please re-run this script as an administrator or in an elevated session."
89+ Write-Host " "
90+ Break
91+ }
92+
93+ # Check Bitness
94+ Write-Host " Checking operating system bitness..."
95+ If (((Get-WmiObject - Class Win32_ComputerSystem).SystemType) -match " x64" ) {
96+ $Bitness = " x64"
97+ $ProgramFiles = ${env: ProgramFiles(X86)}
98+ $ProgramFiles64 = ${env: ProgramFiles}
99+ } Else {
100+ $Bitness = " x86"
101+ $ProgramFiles = ${env: ProgramFiles}
102+ }
103+
104+ # Create first run registry key
105+ If ($ForceFirstRun -eq $true ) {
106+ New-Item " HKLM:\SOFTWARE\XenPrep" - Force | Out-Null
107+ New-ItemProperty " HKLM:\SOFTWARE\XenPrep" - Name " FirstRun" - PropertyType " DWord" - Value 0 - Force | Out-Null
108+ }
109+
110+ # Check first run registry key
111+ If (((Get-ItemProperty " HKLM:\SOFTWARE\XenPrep" - ErrorAction SilentlyContinue).FirstRun) -eq " 1" ) {
112+ $FirstRunActions = $false
113+ } Else {
114+ $FirstRunActions = $true
115+ }
116+
117+ # Set first run key
118+ New-Item " HKLM:\SOFTWARE\XenPrep" - Force | Out-Null
119+ New-ItemProperty " HKLM:\SOFTWARE\XenPrep" - Name " FirstRun" - PropertyType " DWord" - Value 1 - Force | Out-Null
120+
121+ # ##
122+ # ## First run actions, proccessed only one time in Seal/Rearm mode.
123+ # ##
124+
125+ If ($Mode -eq " Seal" -and $FirstRunActions -eq $true ) {
126+
127+ # ##
128+ # ## Windows Cleanup Part
129+ # ##
130+
131+ # Create SageRun Set 11 in the cleanmgr Registry Hive. Used by cleanmgr.exe to clean specific Things like old Logs and MemoryDumps...
132+ New-ItemProperty - Path ' HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' - Name StateFlags0011 - Value 2 - PropertyType DWord - Force | Out-Null
133+ # Delete specific SageRun Set 11 Flags for Windows Update Cleanup because WU Cleanup requires a restart to complete the Cleanup. WU Cleanup should be done manually for now.
134+ Remove-ItemProperty - Path ' HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' - Name StateFlags0011 - ErrorAction SilentlyContinue
135+
136+ }
137+
138+ # ##
139+ # ## General actions, processed in Startup and Seal/Rearm mode
140+ # ##
141+ If ($Mode -eq " Startup" -or $Mode -eq " Seal" ) {
142+ # Time Sync
143+ Write-Host " Syncing time..."
144+ Start-Process " w32tm.exe" - ArgumentList " /config /update" - Wait - WindowStyle Minimized
145+ Start-Process " w32tm.exe" - ArgumentList " /resync" - Wait - WindowStyle Minimized
146+
147+ # Group Policy Update
148+ Write-Host " Updating group policy..."
149+ Start-Process " cmd.exe" - ArgumentList " /C echo n | gpupdate.exe /target:computer" - Wait - WindowStyle Minimized
150+
151+ }
152+
153+ # ##
154+ # ## Custom shut down actions, proccessed only in Seal/Rearm mode
155+ # ##
156+
157+ If ($Mode -eq " Seal" ) {
158+
159+ # Put Actions here!
160+
161+ }
162+
163+ # ##
164+ # ## Shut down actions, proccessed only in Seal/Rearm mode
165+ # ##
166+ If ($Mode -eq " Seal" ) {
167+
168+ # Delete cached profiles
169+ If ($CleanupProfiles -eq $true ) {
170+ Write-Host " Cleaning up cached profiles..."
171+ If ((Test-Path " $AddonFolder \DelProf2\delprof2.exe" ) -eq $false ) {
172+ Write-Host " "
173+ Write-Warning " Profile clean up failed!"
174+ Write-Warning " delprof2.exe couldn't be found."
175+ Write-Host " "
176+ } Else {
177+ Start-Process - FilePath " $AddonFolder \DelProf2\delprof2.exe" - ArgumentList " /u /i" - Wait - WindowStyle Minimized
178+ }
179+ }
180+
181+ # Delete Temp Files, Windows installers, Memory Dumps and much more via Cleanup Manager (cleanmgr.exe)
182+ If ($CleanupWindows -eq $true ) {
183+ Write-Host " Cleaning up Temp Files..."
184+ If ((Get-Command " cleanmgr.exe" - ErrorAction SilentlyContinue) -eq $null ) {
185+ Write-Host " "
186+ Write-Warning " Windows Cleanup failed!"
187+ Write-Warning " cleanmgr.exe couldn't be found."
188+ Write-Host " "
189+ } Else {
190+ Start-Process - FilePath " cleanmgr.exe" - ArgumentList " /sagerun:11" - Wait - WindowStyle Minimized
191+ }
192+ }
193+
194+ # Generalize AppSense CCA and EM
195+ If ($AppSense -eq $true ) {
196+ Write-Host " Generalizing AppSense components..."
197+ Get-Service - Name " AppSense Client Communications Agent" - ErrorAction SilentlyContinue | Out-Null
198+ If ($? ) {
199+ Set-ItemProperty - Path " HKLM:\Software\AppSense Technologies\Communications Agent" - Name " Machine ID" - Value " "
200+ Set-ItemProperty - Path " HKLM:\Software\AppSense Technologies\Communications Agent" - Name " Group ID" - Value " "
201+ Get-ChildItem - Path " C:\appsensevirtual" - Recurse | Remove-Item - Force
202+ } Else {
203+ Write-Host " "
204+ Write-Warning " AppSense generalization failed!"
205+ Write-Warning " AppSense components couldn't be found."
206+ Write-Host " "
207+ }
208+ }
209+
210+ # Generalize TrendMicro OfficeScan
211+ If ($TrendMicro -eq $true ) {
212+ Write-Host " Generalizing TrendMicro Anti Virus..."
213+
214+ # Workaround: Da TrendMicro die TCacheGen Dateien nach der Generalisierung löscht, kopieren wir sie hier zuerst vom Addon Folder in das TrendMicro Verzeichnis
215+ # Stand: Office Scan 10.6 SP3 - 11.02.2016
216+ Copy-Item - Path " $AddonFolder \TrendMicro\TCacheGen\TCacheGen*.exe" - Destination " $ProgramFiles \Trend Micro\OfficeScan Client\" - Force - ErrorAction SilentlyContinue
217+ # Workaround End
218+
219+ If ((Test-Path " $ProgramFiles \Trend Micro\OfficeScan Client\TCacheGenCli_x64.exe" ) -eq $false ) {
220+ Write-Host " "
221+ Write-Warning " TrendMicro generalization failed!"
222+ Write-Warning " TrendMicro Tools for generalization couldn't be found."
223+ Write-Host " "
224+ } Else {
225+ If ($Bitness -eq " x64" ) {
226+ Start-Process - FilePath " $ProgramFiles \Trend Micro\OfficeScan Client\TCacheGenCli_x64.exe" - ArgumentList " Remove_GUID" - Wait - WindowStyle Minimized
227+ } Else {
228+ Start-Process - FilePath " $ProgramFiles \Trend Micro\OfficeScan Client\TCacheGenCli.exe" - ArgumentList " Remove_GUID" - Wait - WindowStyle Minimized
229+ }
230+ }
231+ }
232+
233+ # Delete VMware Tools Status Tray Icons
234+ If ($Optimize -eq $true -and $VMware -eq $true ) {
235+ Write-Host " Disabling VMware Tools Status Tray..."
236+ # Deleting VMware Tools Status Tray Icons
237+ Remove-ItemProperty - Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run - Name " VMware Tools" - Force - ErrorAction SilentlyContinue
238+ Remove-ItemProperty - Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run - Name " VMware User Process" - Force - ErrorAction SilentlyContinue
239+ }
240+
241+ # Clear event log
242+ If ($CleanupEventlog -eq $true ) {
243+ Write-Host " Clearing event logs..."
244+ Clear-EventLog - LogName Application
245+ Clear-EventLog - LogName Security
246+ Clear-EventLog - LogName System
247+ }
248+
249+ # Optimize target device
250+ If ($Optimize -eq $true ) {
251+ Write-Host " Optimizing target device..."
252+ If ((Test-Path " $ProgramFiles64 \Citrix\PvsVm\TargetOSOptimizer\TargetOSOptimizer.exe" ) -eq $false ) {
253+ Write-Host " "
254+ Write-Warning " Citrix Target Optimization failed!"
255+ Write-Warning " Citrix Target Optimizer couldn't be found."
256+ Write-Host " "
257+ } Else {
258+ If ($Bitness -eq " x64" ) {
259+ Start-Process - FilePath " $ProgramFiles64 \Citrix\PvsVm\TargetOSOptimizer\TargetOSOptimizer.exe" - ArgumentList " /silent" - Wait - WindowStyle Minimized
260+ } Else {
261+ Start-Process - FilePath " $ProgramFiles \Citrix\PvsVm\TargetOSOptimizer\TargetOSOptimizer.exe" - ArgumentList " /silent" - Wait - WindowStyle Minimized
262+ }
263+ }
264+ }
265+
266+ # Flush DNS cache
267+ Write-Host " Flushing DNS cache..."
268+ Start-Process - FilePath " ipconfig.exe" - ArgumentList " /flushdns" - Wait - WindowStyle Minimized
269+
270+ # Reclaim Space on vDisk/Harddisk
271+ If ($Optimize -eq $true ) {
272+ Write-Host " Reclaiming Disk Space..."
273+ If ((Test-Path " $AddonFolder \sdelete\sdelete.exe" ) -eq $false ) {
274+ Write-Host " "
275+ Write-Warning " Space Reclamation failed!"
276+ Write-Warning " sdelete.exe couldn't be found."
277+ Write-Host " "
278+ } Else {
279+ Start-Process - FilePath " $AddonFolder \sdelete\sdelete.exe" - ArgumentList " /accepteula -q -z C:" - Wait - WindowStyle Minimized
280+ }
281+ }
282+ }
283+
284+ # ##
285+ # ## Custom start up actions, proccessed only in startup mode
286+ # ##
287+
288+ # If ($Mode -eq "Startup") {
289+ #
290+ # }
291+
292+ # ##
293+ # ## Start up actions, proccessed only in startup mode
294+ # ##
295+
296+ # If ($Mode -eq "Startup") {
297+ #
298+ # }
299+
300+ # ##
301+ # ## Shutdown task
302+ # ##
303+ If ($Mode -eq " Seal" -and $Shutdown -eq $true ) {
304+ Write-Host " Shutting down computer..."
305+ If ($Silent -eq $true ) {
306+ Stop-Computer
307+ } Else {
308+ Stop-Computer - Confirm
309+ }
310+ }
0 commit comments