Skip to content

Commit 8dc42cb

Browse files
committed
Added Remove-CrashDump
1 parent 6e513bb commit 8dc42cb

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Tests/WindowsDiskCleanup.Tests.ps1

+45
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,49 @@ Describe 'WindowsDiskCleanup.Tests' {
4848
Test-Path $path | Should -Be $true
4949
}
5050
}
51+
52+
Context 'Remove-CrashDump Tests' {
53+
BeforeAll {
54+
$LOCALAPPDATA = "$PSScriptRoot\LocalAppData"
55+
$env:LOCALAPPDATA = $LOCALAPPDATA
56+
$crashDumpPath = [System.IO.Path]::Combine($env:LOCALAPPDATA, 'CrashDumps')
57+
$testDumpFilePath = [System.IO.Path]::Combine($crashDumpPath, 'test.dmp')
58+
59+
if (-not (Test-Path $crashDumpPath)) {
60+
New-Item -Path $crashDumpPath -ItemType Directory -Force | Out-Null
61+
}
62+
}
63+
64+
BeforeEach {
65+
$file = New-Item -Path $testDumpFilePath -ItemType File -Force
66+
$file.CreationTimeUtc = (Get-Date).AddDays(-10)
67+
}
68+
69+
AfterEach {
70+
if (Test-Path $testDumpFilePath) {
71+
Remove-Item -Path $testDumpFilePath -Force
72+
}
73+
}
74+
75+
AfterAll {
76+
if (Test-Path $LOCALAPPDATA) {
77+
Remove-Item -Path $LOCALAPPDATA -Recurse -Force
78+
}
79+
}
80+
81+
It 'should remove crash dump files older than specified days' {
82+
Remove-CrashDump -Days 5 -DryRun:$false
83+
Test-Path $testDumpFilePath | Should -Be $false
84+
}
85+
86+
It 'should not remove crash dump files in dry run mode' {
87+
Remove-CrashDump -Days 5 -DryRun:$true
88+
Test-Path $testDumpFilePath | Should -Be $true
89+
}
90+
91+
It 'should do nothing if crash dump path does not exist' {
92+
Remove-Item -Path $crashDumpPath -Recurse -Force
93+
{ Remove-CrashDump -Days 5 -DryRun:$false } | Should -Not -Throw
94+
}
95+
}
5196
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function Remove-CrashDump {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter(Mandatory = $true)]
5+
[int]$Days,
6+
7+
[switch]$DryRun
8+
)
9+
10+
process {
11+
$crashDumpPath = [System.IO.Path]::Combine($env:LOCALAPPDATA, 'CrashDumps')
12+
if (Test-Path $crashDumpPath) {
13+
Get-ChildItem $crashDumpPath -Filter "*.dmp" | Remove-OldFile -Days $Days -DryRun:$DryRun
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)