Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve VM-Clean-Up #816

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading