-
Notifications
You must be signed in to change notification settings - Fork 250
/
provision.ps1
83 lines (70 loc) · 2.4 KB
/
provision.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
$ErrorActionPreference = "Stop"
. a:\Test-Command.ps1
Write-Host "Enabling file sharing firewall rules"
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
if(Test-Path "e:/VBoxWindowsAdditions.exe") {
Write-Host "Installing Guest Additions"
Get-ChildItem E:\cert\ -Filter vbox*.cer | ForEach-Object {
E:\cert\VBoxCertUtil.exe add-trusted-publisher $_.FullName --root $_.FullName
}
mkdir "C:\Windows\Temp\virtualbox" -ErrorAction SilentlyContinue
Start-Process -FilePath "e:/VBoxWindowsAdditions.exe" -ArgumentList "/S" -WorkingDirectory "C:\Windows\Temp\virtualbox" -Wait
Remove-Item C:\Windows\Temp\virtualbox -Recurse -Force
}
Write-Host "Cleaning SxS..."
Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase
@(
"$env:localappdata\Nuget",
"$env:localappdata\temp\*",
"$env:windir\logs",
"$env:windir\panther",
"$env:windir\temp\*",
"$env:windir\winsxs\manifestcache"
) | % {
if(Test-Path $_) {
Write-Host "Removing $_"
try {
Takeown /d Y /R /f $_
Icacls $_ /GRANT:r administrators:F /T /c /q 2>&1 | Out-Null
Remove-Item $_ -Recurse -Force | Out-Null
} catch { $global:error.RemoveAt(0) }
}
}
Write-Host "defragging..."
if (Test-Command -cmdname 'Optimize-Volume') {
Optimize-Volume -DriveLetter C
} else {
Defrag.exe c: /H
}
Write-Host "0ing out empty space..."
$FilePath="c:\zero.tmp"
$Volume = Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'"
$ArraySize= 64kb
$SpaceToLeave= $Volume.Size * 0.05
$FileSize= $Volume.FreeSpace - $SpacetoLeave
$ZeroArray= new-object byte[]($ArraySize)
$Stream= [io.File]::OpenWrite($FilePath)
try {
$CurFileSize = 0
while($CurFileSize -lt $FileSize) {
$Stream.Write($ZeroArray,0, $ZeroArray.Length)
$CurFileSize +=$ZeroArray.Length
}
}
finally {
if($Stream) {
$Stream.Close()
}
}
Del $FilePath
Write-Host "copying auto unattend file"
mkdir C:\Windows\setup\scripts
copy-item a:\SetupComplete-2012.cmd C:\Windows\setup\scripts\SetupComplete.cmd -Force
mkdir C:\Windows\Panther\Unattend
copy-item a:\postunattend.xml C:\Windows\Panther\Unattend\unattend.xml
Write-Host "Recreate pagefile after sysprep"
$System = GWMI Win32_ComputerSystem -EnableAllPrivileges
if ($system -ne $null) {
$System.AutomaticManagedPagefile = $true
$System.Put()
}