Private/Clear-CitrixExportFolders.ps1

<#
.SYNOPSIS
Cleans up export folders by deleting files from specific directories except protected folders.
 
.DESCRIPTION
The Clear-CitrixExportFolders function removes all files from a predefined set of Citrix export subfolders under a root export directory,
excluding protected folders like 'CitrixSessions' and 'Json'. This helps maintain a clean state before generating a new report.
 
.PARAMETER RootExportFolder
The root folder path under which Citrix export subfolders exist and will be cleaned (except protected folders).
 
.EXAMPLE
Clear-CitrixExportFolders -RootExportFolder "C:\Temp\CitrixReports"
 
Cleans up all Citrix export subfolders under C:\Temp\CitrixReports, skipping 'CitrixSessions' and 'Json'.
 
.NOTES
- Protected folders: 'CitrixSessions' and 'Json' will not be touched.
- Only files are removed; folder structures are preserved.
#>


function Clear-CitrixExportFolders {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$RootExportFolder
    )

    $foldersToSkip = @("CitrixSessions", "Json")
    $folderNames = @(
        "CitrixDetails",
        "CitrixSessions",
        "Hypervisors",
        "Json",
        "Licensing",
        "CitrixResourceList",
        "ServerList",
        "VDIList"
    )

    foreach ($folder in $folderNames) {
        $folderPath = Join-Path $RootExportFolder $folder

        if (-not (Test-Path $folderPath)) {
            Write-Host "📁 Skipping missing folder: $folderPath"
            continue
        }

        if ($foldersToSkip -contains $folder) {
            Write-Host "🚫 Skipping cleanup for protected folder: $folder"
            continue
        }

        $files = Get-ChildItem -Path $folderPath -File -ErrorAction SilentlyContinue
        if ($files.Count -gt 0) {
            Write-Host "🧹 Removing $($files.Count) file(s) from: $folderPath"
            $files | Remove-Item -Force -ErrorAction SilentlyContinue
        } else {
            Write-Host "✅ No files to remove in: $folderPath"
        }
    }
}