<#
.SYNOPSIS
    Script will discover all files that are considered "expired".
.DESCRIPTION
    Script will discover all files that are considered "expired" based on the parameters in the ./Helpers/ITDExpiredFiles.json files in the ITD-WindowsServer module
.NOTES
    Information or caveats about the function e.g. 'This function is not supported in Linux'
.LINK
    
.EXAMPLE
    Get-ITDExpiredFiles
.EXAMPLE
    Get-ITDExpiredFiles -Credential $PrvCred
.EXAMPLE
    Get-ITDExpiredFiles -ComputerName itdxyz.nd.gov -Credential $PrvCred
#>

function Get-ITDExpiredFiles {
    [CmdletBinding()]
    param (
        [string]
        $ComputerName,

        [PSCredential]
        $Credential
    )
    Begin {
        Write-Verbose -Message "Load json files into memory"
        $JsonFiles = Get-ChildItem -Path "$PSScriptRoot\..\Helpers\*.json" | Where-Object Name -NE ^template.json
        $MachineInfo = ForEach ($file in $JsonFiles) {
            Get-Content -Path $file.FullName | ConvertFrom-Json
        }
        
        If ($PSBoundParameters.ContainsKey('ComputerName')) {
            Write-Verbose -Message "ComputerName found"
            $MachineInfo = $MachineInfo | Where-Object ComputerName -EQ $ComputerName
        }
    }

    Process {
        Write-Verbose -Message "Prep discovery function"
        $GetExpiredFilesFunc = {
            Write-Verbose -Message ($env:COMPUTERNAME + " " + $args[0] + " " + $args[1] + " " + $args[2])
            
            $GetChildItemParams = @{
                Path    = $args[0];
                Filter  = $args[1];
                Recurse = $args[2];
            }
        
            $FilesFound = (Get-ChildItem @GetChildItemParams | Where-Object LastWriteTime -LT ((Get-Date).AddDays(-$args[3])))
            Write-Output $FilesFound
        }

        $GetITDExpiredFilesResult = [System.Collections.ArrayList]@()
        ForEach ($Server in $MachineInfo) {
            Write-Verbose -Message ("Start " + $Server.ComputerName)

            Write-Verbose -Message "Ping test before any Invoke-Command"
            If ((Test-NetConnection -ComputerName $Server.ComputerName).PingSucceeded) {
                ForEach ($Directory in $Server.Directory) {
                    Write-Verbose -Message ("Start " + $server.ComputerName + " " + $Directory.Path)
                    $FilesFoundOnServer = $null
                    $InvokeCommandParams = $null
                    $InvokeResult = $null
    
                    $InvokeCommandParams = @{
                        ComputerName = $Server.ComputerName;
                        Credential   = $Credential;
                        ScriptBlock  = $GetExpiredFilesFunc;
                        ArgumentList = @($Directory.Path, ("*" + $Directory.Extension), $Directory.Recursive, $Directory.DaysToKeep);
                    }
                    $FilesFoundOnServer = Invoke-Command @InvokeCommandParams
                    Write-Output $FilesFoundOnServer
                    #$null = $GetITDExpiredFilesResult.Add($FilesFoundOnServer)
                    Write-Verbose -Message ("End   " + $server.ComputerName + " " + $Directory.Path)
                }
            }
            Else {
                Write-Error -Message ($Server.ComputerName + " ping test failed, generate ticket someday.")
            }
        }
        Write-Verbose -Message ("End   " + $server.ComputerName)
    }
    End {
        #Write-Output $GetITDExpiredFilesResult
        Write-Verbose -Message "End   Get-ITDExpiredFiles"
    }
}