<#
EFT-Log-Download.ps1
Author: Emil Holgersen, Thales Danmark
Date: 15-04-2020
Version: 1.0
Description: Tool for copying EFT module logfiles from archive to user desktop
NOTE: This script has to be run on Citrix!

Input: logfile date, EFT SAM ID
Output: Logfile in folder on user dekstop, if logfile exists
#>

# Input Date and validate it, and then validate that we have files from that date. Outer while-loop is so we keep asking until we get a valid date with data in archive
While (1) {
    # Input date and validate that the date exists (inner while-loop is so we keep asking until we get a valid date)
    While (1) {
        $Input = (Read-Host -Prompt 'Please input target logfile date')
        Try {
            #Check if valid date
            $Date = [DateTime]$Input
            Write-Host "$($Date.ToString("yyyy-MM-dd")) is a valid date" -fore Green
            Break # Date is valid, break the inner while-loop
        }
        Catch {
            Write-Host "$Input does not appear to be a valid date" -Fore Red
        }
    }

    # Validate that we have logfiles from that date
    # Format is strange around change of month. If the logfiles are from the last of the month, they we be copied and named as if they are the 0th of the following month. The same logic applies at new year.
    # I suspect the reason for that is the code that copies the logfiles does it after midnight, and stores it as "currentday-1"
    [String]$Year = $Date.AddDays(1).ToString("yyyy")
    [String]$Month = $Date.AddDays(1).ToString("MM")
    [String]$Day = ([Int32]$Date.AddDays(1).ToString("dd") -1).ToString("00") # This is a trick/hack: subtract 1, and always show 2 digits (leading 0 if necesary)

    # Check if we have a folder from that date (Format: \\rejk-p-int003\EW\EFTLogs\YYYY\MM\DD)
    If (Test-Path "\\rejk-p-int003\EW\EFTLogs\$Year\$Month\$Day" -PathType Container) {
        Write-Host 'We have logfiles from that date' -Fore Green
        Break # we have logfiles from this date, break the outer while-loop
    }
    Else {
        Write-Host 'We have no logfiles from that date, is it the correct one?' -Fore Red
    }
}

# Input and validate EFT module SAM. Outer while-loop is so we keep asking until user gets it right.
While (1) {
    $Input = (Read-Host -Prompt 'Please input target EFT module SAM')
    Try {
        #Check if valid date
        $EFTSAM = [Int32]$Input #convert to integer
        If ($EFTSAM -ge 41000 -and $EFTSAM -le 49999) {
            $EFTSAM = $($EFTSAM.ToString("000000"))
            Write-Host "$EFTSAM within expected EFT SAM range" -Fore Green
            Break # SAM appears valid, break the while-loop
        }
        Else {
            Throw # Not within range, we therefore return error for the catch to handle
        }
    }
    Catch {
        Write-Host "$Input does not appear to be a valid EFT SAM" -Fore Red
    }
}
# Check if file exists, and copy if it does
$Filepath = "\\rejk-p-int003\EW\EFTLogs\$Year\$Month\$Day\EFTLog-$EFTSAM-$Year$Month$Day.log.gz"
$CurrentUserDesktop = [Environment]::GetFolderPath("Desktop")
If (Test-Path $Filepath -PathType Leaf) {
    Write-Host 'It appears we have logfiles from that EFT on that date' -Fore Green
    If (!(Test-Path "$CurrentUserDesktop\EFTLOGS")) {
        New-Item -ItemType Directory -Force -Path "$CurrentUserDesktop\EFTLOGS" | Out-Null
    }
    If (Test-Path "$CurrentUserDesktop\EFTLOGS\EFTLog-$EFTSAM-$Year$Month$Day.log.gz") {
        Write-Host "File already exists on desktop: $CurrentUserDesktop\EFTLOGS\EFTLog-$EFTSAM-$Year$Month$Day.log.gz" -Fore Red
    }
    Else {
        Try {
            Copy-Item $Filepath -Destination "$CurrentUserDesktop\EFTLOGS\EFTLog-$EFTSAM-$Year$Month$Day.log.gz"
            Write-Host "EFTLog-$EFTSAM-$Year$Month$Day.log.gz copied to EFTLOGS on user desktop" -Fore Green
        }
        Catch {
            Write-Host "File copy failed, unknown error" -Fore Red
        }
    }
}
Else {
    Write-Host "No logfile found for $EFTSAM on that date, check date and SAM. Please note that sometimes logfiles are not archived on server" -Fore Red
}