Cleanup-TestFiles.ps1


Description

Purpose

No synopsis provided.

Detailed Description

No detailed description provided.

Back to Top

Usage

No usage examples provided.

Back to Top

Notes

No additional notes.

Back to Top


Script

function Cleanup-TestFiles {
    [CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Medium')]
    param (
        [Parameter(Mandatory = $true)]
        [string[]]$LogFiles
    )

    foreach ($logFile in $LogFiles) {
        Write-Verbose "Processing log file: $logFile"

        if (-not (Test-Path -Path $logFile)) {
            Write-Warning "Log file not found: $logFile"
            continue
        }

        $logData = Get-Content -Path $logFile | ConvertFrom-Json

        foreach ($entry in $logData) {
            $server = $entry.ServerName
            $filePath = $entry.FullName

            if (-not $filePath) {
                Write-Warning "No file path specified in log entry for server: $server"
                continue
            }

            $destinationPath = "\\$server\C$\Temp\$($entry.FileName)"
            Write-Verbose "Attempting to remove file: $destinationPath on server: $server"

            if ($PSCmdlet.ShouldProcess($destinationPath, "Remove file")) {
                try {
                    Remove-Item -Path $destinationPath -Force -ErrorAction Stop
                    Write-Verbose "File removed successfully: $destinationPath"
                } catch [System.Management.Automation.ItemNotFoundException] {
                    Write-Warning "File not found: $destinationPath. It may have already been deleted."
                } catch {
                    Write-Error "Failed to remove file: $destinationPath. $_"
                }
            }
        }
    }
}

# Example usage
# $logFiles = Get-ChildItem -Path "C:\Temp\TrainingGround" -Filter "CopyLog-*.json" | Select-Object -ExpandProperty FullName
# Cleanup-TestFiles -LogFiles $logFiles -Verbose -WhatIf

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top