Skip to content

Commit

Permalink
Merge pull request #816 from Ana06/clean-up
Browse files Browse the repository at this point in the history
Improve VM-Clean-Up
  • Loading branch information
Ana06 authored Jan 4, 2024
2 parents b29ec65 + 677a194 commit 990b75d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/common.vm/common.vm.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
<metadata>
<id>common.vm</id>
<version>0.0.0.20231221</version>
<version>0.0.0.20240104</version>
<description>Common libraries for VM-packages</description>
<authors>Mandiant</authors>
</metadata>
Expand Down
27 changes: 25 additions & 2 deletions packages/common.vm/tools/vm.common/vm.common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,19 @@ public class Shell {
}
}

# Remove a directory and if not possible as many of its subfolder and files as possible
function VM-Remove-Dir {
param (
[Parameter(Mandatory=$true)]
[string[]]$item
)
Remove-Item -Path $item -Recurse -Force -ErrorAction Continue
if (!$? -and $item.PSIsContainer) {
Get-ChildItem -Path $item -Force | ForEach-Object {
VM-Remove-Dir $item
}
}
}

# Usage example:
# VM-Remove-DesktopFiles -excludeFolders "Labs", "Demos" -excludeFiles "MICROSOFT Windows 10 License Terms.txt", "Labs.zip"
Expand All @@ -1257,8 +1270,9 @@ function VM-Remove-DesktopFiles {
[Parameter(Mandatory=$false)]
[string[]]$excludeFiles
)
# Ensure that the "PS_Transcripts" folder and the Tools folder (if located on the desktop) are not deleted.
$defaultExcludedFolders = @("PS_Transcripts", ${Env:TOOL_LIST_DIR})
# Ensure that the "Recycle Bin" folder, the "PS_Transcripts" folder,
# and the Tools folder (if located on the desktop) are not deleted.
$defaultExcludedFolders = @("Recycle Bin", "PS_Transcripts", ${Env:TOOL_LIST_DIR})
# Ensure that the "fakenet_logs" shortcut is not deleted.
$defaultExcludedFiles = @("fakenet_logs.lnk")
$excludeFolders = $excludeFolders + $defaultExcludedFolders
Expand All @@ -1285,6 +1299,9 @@ function VM-Remove-DesktopFiles {
}
}
}

# Remove as much of PS_Transcripts as possible
VM-Remove-Dir "PS_Transcripts"
}

function VM-Clear-TempAndCache {
Expand Down Expand Up @@ -1328,12 +1345,18 @@ function VM-Clean-Up {
Write-Host "[+] Clearing Temp and Cache..." -ForegroundColor Green
VM-Clear-TempAndCache

Write-Host "[+] Clearing Recycle Bin" -ForegroundColor Green
Clear-RecycleBin -Force

Write-Host "[+] Running Disk Cleanup..." -ForegroundColor Green
VM-Write-Log "INFO" "Performing Disk Cleanup."
Invoke-Expression 'cmd /c cleanmgr.exe /AUTOCLEAN'

Write-Host "[+] Clearing up free space. This may take a few minutes..." -ForegroundColor Green
VM-Clear-FreeSpace

Write-Host "[+] Clear PowerShell History" -ForegroundColor Green
Remove-Item (Get-PSReadlineOption).HistorySavePath -Force -ea 0
}

function VM-Add-To-Path {
Expand Down

0 comments on commit 990b75d

Please sign in to comment.