Skip to content

Commit

Permalink
Merge pull request #817 from Ana06/fix-clean-up
Browse files Browse the repository at this point in the history
Improve Clean Up
  • Loading branch information
Ana06 authored Jan 5, 2024
2 parents 990b75d + a17538e commit 9352d21
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 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.20240104</version>
<version>0.0.0.20240105</version>
<description>Common libraries for VM-packages</description>
<authors>Mandiant</authors>
</metadata>
Expand Down
40 changes: 11 additions & 29 deletions packages/common.vm/tools/vm.common/vm.common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -1245,20 +1245,6 @@ 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"
# The function is run against both the Current User and 'Public' desktops due to some cases where desktop icons showing on
Expand All @@ -1285,23 +1271,19 @@ function VM-Remove-DesktopFiles {
# Use -Force to get hidden files (such as desktop.ini)
Get-ChildItem -Path $userDesktopPath -Force | ForEach-Object {
$item = $_
try{
if ($item.PSIsContainer -and ($item.Name -notin $excludeFolders -and $item.FullName -notin $excludeFolders)) {
VM-Write-Log "INFO" "Deleting folder: $($item.FullName)"
Remove-Item -Path $item.FullName -Recurse -Force -ErrorAction Continue
}
elseif ($item.PSIsContainer -eq $false -and ($item.Name -notin $excludeFiles -and $item.FullName -notin $excludeFiles)) {
VM-Write-Log "INFO" "Deleting file: $($item.FullName)"
Remove-Item -Path $item.FullName -Force -ErrorAction Continue
}
} catch {
VM-Write-Log-Exception $_
if ($item.PSIsContainer -and ($item.Name -notin $excludeFolders -and $item.FullName -notin $excludeFolders)) {
VM-Write-Log "INFO" "Deleting folder: $($item.FullName)"
Remove-Item -Path $item.FullName -Recurse -Force -ErrorAction SilentlyContinue
}
elseif ($item.PSIsContainer -eq $false -and ($item.Name -notin $excludeFiles -and $item.FullName -notin $excludeFiles)) {
VM-Write-Log "INFO" "Deleting file: $($item.FullName)"
Remove-Item -Path $item.FullName -Force -ErrorAction SilentlyContinue
}
if(!$?){
VM-Write-Log "ERROR" "`tFailed to delete"
}
}
}

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

function VM-Clear-TempAndCache {
Expand Down Expand Up @@ -1346,7 +1328,7 @@ function VM-Clean-Up {
VM-Clear-TempAndCache

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

Write-Host "[+] Running Disk Cleanup..." -ForegroundColor Green
VM-Write-Log "INFO" "Performing Disk Cleanup."
Expand Down

0 comments on commit 9352d21

Please sign in to comment.