Script not pulling subset of files

So I have been working on this script to archive log files that we create when running other scripts. The problem that I am finding is that instead of archiving just the files in between the dates we indicate, the script is archiving all of the files in the directory it is working on. I am not sure why my logic for only pulling files between certain dates provided is not working. Any help on the issue would be greatly appreciated.

[CmdLetBinding()]
param(
	[Parameter(Mandatory=$False, HelpMessage="Path to a .csv or .txt file with a list of servers (no header)")]
    [string]$ServerListFile = $null,
	[Parameter(Mandatory=$False, HelpMessage="Path to the folders to be archived")]
    [string]$BaseFolder = $null,
	[Parameter(Mandatory=$False, HelpMessage="Oldest date of files to archive")]
    [string]$StartDate = $null,
	[Parameter(Mandatory=$False, HelpMessage="Newest date of files to archive")]
    [string]$EndDate = $null,
	[Parameter(Mandatory=$False, HelpMessage="Name of a computer")]
    [array]$ComputerName = $null
)

if($basefolder){
    $Beginfolder = $basefolder
} else {
    $Beginfolder = "E:\Automation\Results"
}

if($startdate){
    $startdays = (New-TimeSpan -start $StartDate -End (Get-Date)).days
    $Start = (get-date).adddays(-$startdays)
} else {
    $start = (get-date).addMonths(-1)
}

if($enddate){
    $enddays = (New-TimeSpan -start $EndDate -End (Get-Date)).days
    $End = (get-date).adddays(-$enddays)
} else {
    $end = (get-date).adddays(-1)
}

function Get-TimeStamp {
    return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
}

$LogFolder = "E:\Automation\Results\Archival"
$Exists = Test-Path -Path $LogFolder
if ($Exists -eq "True") {
    Write-Host "Directory Exists!"
} else {
    New-Item -Path $LogFolder -ItemType directory
}

$logdate = (Get-Date -UFormat ".%a %m-%d-%Y %H-%M %p")
$logs = new-item -path $LogFolder -Name "Log Archival Results$logdate.log"

Set-Location -Path "$BeginFolder"

$Directories = Get-ChildItem -Path "$BeginFolder" -directory -exclude "Archive"

if ($null -eq $Directories){
    $Directories = $Beginfolder
}

$Directories | Foreach-Object -ThrottleLimit 15 -Parallel {
    $Directory = $_
    write-host "$Directory"
    $Folder = $_.name
    write-host "$folder"
    $DestFolder = "$Directory\Archive"
    write-host = "$DestFolder"
    Write-Host "$logs"
    $Exists = Test-Path -Path $DestFolder
    if ($Exists -eq "True") {
        Write-Host "Directory Exists!"
    } else {
        New-Item -Path $DestFolder -ItemType directory
    }

    $Files = (Get-ChildItem -path $Directory -File | where-object{$_.Creationdate -ge $start -and $_.Creationdate -le $end})
    write-host "$files"

    $Filename = "$Folder results logs between $using:StartDate and $using:EndDate.zip"
    write-host "$Filename"

    Foreach ($File in $Files) {
        function Get-TimeStamp {
            return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
        }
        $Logs = $using:Logs
        Write-Host "$logs"
        Write-Host "$Filename"
        write-host "$file"
        Compress-Archive -path "$file" -DestinationPath "$DestFolder\$Filename" -CompressionLevel Fastest -update -whatif
        Write-Host "$(Get-Timestamp) File $file has been added to the archive file $Filename"
        Add-Content -path $Logs -Value "$(Get-Timestamp) File $file has been added to the archive file $Filename"
        Remove-item -path $file -confirm:$False -recurse -whatif
    }
}

$Directory = Get-Item -Path "$BeginFolder"
write-host "$Directory"
$Folder = $Directory.Name
write-host "$folder"
$DestFolder = "$Beginfolder\Archive"
write-host = "$DestFolder"
$Exists = Test-Path -Path $DestFolder
if ($Exists -eq "True") {
    Write-Host "Directory Exists!"
} else {
    New-Item -Path $DestFolder -ItemType directory
}

$Files = (Get-ChildItem -path $Beginfolder -file | where-object{$_.Creationtime -ge $start -and $_.Creationtime -le $end})
write-host "$files"

$Filename = "$folder results logs between $StartDate and $EndDate.zip"
write-host "$Filename"

Foreach ($File in $Files){
    function Get-TimeStamp {
        return "[{0:MM/dd/yy} {0:HH:mm:ss}]" -f (Get-Date)
    }
    write-host "$file"
    Compress-Archive -path "$file" -DestinationPath "$DestFolder\$Filename" -CompressionLevel Fastest -update -whatif
    Write-Host "$(Get-Timestamp) File $file has been added to the archive file $Filename"
    Add-Content -path $Logs -Value "$(Get-Timestamp) File $file has been added to the archive file $Filename"
    Remove-item -path $file -whatif
}

Without actually digging into your code … I can see several issues.

First of all you make us review your complete script while you actually only have an issue with a small aspect of it. :face_with_raised_eyebrow:

Why do you define the function Get-TimeStamp three times? :point_up_2:t4:

Why do your script parameters all have the type [String] while some of them should have [DateTime]? And it does not make any sense to initialize them with $Null. If you initialize parameters you should do it with valid and meaningful default values.

Are you sure you want to check for the CreationTime of the files? Most of the times that’s missleading as this property gets changed when the files are copied. If you copy an old file to a given folder it will get the current time as the creation time while the LastWriteTime stays the same as in the source folder.

The following small function does what it is suppose to do - it outputs a selection of files from a given folder in a given time frame:

function Select-Files {
    [CmdletBinding()]
    param (
        [DateTime]$StartDate,
        [DateTime]$EndDate,
        [string]$Path
    )
    Get-ChildItem -Path $Path -Filter * |
        Where-Object {
            $_.LastWriteTime -ge $StartDate -and
            $_.LastWriteTime -lt $EndDate
        }
}

I’d recommend to read the following help topics:

Edit:

And BTW: The property is called CreationTime not CreationDate :wink:

Olaf,

As always you have set me up for success. I took your advice with a modification. Instead of putting parameters in the function, I used the existing parameters and variables I already had set.

function Select-Files {
        Get-ChildItem -Path $Directory -Filter * |
            Where-Object {
                $_.LastWriteTime -ge $using:Start -and
                $_.LastWriteTime -le $using:End
            }
    }

This ultimately was a rewrite of another script I had wrote to allow us to use PowerShell 7’s ForEach-Object -Parallel instead of just ForEach to include concurrency into the script.

I’d recommend - if you do rewrite something anyway you should do it properly. :wink: It would make your code more reliable and easier to maintain.