Skip to content

Commit

Permalink
run dism cleanup after reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
mwrock committed Sep 27, 2016
1 parent ba4f4b6 commit ea7c057
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions hyperv-nano.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"answer_files/nano/hyperv/Autounattend.xml",
"scripts/nano_hyperv_create.ps1",
"scripts/nano_cleanup.ps1",
"scripts/nano_cleanup_after_reboot.ps1",
"scripts/SetupComplete.cmd",
"scripts/postunattend.xml"
]
Expand Down
2 changes: 1 addition & 1 deletion scripts/SetupComplete.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ REG ADD HKLM\Software\Microsoft\Windows\CurrentVersion\WSMAN\Client /v auth_basi
netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
wmic useraccount where "name='vagrant'" set PasswordExpires=FALSE

cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command . c:\windows\setup\scripts\nano_cleanup.ps1 > c:\windows\setup\scripts\cleanup.txt
cmd.exe /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command . c:\windows\setup\scripts\nano_cleanup.ps1 > c:\windows\setup\scripts\cleanup1.txt
29 changes: 2 additions & 27 deletions scripts/nano_cleanup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ Import-Module C:\windows\system32\windowspowershell\v1.0\Modules\Storage\Storage
$sess = New-CimInstance -Namespace root/Microsoft/Windows/WindowsUpdate -ClassName MSFT_WUOperationsSession
Invoke-CimMethod -InputObject $sess -MethodName ApplyApplicableUpdates

Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase

$partition = Get-Partition -DriveLetter C
$c_size = $partition.size
$partition = Get-Partition -DriveLetter D
Expand All @@ -15,28 +13,5 @@ $d_size = $partition.size
Remove-Partition -DriveLetter D -Confirm:$false
Resize-Partition -DriveLetter C -Size ($c_size + $d_size)

Optimize-Volume -DriveLetter C

$FilePath="c:\zero.tmp"
$Volume= Get-Volume -DriveLetter C
$ArraySize= 64kb
$SpaceToLeave= $Volume.Size * 0.05
$FileSize= $Volume.SizeRemaining - $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

shutdown /s /t 0
schtasks /create /tn "Postinstall" /tr "powershell -file c:\windows\setup\scripts\nano_cleanup_after_reboot.ps1" /sc onstart /RL highest /ru vagrant /rp vagrant /f
shutdown /r /t 0
34 changes: 34 additions & 0 deletions scripts/nano_cleanup_after_reboot.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Import-Module C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Utility\Microsoft.PowerShell.Utility.psd1
Import-Module C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1
Import-Module C:\windows\system32\windowspowershell\v1.0\Modules\Storage\Storage.psd1

start-transcript -path $env:temp\transcript1.txt -noclobber

Dism.exe /online /Cleanup-Image /StartComponentCleanup /ResetBase

Optimize-Volume -DriveLetter C

$FilePath="c:\zero.tmp"
$Volume= Get-Volume -DriveLetter C
$ArraySize= 64kb
$SpaceToLeave= $Volume.Size * 0.05
$FileSize= $Volume.SizeRemaining - $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

schtasks /delete /tn "Postinstall" /f
shutdown /s /t 0
1 change: 1 addition & 0 deletions scripts/nano_create.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mkdir E:\Windows\Panther
copy-item a:\postunattend.xml E:\Windows\Panther\unattend.xml -Force
copy-item a:\SetupComplete.cmd E:\Windows\setup\scripts\SetupComplete.cmd -Force
copy-item a:\nano_cleanup.ps1 E:\Windows\setup\scripts\nano_cleanup.ps1 -Force
copy-item a:\nano_cleanup_after_reboot.ps1 E:\Windows\setup\scripts\nano_cleanup_after_reboot.ps1 -Force

bcdedit /set "{current}" device "partition=E:"
bcdedit /set "{current}" osdevice "partition=E:"
Expand Down
1 change: 1 addition & 0 deletions scripts/nano_hyperv_create.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mkdir E:\Windows\Panther
copy-item a:\postunattend.xml E:\Windows\Panther\unattend.xml -Force
copy-item a:\SetupComplete.cmd E:\Windows\setup\scripts\SetupComplete.cmd -Force
copy-item a:\nano_cleanup.ps1 E:\Windows\setup\scripts\nano_cleanup.ps1 -Force
copy-item a:\nano_cleanup_after_reboot.ps1 E:\Windows\setup\scripts\nano_cleanup_after_reboot.ps1 -Force

bcdedit /set "{current}" device "partition=E:"
bcdedit /set "{current}" osdevice "partition=E:"
Expand Down

0 comments on commit ea7c057

Please sign in to comment.