
[CmdletBinding()]
param (
    [string]
    $ComputerName,

    [switch]
    $WhatIf
)


Write-Verbose -Message "Prepare variables / SQL connection based on PSU server" -Verbose
$RequestedBy = $UAJob.Identity.Name # user that started the job
$PSUJobId = $UAJob.Id

$FilesRemovedSuccess = @()
$FilesRemovedFailure = @()
$GetITDExpiredFilesAutoParams += @{}

Write-Verbose -Message ("UAJob.ComputerName = " + $UAJob.ComputerName) -Verbose
switch($UAJob.ComputerName){
    "ITDWINAUTOT1" {
        $ServerInstance = "itdintsql22p1.nd.gov\INTSQL22P1"
        $Database = "ITD-Systems-Automation"
        $Table = "Infra_WindowsServer_FileManagement_RemoveITDExpiredFiles_NPD"
    }
    "ITDWINAUTOP1" {
        $ServerInstance = "itdintsql22p1.nd.gov\INTSQL22P1"
        $Database = "ITD-Systems-Automation"
        $Table = "Infra_WindowsServer_FileManagement_RemoveITDExpiredFiles_PRD"
    }
}

If ($PSBoundParameters.ContainsKey('ComputerName')) {
    Write-Verbose -Message "ComputerName parameter found" -Verbose
    $GetITDExpiredFilesParams = @{
        Credential = $Secret:ndgov_svcitdpsuwin
    }
    $GetITDExpiredFilesParams += @{
        ComputerName = $ComputerName;
    }
}

$FilesToRemove = Get-ITDExpiredFiles @GetITDExpiredFilesParams
Write-Verbose -Message ("Found " + $FilesToRemove.count + " expired files to remove") -Verbose

ForEach ($File in $FilesToRemove) {
    Write-Verbose -Message ("Start~" + $File.PSComputerName + "~" + $File.FullName )
    $ComputerName = $File.PSComputerName
    $DateTime = Get-Date
    $FullName = $File.FullName

    $InvokeCommandParams = @{
        ComputerName = $File.PSComputerName;
        #Credential   = $Secret:ndgov_svcitdpsuwin;
        ErrorAction  = 'Stop';
        ArgumentList = @($File.FullName);
        ScriptBlock  = { Get-Item -Path $args[0] | Remove-Item }
    }

    switch ($WhatIf) {
        $true {
            Write-Verbose -Message "WhatIf switch true" -Verbose
            try {
                Write-Verbose -Message ("Process~" + $File.PSComputerName + "~" + $File.FullName + " removed")
                Write-Host -Message ($Server.ComputerName + " -- " + 'What if: Performing the operation "Remove File" on target ' + $File.FullName)
                # log success
                $FilesRemovedSuccess += [PSCustomObject]@{
                    DateTime = $DateTime.tostring("yyyy/MM/dd HH:mm:ss");
                    ComputerName = $ComputerName;
                    FullName = $FullName;
                }#>
                Write-Output $File
            }
            catch {
                Write-Verbose -Message ("Process~" + $File.PSComputerName + "~" + $File.FullName + " failure")
                # log failure
                $FilesRemovedFailure += [PSCustomObject]@{
                    DateTime = $DateTime.tostring("yyyy/MM/dd HH:mm:ss");
                    ComputerName = $ComputerName;
                    FullName = $FullName;
                }
            }                    
        }
        Default {
            try {
                Write-Verbose -Message "WhatIf switch default" -Verbose
                Invoke-Command @InvokeCommandParams
                Write-Verbose -Message ("Process~" + $File.PSComputerName + "~" + $File.FullName + " removed")
                # log success to sql, add obj to array
                $SqlQuery = "INSERT INTO [$Table] (PSUJobId, DateTime, ComputerName, Status, FullName) Values ('$PSUJobId', '$DateTime', '$ComputerName', 'Success', '$FullName')"
                $SqlRecord = Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Query $SqlQuery -Credential $Secret:sql_itdpsu1 -Verbose
                $FilesRemovedSuccess += [PSCustomObject]@{
                    DateTime = $DateTime.tostring("yyyy/MM/dd HH:mm:ss");
                    ComputerName = $ComputerName;
                    FullName = $FullName;
                }
                Write-Output $File
            }
            catch {
                Write-Verbose -Message ("Start~" + $File.PSComputerName + "~" + $File.FullName + " failure")
                # log failure to sql, add obj to array
                $SqlQuery = "INSERT INTO [$Table] (PSUJobId, DateTime, ComputerName, Status, FullName) Values ('$PSUJobId', '$DateTime', '$ComputerName', 'Failure', '$FullName')"
                $SqlRecord = Invoke-Sqlcmd -ServerInstance $ServerInstance -Database $Database -Query $SqlQuery -Credential $Secret:sql_itdpsu1 -Verbose
                $FilesRemovedFailure += [PSCustomObject]@{
                    DateTime = $DateTime.tostring("yyyy/MM/dd HH:mm:ss");
                    ComputerName = $ComputerName;
                    FullName = $FullName;
                }
            }
        }
    }

    # create CHG request for the work
    Write-Verbose -Message "Submit CHG for the work.   TBD" -Verbose

    Write-Verbose -Message ("End~" + $File.PSComputerName + "~" + $File.FullName ) -Verbose
    ### Generate CHG
}