Skip to content

Commit 44e10f2

Browse files
authored
Skip hook if directory to check handles for doesn't exist (#7)
1 parent 05188f1 commit 44e10f2

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

hooks/purge.ps1

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ param(
1313

1414
pslist -t -accepteula -nobanner
1515

16-
$absDir = Resolve-Path $Dir
17-
"Finding handles in $absDir"
16+
if (Test-Path -Path $Dir) {
17+
$absDir = Resolve-Path $Dir -ErrorAction Stop
18+
} else {
19+
Write-Output "Skipping: Directory $($Dir) does not exist, no file-handles can exist."
20+
exit 0
21+
}
22+
Write-Output "Finding handles in $absDir"
1823
$OUT=$(handle64 -accepteula -nobanner $absDir)
1924

2025
$processMap = @{}
@@ -31,18 +36,18 @@ ForEach ($line in $OUT -split "`r`n")
3136

3237
if ($processMap.Count -eq 0)
3338
{
34-
"No handles found."
39+
Write-Output "No handles found."
3540
}
3641
else
3742
{
3843
$processMap | Format-Table
3944

40-
"Whitelisted: $Whitelist"
45+
Write-Output "Whitelisted: $Whitelist"
4146
foreach($ppid in $processMap.keys)
4247
{
4348
$imageName = $processMap.$ppid
4449
if (! $Whitelist.Contains($imageName)){
45-
"Killing $ppid"
50+
Write-Output "Killing $ppid"
4651
taskkill /f /t /pid $ppid
4752
}
4853
}

0 commit comments

Comments
 (0)