· 9 years ago · Jan 13, 2017, 05:03 PM
1[CmdletBinding(SupportsShouldProcess = $true)]
2param(
3)
4 <#
5 .Synopsis
6 prepare Base Image System
7 .Description
8 .EXAMPLE
9 .Inputs
10 .Outputs
11 .NOTES
12 Author: Matthias Schlimm
13 Editor: Mike Bijl (Rewritten variable names and script format)
14 Company: Login Consultants Germany GmbH
15
16 Date: 27.09.2012
17
18 History
19 Last Change: 27.09.2012 MS: Script created
20 Last Change: 10.10.2012 MS: removed slmgr.vbs /rearm - write it into extra script
21 Last Change: 17.10.2012 MS: remove chkdsk /F - beacuse it would like to run at next start
22 Last Change: 04.02.2013 MS: added Get-ChildItem -Path $CTX_SYS32_CACHE_PATH -filter * -Recurse | foreach ($_) {remove-item $_.fullname}
23 Last Change: 18.09.2013 MS: replace $date with $(Get-date) to get current timestamp at running scriptlines write to the logfile
24 Last Change: 18.09.2013 MS: Add Startup sceduled task to personalize system
25 Last Change: 17.12.2013 MS: ARP Cache Changes – Windows 2008 / Vista / Windows 7, see http://support.citrix.com/article/ctx127549 for further details"
26 Last Change: 28.01.2014 MS: add ErrorAction to "Remove-Item -Path '$CTX_SYS32_CACHE_PATH' -Recurse -ErrorAction SilentlyContinue"
27 Last Change: 28.01.2014 MS: remove ipconfig /release
28 Last Change: 28.01.2014 MS: Change executionpoliy unrestricted for Line 36
29 Last Change: 11.03.2014 MS: Read language specified adapter name to support mui installations for each customer, thanks to Benny Ruoff
30 Last Change: 18.03.2014 BR: revisited Script
31 Last Change: 01.04.2014 MS: add array to remove WindowsUpdateInformations
32 Last Change: 02.04.2014 MS: [array]$PreMSG = "N" #<<-- display a messagebox to perform these step, set Y = YES or N = NO
33 Last Change: 02.04.2014 MS: add Question to run Defrag on Systemdisk
34 Last Change: 02.04.2014 MS: add Question to run Sysinternals SDelete to zero out empty vDisk areas and reduce storage
35 Last Change: 13.05.2014 MS: add Multihomed Support to read adaptername from each network, see line 80
36 Last Change: 13.05.2014 MS: add PreCLI commands for silent action
37 Last Change: 15.05.2014 MS: change console output to get-adaptername, line 91 -> Write-BISFLog -Msg "Read AdapterName: $element"
38 Last Change: 13.08.2014 MS: remove $logfile = Set-logFile, it would be used in the 10_XX_LIB_Config.ps1 Script only
39 Last Change: 14.08.2014 MS: remove CLI-Command to show on console, send to logfile only
40 Last Change: 13.10.2014 MS: check WSUS Client-Side-Targeting to delete ClientID or set Service to manual
41 Last Change: 09.02.2015 MS: use NimbleFastReclaim instead of sdelete
42 Last Change: 09.02.2015 JP/MS: Clear the Windows event logs
43 Last Change: 09.02.2015 JP/MS: Add Question to run CCleaner
44 Last Change: 12.12.2015 MS: add question to run reset perfomance Counters
45 Last Change: 14.04.2015 MS: remove defrag, now it performed on the vDisk in the POST Script
46 Last Change: 18.05.2015 MS: add CLI Switch VERYSILENT handling
47 Last Change: 20.05.2015 MS: feature 45 - W2012 R2 only - fix No remote Desktop Licence Server availible on RD Session Host server 2012
48 Last Change: 08.06.2015 MS: Executing all queued .NET compilation jobs - Precompiling assemblies with Ngen.exe can improve the startup time for some applications.
49 Last Change: 21.07.2015 BR: edited Path $Dir_SwDistriPath, attended "Download"
50 Last Change: 10.08.2015 MS: Bug 62 - add new function Write-ZeroesToFreeSpace instead of NimbleFastReclaim -> buggy on W2012R2
51 Last Change: 11.08.2015 BR: Disable Windows Update Service
52 Last Change: 11.08.2015 MS: buggy code in line 98 -> $NgenPath = Get-ChildItem -Path 'c:\windows\Microsoft.NET' -Recurse "ngen.exe" | % {$_.FullName}
53 Last Change: 01.10.2015 MS: rewritten script to use central BISF function
54 Last Change: 04.11.2015 MS: add CLI switch -delAllUsersStartmenu to delete all Objects in C:\ProgramData\Microsoft\Windows\Start Menu\*
55 Last Change: 25.11.2015 MS: Stop DHCP client Service, see https://www.citrix.com/blogs/2015/09/29/pvs-target-devices-the-blue-screen-of-death-rest-easy-we-can-fix-that/
56 Last Change: 25.11.2015 MS: add clear DHCP entries of Networkadapter, to prevent BlueScreen on some PVS Targetdevices https://www.citrix.com/blogs/2015/09/29/pvs-target-devices-the-blue-screen-of-death-rest-easy-we-can-fix-that/
57 Last Change: 25.11.2015 MS: reset Distributed Transaction Coordinator service if installed
58 Last Change: 15.12.2015 MS: Feature 96 - added vmware Tools optimizations, thx to Ingmar Verheij - http://www.ingmarverheij.com
59 Last Change: 16.12.2015 MS: Feature 99 - add Disable Task offload, thx to Ingmar Verheij - http://www.ingmarverheij.com
60 Last Change: 16.12.2015 MS: Feature 99 - add Increases the UDP packet size to 1500 bytes for FastSend - http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2040065, thx to Ingmar Verheij - http://www.ingmarverheij.com
61 Last Change: 16.12.2015 MS: Feature 99 - add Set multiplication factor to the default UDP scavenge value (MaxEndpointCountMult), http://support.microsoft.com/kb/2685007/en-us , thx to Ingmar Verheij - http://www.ingmarverheij.com
62 Last Change: 16.12.2015 MS: Feature 99 - add Disable Receive Side Scaling (RSS), http://support.microsoft.com/kb/951037/en-us , thx to Ingmar Verheij - http://www.ingmarverheij.com
63 Last Change: 16.12.2015 MS: Feature 99 - add Disable IPv6 completely , thx to Ingmar Verheij - http://www.ingmarverheij.com
64 Last Change: 16.12 2015 MS: Feature 97 - add Hide PVS status icon, http://forums.citrix.com/thread.jspa?threadID=273278, , thx to Ingmar Verheij - http://www.ingmarverheij.com
65 Last Change: 16.12.2015 MS: Feature 100 - disable Windows Services, thx to Thomas Krampe
66 Last Change: 16.12.2015 MS: Feature 100 - disable useless Scheduled tasks, thx to Thomas Krampe
67 Last Change: 16.12.2015 MS: Feature 100 - Win8 only, run disk cleanup, thx to Thomas Krampe
68 Last Change: 16.12.2015 MS: Feature 100 - add Disable Data Execution Prevention, Disable Startup Repair option, Disable New Network dialog, Set Power Saving Scheme to High Performance, , thx to Thomas Krampe
69 Last Change: 07.01.2016 MS: Feature 79 - add Optimize-BISFWinSxs to cleanup and reduce WinSxs Folder
70 Last Change: 20.01.2016 MS: Fix for Feature 99: Wrong Dword to completly disable IPv6 - 0x000000FF, thanks to Jonathan Pitre
71 Last Change: 20.01.2016 MS: Fix for delAllUsersStartmenu, typos in variable
72 Last Change: 10.03.2016 MS: Issue 111 - use nvspbind.exe to unbind IPV6 from AdapterGuid
73 Last Change: 10.03.2016 MS: adding Delprof2.exe usage
74 Last Change: 22.03.2016 MS: change sdelete to run on the WriteCacheDisk on PVS Target Devices only
75 Last Change: 24.03.2016 MS: modify BIS-F scheduled task if even exist, thx to Valentino Pemoni
76 Last Change: 10.11.2016 MS: adding Pre-Commands for Windows Server 2016 and Windows 10
77 Last Change: 11.11.2016 MS: Create-BISFTask running in own function
78 Last Change: 05.12.2016 MS: Bugfix: defrag not identify the right driveletter of the vDisk after P2PVS, if the Drivelabel is empty
79 Last Change: 05.12.2016 MS: variables must be cleared after each step, to not store the value in the variable and use them in the next $prepCommand
80 Last Change: 01.12.2017 JP: Add Disk Cleanup for Windows 7 and 2008 R2
81 .Link
82 #>
83
84Begin {
85
86 ####################################################################
87 # define environment
88 # Setting default variables ($PSScriptroot/$logfile/$PSCommand,$PSScriptFullname/$scriptlibrary/LogFileName) independent on running script from console or ISE and the powershell version.
89 If ($($host.name) -like "* ISE *") { # Running script from Windows Powershell ISE
90 $PSScriptFullName = $psise.CurrentFile.FullPath.ToLower()
91 $PSCommand = (Get-PSCallStack).InvocationInfo.MyCommand.Definition
92 } ELSE {
93 $PSScriptFullName = $MyInvocation.MyCommand.Definition.ToLower()
94 $PSCommand = $MyInvocation.Line
95 }
96 [string]$PSScriptName = (Split-Path $PSScriptFullName -leaf).ToLower()
97 If (($PSScriptRoot -eq "") -or ($PSScriptRoot -eq $null)) { [string]$PSScriptRoot = (Split-Path $PSScriptFullName).ToLower()}
98
99 ####################################################################
100 # define environment
101 $PreMSG = @()
102 $PreTXT = @()
103 $PreCMD = @()
104 $PreCLI = @()
105 $CTX_SYS32_CACHE_PATH = "C:\Program Files (x86)\Citrix\System32\Cache\*"
106 $REG_hklm_WSUS = "$hklm_software\Microsoft\Windows\CurrentVersion\WindowsUpdate"
107 $REG_hklm_WSUS_TargetGroup = "$hklm_sw\Policies\Microsoft\Windows\WindowsUpdate"
108 $Dir_SwDistriPath = "C:\Windows\SoftwareDistribution\Download\*"
109 $File_WindowsUpdateLog = "C:\Windows\WindowsUpdate.log"
110 $Dir_AllUsersStartmenu = "C:\ProgramData\Microsoft\Windows\Start Menu\*"
111 $Global:BISFtask="LIC_BISF_Device_Personalize"
112
113 # All commands that are used to prepare for building the vDisk
114 [array]$PrepCommands = @(
115
116 [pscustomobject]@{
117 Order="010";
118 Enabled="$true";
119 showmessage="N";
120 CLI="";
121 TestPath ="";
122 Description="Delete all Citrix Cached files $CTX_SYS32_CACHE_PATH";
123 Command="Remove-Item -Path '$CTX_SYS32_CACHE_PATH' -Recurse -ErrorAction SilentlyContinue"},
124
125 [pscustomobject]@{
126 Order="011";
127 Enabled="$true";
128 showmessage="N";
129 CLI="";
130 TestPath ="";
131 Description="Delete SoftwareDistribution $Dir_SwDistriPath";
132 Command="Remove-Item -Path '$Dir_SwDistriPath' -Recurse -ErrorAction SilentlyContinue"},
133
134 [pscustomobject]@{
135 Order="012";
136 Enabled="$true";
137 showmessage="N";
138 CLI="";
139 TestPath ="";
140 Description="Delete Windows Update Log $File_WindowsUpdateLog";
141 Command="Remove-Item '$File_WindowsUpdateLog' -ErrorAction SilentlyContinue"},
142
143 [pscustomobject]@{
144 Order="013";
145 Enabled="$true";
146 showmessage="Y";
147 CLI="LIC_BISF_CLI_SM";
148 TestPath ="";
149 Description="Delete AllUsers Start Menu $Dir_AllUsersStartmenu";
150 Command="Remove-Item '$Dir_AllUsersStartmenu' -Recurse -Force -ErrorAction SilentlyContinue"},
151
152 [pscustomobject]@{
153 Order="014";
154 Enabled="$true";
155 showmessage="Y";
156 CLI="LIC_BISF_CLI_DP";
157 TestPath ="$($env:SystemRoot)\system32\delprof2.exe";
158 Description="Run Delprof2 to deletes inactive user profiles. ";
159 Command="delprof2.exe /u /r"},
160
161 [pscustomobject]@{
162 Order="020";
163 Enabled="$true";
164 showmessage="N";
165 CLI="";
166 TestPath ="";
167 Description="Purge DNS resolver Cache";
168 Command="ipconfig /flushdns"},
169
170 [pscustomobject]@{
171 Order="021";
172 Enabled="$true";
173 showmessage="N";
174 CLI="";
175 TestPath ="";
176 Description="Purge IP-to-Physical address translation tables Cache (ARP Table)";
177 Command="arp -d *"},
178
179 [pscustomobject]@{
180 Order="022";
181 Enabled="$true";
182 showmessage="Y";
183 CLI="LIC_BISF_CLI_CC";
184 TestPath = "$($env:ProgramFiles)\CCleaner\CCleaner.exe" ;
185 Description="Run CCleaner to clean temp files ?";
186 Command="Start-BISFProcWithProgBar -ProcPath '$($env:ProgramFiles)\CCleaner\CCleaner.exe' -Args '/AUTO' -ActText 'CCleaner is running'"},
187
188 [pscustomobject]@{
189 Order="023";
190 Enabled="$true";
191 showmessage="Y";
192 CLI="LIC_BISF_CLI_PF";
193 TestPath ="";
194 Description="Reset Performance Counters ? See CTX129350 or CTX127151 for further informations";
195 Command="lodctr.exe /r"},
196
197 [pscustomobject]@{
198 Order="024";
199 Enabled="$true";
200 showmessage="N";
201 CLI="";
202 TestPath ="";
203 Description="Clear all event logs";
204 Command="'wevtutil el | Foreach-Object {wevtutil cl $_}'"},
205
206 [pscustomobject]@{
207 Order="026";
208 Enabled="$true";
209 showmessage="N";
210 CLI="";
211 TestPath ="";
212 Description="Disabling TCP/IP task offloading";
213 Command="Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'DisableTaskOffload' -Value '1' -Type DWORD"},
214
215 [pscustomobject]@{
216 Order="027";
217 Enabled="$true";
218 showmessage="N";
219 CLI="";
220 TestPath ="";
221 Description="Increases the UDP packet size to 1500 bytes for FastSend";
222 Command="Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\afd\Parameters' -Name 'FastSendDatagramThreshold' -Value '1500' -Type DWORD"},
223
224 [pscustomobject]@{
225 Order="028";
226 Enabled="$true";
227 showmessage="N";
228 CLI="";
229 TestPath ="";
230 Description="Set multiplication factor to the default UDP scavenge value (MaxEndpointCountMult)";
231 Command="Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\BFE\Parameters' -Name 'MaxEndpointCountMult' -Value '0x10' -Type DWORD"},
232
233 [pscustomobject]@{
234 Order="029";
235 Enabled="$true";
236 showmessage="N";
237 CLI="";
238 TestPath ="";
239 Description="Disable Receive Side Scaling (RSS)";
240 Command="Start-Process -FilePath 'netsh.exe' -Argumentlist 'int tcp set global rss=disable' -Wait -WindowStyle Hidden"},
241
242 [pscustomobject]@{
243 Order="030";
244 Enabled="$true";
245 showmessage="Y";
246 CLI="LIC_BISF_CLI_V6";
247 TestPath ="";
248 Description="Disable IPv6 in Registry";
249 Command="Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\TcpIp6\Parameters' -Name 'DisabledComponents' -Value '0x000000FF' -Type DWORD"},
250
251 [pscustomobject]@{
252 Order="031";
253 Enabled="$true";
254 showmessage="Y";
255 CLI="";
256 TestPath ="";
257 Description="Disable Data Execution Prevention";
258 Command="Start-Process 'bcdedit.exe' -Verb runAs -ArgumentList '/set nx AlwaysOff' | Out-Null"},
259
260 [pscustomobject]@{
261 Order="032";
262 Enabled="$true";
263 showmessage="Y";
264 CLI="";
265 TestPath ="";
266 Description="Disable Startup Repair option";
267 Command="Start-Process 'bcdedit.exe' -Verb runAs -ArgumentList '/set {default} bootstatuspolicy ignoreallfailures' | Out-Null"},
268
269 [pscustomobject]@{
270 Order="033";
271 Enabled="$true";
272 showmessage="Y";
273 CLI="";
274 TestPath ="";
275 Description="Disable New Network dialog";
276 Command="Set-ItemProperty -Name NewNetworkWindowOff -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Network' -Type String -Value 0"},
277
278 [pscustomobject]@{
279 Order="034";
280 Enabled="$true";
281 showmessage="Y";
282 CLI="";
283 TestPath ="";
284 Description="Set Power Saving Scheme to High Performance";
285 Command="Start-Process 'powercfg.exe' -Verb runAs -ArgumentList '-s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c'"}
286
287 )
288 $ordercnt = 100
289
290 # recommended: Running sdelete on PVS WriteCahceDisk on each PVS Target Devices only
291 IF ($returnTestPVSSoftware -eq "true")
292 {
293 IF (($LIC_BISF_CLI_SD -ne $null) -or ($LIC_BISF_CLI_SD -ne "")) {
294 $PrepCommands += [pscustomobject]@{
295 Order="$ordercnt";
296 Enabled="$true";
297 showmessage="N";
298 CLI="";
299 TestPath ="";
300 Description="Reset Registry Value LIC_BISF_sDeleteRun in Registry ?";
301 Command="Set-ItemProperty -Path '$hklm_software_LIC_CTX_BISF_SCRIPTS' -Name 'LIC_BISF_sDeleteRun' -value '$false'"};
302 $ordercnt += 1
303 }
304 $PrepCommands += [pscustomobject]@{
305 Order="$ordercnt";
306 Enabled="$true";
307 showmessage="Y";
308 CLI="LIC_BISF_CLI_SD";
309 TestPath ="C:\Windows\system32\sdelete.exe";
310 Description="Running sdelete to Zero-Out free Space on PVS WriteCacheDisk on each PVS Target Device at System Startup ? Regarding on the EULA you must download sdelete.exe first and save them in C:\Windows\system32";
311 Command="Set-ItemProperty -Path '$hklm_software_LIC_CTX_BISF_SCRIPTS' -Name 'LIC_BISF_sDeleteRun' -value '$true'"};
312 $ordercnt += 1
313 } ELSE {
314 $PrepCommands += [pscustomobject]@{
315 Order="$ordercnt";
316 Enabled="$true";
317 showmessage="N";
318 CLI="";
319 TestPath ="";
320 Description="Reset Registry Value LIC_BISF_sDeleteRun in Registry ?";
321 Command="Set-ItemProperty -Path '$hklm_software_LIC_CTX_BISF_SCRIPTS' -Name 'LIC_BISF_sDeleteRun' -value '$false'"};
322 $ordercnt += 1
323 }
324
325 ### Executing all queued .NET compilation jobs - Precompiling assemblies with Ngen.exe can improve the startup time for some applications.
326 $NgenPath = Get-ChildItem -Path 'c:\windows\Microsoft.NET' -Recurse "ngen.exe" | % {$_.FullName}
327 foreach ($element in $NgenPath) {
328 Write-BISFLog -Msg "Read Ngen Path: $element"
329 $PrepCommands += [pscustomobject]@{
330 Order="$ordercnt";
331 Enabled="$true";
332 showmessage="N";
333 CLI="";
334 TestPath ="";
335 Description="Executing all queued .NET compilation jobs for $element";
336 Command="$element ExecuteQueuedItems" };
337 $ordercnt += 1
338 }
339
340 ## Read language specified adapter name to support mui installations for each customer
341 $adapter = get-BISFAdapterName
342 foreach ($element in $adapter) {
343 Write-BISFLog -Msg "Read AdapterName: $element"
344 $PrepCommands += [pscustomobject]@{
345 Order="$ordercnt";
346 Enabled="$true";
347 showmessage="N";
348 CLI="";
349 TestPath ="";
350 Description="ARP Cache Changes Adapter: $element ... (http://support.citrix.com/article/ctx127549)";
351 Command="netsh interface ipv4 set interface ""$element"" basereachable=600000"};
352 $ordercnt += 1
353
354 }
355
356 ## Read GUID of DHCP Network Adapter and clear DHCP-option, see https://www.citrix.com/blogs/2015/09/29/pvs-target-devices-the-blue-screen-of-death-rest-easy-we-can-fix-that/
357 $adapter = get-BISFAdapterGUID
358 $PrepCommands += [pscustomobject]@{
359 Order="$ordercnt";
360 Enabled="$true";
361 showmessage="N";
362 CLI="";
363 TestPath ="";
364 Description="Stop DHCP Client Service";
365 Command="Stop-Service -Name dhcp -ErrorAction SilentlyContinue"};
366 $ordercnt += 1
367
368 $PrepCommands += [pscustomobject]@{
369 Order="$ordercnt";
370 Enabled="$true";
371 showmessage="N";
372 CLI="";
373 TestPath ="";
374 Description="clear NameServer in Registry TCPIP\Parameters";
375 Command="Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' -Name 'NameServer' -value '' "};
376 $ordercnt += 1
377
378 foreach ($element in $adapter) {
379 Write-BISFLog -Msg "Read AdapterGUID: $element"
380 $REG_HKLM_TCPIP_Interfaces_GUID = "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\$element"
381 $PrepCommands += [pscustomobject]@{
382 Order="$ordercnt";
383 Enabled="$true";
384 showmessage="N";
385 CLI="";
386 TestPath ="";
387 Description="AdapterGUID: $element - clear NameServer";
388 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'NameServer' -value '' "};
389 $ordercnt += 1
390
391 $PrepCommands += [pscustomobject]@{
392 Order="$ordercnt";
393 Enabled="$true";
394 showmessage="N";
395 CLI="";
396 TestPath ="";
397 Description="AdapterGUID: $element - clear Domain";
398 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'Domain' -value '' "};
399 $ordercnt += 1
400
401 $PrepCommands += [pscustomobject]@{
402 Order="$ordercnt";
403 Enabled="$true";
404 showmessage="N";
405 CLI="";
406 TestPath ="";
407 Description="AdapterGUID: $element - clear DhcpIPAddress";
408 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'DHCPIPAddress' -value '' "};
409 $ordercnt += 1
410
411 $PrepCommands += [pscustomobject]@{
412 Order="$ordercnt";
413 Enabled="$true";
414 showmessage="N";
415 CLI="";
416 TestPath ="";
417 Description="AdapterGUID: $element - clear DhcpSubnetmask";
418 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'DhcpSubnetmask' -value '' "};
419 $ordercnt += 1
420
421 $PrepCommands += [pscustomobject]@{
422 Order="$ordercnt";
423 Enabled="$true";
424 showmessage="N";
425 CLI="";
426 TestPath ="";
427 Description="AdapterGUID: $element - clear DhcpServer";
428 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'DhcpServer' -value '' "};
429 $ordercnt += 1
430
431 $PrepCommands += [pscustomobject]@{
432 Order="$ordercnt";
433 Enabled="$true";
434 showmessage="N";
435 CLI="";
436 TestPath ="";
437 Description="AdapterGUID: $element - clear DhcpNameServer";
438 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'DhcpNameServer' -value '' "};
439 $ordercnt += 1
440
441 $PrepCommands += [pscustomobject]@{
442 Order="$ordercnt";
443 Enabled="$true";
444 showmessage="N";
445 CLI="";
446 TestPath ="";
447 Description="AdapterGUID: $element - clear DhcpDefaultGateway";
448 Command="Set-ItemProperty -Path '$REG_HKLM_TCPIP_Interfaces_GUID' -Name 'DhcpDefaultGateway' -value '' "};
449 $ordercnt += 1
450
451 $PrepCommands += [pscustomobject]@{
452 Order="$ordercnt";
453 Enabled="$true";
454 showmessage="Y";
455 CLI="LIC_BISF_CLI_V6";
456 TestPath ="$($env:SystemRoot)\system32\nvspbind.exe";
457 Description="Disable IPv6 on AdapterGUID: $element";
458 Command="nvspbind.exe /d ""$element"" ms_tcpip6"};
459 $ordercnt += 1
460 }
461
462 ## reset Distributed Transaction Coordinator service if installed
463 $svc = Test-BISFService -ServiceName "MSDTC"
464 IF ($svc -eq $true)
465 {
466 $PrepCommands += [pscustomobject]@{
467 Order="$ordercnt";
468 Enabled="$true";
469 showmessage="N";
470 CLI="";
471 TestPath ="";
472 Description="Reset Microsoft Distributed Transaction Service ";
473 Command="msdtc.exe -reset" };
474 $ordercnt += 1
475 }
476
477
478 ## vmware tools optimizations
479 $svc = Test-BISFService -ServiceName "vmtools"
480 IF ($svc -eq $true)
481 {
482 $PrepCommands += [pscustomobject]@{
483 Order="$ordercnt";
484 Enabled="$true";
485 showmessage="N";
486 CLI="";
487 TestPath ="";
488 Description="Hide Vmware Tools Icon in Systray";
489 Command="Set-ItemProperty -Path 'HKLM:\SOFTWARE\VMware, Inc.\VMware Tools' -Name 'ShowTray' -Value '0' -Type DWORD" };
490 $ordercnt += 1
491 }
492
493 $svc = Test-BISFService -ServiceName "vmdebug"
494 IF ($svc -eq $true)
495 {
496 $PrepCommands += [pscustomobject]@{
497 Order="$ordercnt";
498 Enabled="$true";
499 showmessage="N";
500 CLI="";
501 TestPath ="";
502 Description="Disable VMware debug driver";
503 Command="Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\services\vmdebug' -Name 'Start' -Value '4' -Type DWORD" };
504 $ordercnt += 1
505 }
506
507
508 ## hide PVS status icon
509 IF ($returnTestPVSSoftware -eq "true")
510 {
511 $PrepCommands += [pscustomobject]@{
512 Order="$ordercnt";
513 Enabled="$true";
514 showmessage="N";
515 CLI="";
516 TestPath ="";
517 Description="Hide PVS Status Icon in Systray";
518 Command="New-Item -Path 'HKLM:\SOFTWARE\CITRIX\ProvisioningServices\Status' -Force | out-null; Set-ItemProperty -Path 'HKLM:\SOFTWARE\CITRIX\ProvisioningServices\Status' -Name 'ShowIcon' -Value '0' -Type DWORD" };
519 $ordercnt += 1
520
521 }
522
523 ## WSUS client-side targeting
524
525 IF ($val.TargetGroupEnabled -eq 1)
526 {
527 Write-BISFLog -Msg "WSUS client-side targeting detected"
528 $PrepCommands += [pscustomobject]@{
529 Order="$ordercnt";
530 Enabled="$true";
531 showmessage="N";
532 CLI="";
533 TestPath ="";
534 Description="Delete WSUS - SusClientId in $REG_hklm_WSUS";
535 Command="Remove-ItemProperty -Path '$REG_hklm_WSUS' -Name 'SusClientId' -ErrorAction SilentlyContinue"};
536 $ordercnt += 1
537
538 $PrepCommands += [pscustomobject]@{
539 Order="$ordercnt";
540 Enabled="$true";
541 showmessage="N";
542 CLI="";
543 TestPath ="";
544 Description="Delete WSUS - SusClientIdValidation in $REG_hklm_WSUS";
545 Command="Remove-ItemProperty -Path '$REG_hklm_WSUS' -Name 'SusClientIdValidation' -ErrorAction SilentlyContinue"};
546 $ordercnt += 1
547 }
548 $PrepCommands += [pscustomobject]@{
549 Order="$ordercnt";
550 Enabled="$true";
551 showmessage="N";
552 CLI="";
553 TestPath ="";
554 Description="Set Windows Update Service to Disabled";
555 Command="Set-Service -Name wuauserv -StartupType Disabled -ErrorAction SilentlyContinue"};
556 $ordercnt += 1
557
558 ## disable services
559 Write-BISFLog -Msg "Disable Windows Services"
560 $ServicesList = @("Themes,PeerDistSvc,EFS,msiscsi,WSearch,ALG,BITS,BDESVC,wbengine,bthserv,Browser,DeviceAssociationService,DsmSvc,DPS,WdiServiceHost,WdiSystemHost,TrkWks,WPCSvc,Fax,FDResPub,HomeGroupListener,HomeGroupProvider,swprv,netprofm,CscService,defragsvc,SstpSvc,wscsvc,SensrSvc,ShellHWDetection,SNMPTRAP,SSDPSRV,SysMain,TapiSrv,upnphost,VSS,SDRSVC,WcsPlugInService,wcncsvc,WinDefend,WerSvc,WMPNetworkSvc,Wlansvc,WwanSvc")
561 ForEach ($ServiceList in $ServicesList)
562 {
563 $svc = Test-BISFService -ServiceName $ServiceList
564 IF ($svc -eq $true) {
565 $PrepCommands += [pscustomobject]@{
566 Order="$ordercnt";
567 Enabled="$true";
568 showmessage="N";
569 CLI="";
570 TestPath ="";
571 Description="Disable Windows Service $ServiceList ";
572 Command="Invoke-BISFService -ServiceName $ServiceList -Action Stop -StartType Disabled"};
573 $ordercnt += 1
574 }
575 }
576
577 ## disable useless Scheduled tasks
578 IF ($OSVersion -like "6.3*")
579 {
580 Write-BISFLog -Msg "Disable scheduled Tasks"
581 $ScheduledTasksList = @("AitAgent,ProgramDataUpdater,StartupAppTask,Proxy,UninstallDeviceTask,BthSQM,Consolidator,KernelCeipTask,Uploader,UsbCeip,Scheduled,Microsoft-Windows-DiskDiagnosticDataCollector,Microsoft-Windows-DiskDiagnosticResolver,WinSAT,HotStart,AnalyzeSystem,RacTask,MobilityManager,RegIdleBackup,FamilySafetyMonitor,FamilySafetyRefresh,AutoWake,GadgetManager,SessionAgent,SystemDataProviders,UPnPHostConfig,ResolutionHost,BfeOnServiceStartTypeChange,UpdateLibrary,ServerManager")
582 ForEach ($ScheduledTaskList in $ScheduledTasksList)
583 {
584 $task = Get-ScheduledTask -TaskName $ScheduledTaskList -ErrorAction SilentlyContinue
585 IF ($task)
586 {
587 Write-BISFLog -Msg "Scheduled Task $ScheduledTaskList exists"
588 $TaskPathName = Get-ScheduledTask -TaskName $ScheduledTaskList | % {$_.TaskPath}
589 $PrepCommands += [pscustomobject]@{Order="$ordercnt"; Enabled="$true"; showmessage="N"; CLI=""; Description="Disable scheduled Task $ScheduledTaskList "; Command="Disable-ScheduledTask -Taskname $ScheduledTaskList -TaskPath '$TaskPathName' | Out-Null"};$ordercnt += 1
590 } ELSE {
591 Write-BISFLog -Msg "Scheduled Task $ScheduledTaskList NOT exists"
592 }
593 }
594 }
595
596
597
598 ####################################################################
599
600 ####################################################################
601 ####### functions #####
602
603 # Prepare System
604 function PreCommand {
605 Write-BISFLog -Msg "running PreCommands on your Base-Image" -ShowConsole -Color Green
606 Foreach ($prepCommand in ($PrepCommands | Sort-Object -Property "Order")) {
607 Write-BISFLog -Msg "Processing Order-Nbr $($prepCommand.Order): $($prepCommand.Description)"
608 #write-host "TestPath: $($prepCommand.TestPath)" -ForegroundColor White -BackgroundColor Red #<<< enable for debug only
609 IF ( ($prepCommand.TestPath -eq "" ) -or (Test-Path $($prepCommand.TestPath)) )
610 {
611
612 IF ($($prepCommand.TestPath) -ne "" ) {
613 $Productname = (Get-Item $($prepCommand.TestPath)).Basename
614 Write-BISFLog -Msg "Product $Productname installed" -ShowConsole -Color Cyan
615 }
616 Write-BISFLog -Msg "$($prepCommand.Description)" -ShowConsole -Color DarkGreen -SubMsg
617 # write-host "MessageBox: $($prepCommand.showmessage)" -ForegroundColor White -BackgroundColor Red #<<< enable for debug only
618 IF ($($prepCommand.showmessage) -eq "N") {
619 # Write-BISFLog -Msg "$($prepCommand.Command)" -ShowConsole
620 invoke-expression $($prepCommand.Command)
621 } ELSE {
622 Write-BISFLog -Msg "Check Silentswitch..."
623 $varCLI = Get-Variable -Name $($prepCommand.CLI) -ValueOnly
624 If (($varCLI -eq "YES") -or ($varCLI -eq "NO")) {
625 Write-BISFLog -Msg "Silentswitch would be set to $varCLI"
626 } ELSE {
627
628 If ($LIC_BISF_CLI_VS)
629 {
630 Write-BISFLog -Msg "Messagebox would be suppressed from CLI switch ! Please define the CLI switch to run $($prepCommand.Description) and start the script again !" -Type E
631 }
632 Write-BISFLog -Msg "Silentswitch not defined, show MessageBox"
633 $PreMsgBox = Show-BISFMessageBox -Msg "$($prepCommand.Description)" -Title "PRE build Base Image Action" -YesNo -Question
634 Write-BISFLog -Msg "`"$PreMsgBox`" is the response to perform $($prepCommand.Description)... please wait" -ShowConsole -Color DarkGreen -SubMsg
635 }
636 if (($PreMsgBox -eq "YES") -or ($varCLI -eq "YES")) {
637 Write-BISFLog -Msg "Running Command $($PostCommand.Command)"
638 invoke-expression $($prepCommand.Command)
639 }
640 }
641 # these 2 variables must be cleared after each step, to not store the value in the variable and use them in the next $prepCommand
642 $varCLI = @()
643 $PreMsgBox = @()
644 } ELSE {
645 Write-BISFLog -Msg "Product $($prepCommand.TestPath) NOT installed, neccessary for Order-Nbr $($prepCommand.Order): $($prepCommand.Description)" -Type W -SubMsg
646
647 }
648 }
649
650 }
651
652 function Create-BISFTask
653 {
654 # searching for BISF scheduled task and if from different BIS-F version delete them
655
656 $testBISFtask = schtasks /query /v /FO CSV | ConvertFrom-Csv | Where {$_.TaskName -eq "\$BISFtask"}
657 IF (!($testBISFtask))
658 {
659 Write-BISFLog -Msg "Create Startuptask $BISFtask to personalize System" -ShowConsole -Color DarkCyan
660 schtasks.exe /create /sc ONSTART /TN "$BISFtask" /IT /RU 'System' /tr "powershell.exe -Executionpolicy unrestricted -file '$LIC_BISF_MAIN_PersScript'" /f | Out-Null
661
662 } ELSE {
663
664 Write-BISFLog -Msg "Task exist, modify Startuptask $BISFtask to personalize System" -ShowConsole -Color DarkCyan
665 schtasks.exe /change /TN "$BISFtask" /tr "powershell.exe -Executionpolicy unrestricted -file '$LIC_BISF_MAIN_PersScript'" | Out-Null
666 }
667
668 }
669
670 function Test-DrvLabel
671 {
672 $SysDrive = $env:SystemDrive
673 $Sysdrvlabel = gwmi -Class win32_volume -Filter "Driveletter = '$SysDrive' "| % {$_.Label}
674 $DriveLabel = "OSDisk"
675 IF ($Sysdrvlabel -eq $null)
676 {
677 Write-BISFLog -Msg "DriveLabel for $SysDrive would be set to $DriveLabel" -ShowConsole -Color DarkGreen -SubMsg
678 $drive = gwmi win32_volume -Filter "DriveLetter = '$SysDrive'"
679 $drive.Label = "$DriveLabel"
680 $drive.put() | Ou-Null
681 }
682
683
684 }
685
686 function Pre-Win7
687 {
688 Write-BISFLog -Msg "Perform a disk cleanup" -ShowConsole -Color DarkGreen -SubMsg
689 # Perform a disk cleanup (Windows 2008R2 only)
690 # Automate by creating the reg checks corresponding to "cleanmgr /sageset:100" so we can use "sagerun:100"
691 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -Type DWord -Value 0x00000002
692 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -Type DWord -Value 0x00000002
693 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -Type DWord -Value 0x00000002
694 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles' -Type DWord -Value 0x00000002
695 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameStatisticsFiles' -Type DWord -Value 0x00000002
696 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameUpdateFiles' -Type DWord -Value 0x00000002
697 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -Type DWord -Value 0x00000002
698 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files' -Type DWord -Value 0x00000002
699 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files' -Type DWord -Value 0x00000002
700 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -Type DWord -Value 0x00000002
701 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -Type DWord -Value 0x00000002
702 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -Type DWord -Value 0x00000002
703 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup' -Type DWord -Value 0x00000002
704 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -Type DWord -Value 0x00000002
705 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -Type DWord -Value 0x00000002
706 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -Type DWord -Value 0x00000002
707 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Type DWord -Value 0x00000002
708 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -Type DWord -Value 0x00000002
709 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Sync Files' -Type DWord -Value 0x00000002
710 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -Type DWord -Value 0x00000002
711 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Type DWord -Value 0x00000002
712 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files' -Type DWord -Value 0x00000002
713 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions' -Type DWord -Value 0x00000002
714 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender' -Type DWord -Value 0x00000002
715 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files' -Type DWord -Value 0x00000002
716 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files' -Type DWord -Value 0x00000002
717 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files' -Type DWord -Value 0x00000002
718 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files' -Type DWord -Value 0x00000002
719 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files' -Type DWord -Value 0x00000002
720 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files' -Type DWord -Value 0x00000002
721 Start-Process 'cleanmgr.exe' -Verb runAs -ArgumentList '/sagerun:100 | out-Null' -Wait
722 Show-BISFProgressBar -CheckProcess "cleanmgr" -ActivityText "Running Disk Cleanup..."
723
724 }
725
726 function Pre-Win2008R2
727 {
728 Write-BISFLog -Msg "Perform a disk cleanup" -ShowConsole -Color DarkGreen -SubMsg
729
730 # Install Disk Cleanup (without Desktop Experience feature)
731 #Copy-Item -Path "$env:WinDir\winsxs\amd64_microsoft-windows-cleanmgr_31bf3856ad364e35_6.1.7600.16385_none_c9392808773cd7da\cleanmgr.exe" -Destination "$env:WinDir\System32\" -Force |out-null
732 #Depends on Windows Language!
733 #Copy-Item -Path "$env:WinDir\winsxs\amd64_microsoft-windows-cleanmgr.resources_31bf3856ad364e35_6.1.7600.16385_en-us_b9cb6194b257cc63\cleanmgr.exe.mui" -Destination "$env:WinDir\System32\en-US\" -Force |out-null
734
735 # Perform a disk cleanup (Windows 2008 R2 only)
736 # Automate by creating the reg checks corresponding to "cleanmgr /sageset:100" so we can use "sagerun:100"
737 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -Type DWord -Value 0x00000002
738 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -Type DWord -Value 0x00000002
739 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -Type DWord -Value 0x00000002
740 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles' -Type DWord -Value 0x00000002
741 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameStatisticsFiles' -Type DWord -Value 0x00000002
742 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameUpdateFiles' -Type DWord -Value 0x00000002
743 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -Type DWord -Value 0x00000002
744 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files' -Type DWord -Value 0x00000002
745 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files' -Type DWord -Value 0x00000002
746 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -Type DWord -Value 0x00000002
747 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -Type DWord -Value 0x00000002
748 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -Type DWord -Value 0x00000002
749 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup' -Type DWord -Value 0x00000002
750 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -Type DWord -Value 0x00000002
751 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -Type DWord -Value 0x00000002
752 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -Type DWord -Value 0x00000002
753 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Type DWord -Value 0x00000002
754 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -Type DWord -Value 0x00000002
755 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Sync Files' -Type DWord -Value 0x00000002
756 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -Type DWord -Value 0x00000002
757 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Type DWord -Value 0x00000002
758 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files' -Type DWord -Value 0x00000002
759 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions' -Type DWord -Value 0x00000002
760 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender' -Type DWord -Value 0x00000002
761 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files' -Type DWord -Value 0x00000002
762 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files' -Type DWord -Value 0x00000002
763 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files' -Type DWord -Value 0x00000002
764 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files' -Type DWord -Value 0x00000002
765 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files' -Type DWord -Value 0x00000002
766 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files' -Type DWord -Value 0x00000002
767 Start-Process 'cleanmgr.exe' -Verb runAs -ArgumentList '/sagerun:100 | out-Null' -Wait
768 Show-BISFProgressBar -CheckProcess "cleanmgr" -ActivityText "Running Disk Cleanup..."
769
770 }
771
772 function Pre-Win8
773 {
774 Write-BISFLog -Msg "Perform a disk cleanup" -ShowConsole -Color DarkGreen -SubMsg
775 # Perform a disk cleanup (Windows 8 only)
776 # Automate by creating the reg checks corresponding to "cleanmgr /sageset:100" so we can use "sagerun:100"
777 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -Type DWord -Value 0x00000002
778 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -Type DWord -Value 0x00000002
779 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -Type DWord -Value 0x00000002
780 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files' -Type DWord -Value 0x00000002
781 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files' -Type DWord -Value 0x00000002
782 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -Type DWord -Value 0x00000002
783 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -Type DWord -Value 0x00000000
784 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -Type DWord -Value 0x00000002
785 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -Type DWord -Value 0x00000002
786 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -Type DWord -Value 0x00000002
787 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -Type DWord -Value 0x00000002
788 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Type DWord -Value 0x00000002
789 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -Type DWord -Value 0x00000002
790 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -Type DWord -Value 0x00000002
791 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files' -Type DWord -Value 0x00000000
792 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files' -Type DWord -Value 0x00000002
793 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files' -Type DWord -Value 0x00000002
794 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files' -Type DWord -Value 0x00000002
795 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files' -Type DWord -Value 0x00000002
796 Set-ItemProperty -Name StateFlags0100 -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files' -Type DWord -Value 0x00000002
797 Start-Process 'cleanmgr.exe' -Verb runAs -ArgumentList '/sagerun:100 | out-Null' -Wait
798 Show-BISFProgressBar -CheckProcess "cleanmgr" -ActivityText "Running Disk Cleanup..."
799 Optimize-BISFWinSxs
800
801 }
802
803 function Pre-Win2012R2
804 {
805
806 Optimize-BISFWinSxs
807 }
808
809 function Pre-Win2016
810 {
811
812 Optimize-BISFWinSxs
813 }
814
815 function Pre-Win10
816 {
817
818 Optimize-BISFWinSxs
819 }
820
821
822
823 ####################################################################
824}
825
826Process {
827 #### Main Program
828 PreCommand
829 Test-DrvLabel
830 Create-BISFTask
831
832
833 ## OS Windows 7 only
834 IF ($OSName -contains '*Windows 7*')
835 {
836 Write-BISFLog -Msg "Running PreCommands for Windows 7 only" -ShowConsole -Color Green
837 Pre-Win7
838 }
839
840 ## OS Windows 2008 R2 only
841 IF (($OSVersion -like "6.1*") -and ($ProductType -eq "3"))
842 {
843 Write-BISFLog -Msg "Running PreCommands for Windows 2008 R2 only" -ShowConsole -Color Green
844 Pre-Win2008R2
845 }
846
847 ## OS Windows 8 only
848 IF ($OSName -contains '*Windows 8*')
849 {
850 Write-BISFLog -Msg "Running PreCommands for Windows 8 only" -ShowConsole -Color Green
851 Pre-Win8
852 }
853
854 ## OS Windows 2012 R2 only
855 IF (($OSVersion -like "6.3*") -and ($ProductType -eq "3"))
856 {
857 Write-BISFLog -Msg "Running PreCommands for Windows 2012 R2 only" -ShowConsole -Color Green
858 Pre-Win2012R2
859 }
860
861 ## OS Windows 2016 only
862 IF (($OSVersion -like "10*") -and ($ProductType -eq "3"))
863 {
864 Write-BISFLog -Msg "Running PreCommands for Windows Server 2016 only" -ShowConsole -Color Green
865 Pre-Win2016
866 }
867
868 ## OS Windows 10 only
869 IF (($OSVersion -like "10*") -and ($ProductType -eq "1"))
870 {
871 Write-BISFLog -Msg "Running PreCommands for Windows 10 only" -ShowConsole -Color Green
872 Pre-Win10
873 }
874}
875End {
876 Add-BISFFinishLine
877}