0
# Define variables
$LogFilePath = "C:\Logs\FileTransferLog.txt"

# Define the array of users
$Users = @("TEST")

foreach ($User in $Users) {
    $SourceShare1 = "\\TEST\TEST\$User\"
    $DestinationShare1 = "\\TEST1\TEST\$User\"
    $WatcherServer1 = New-Object System.IO.FileSystemWatcher
    $WatcherServer1.Path = $SourceShare1
    $WatcherServer1.Filter = "*.*"
    $WatcherServer1.IncludeSubdirectories = $false
    $WatcherServer1.EnableRaisingEvents = $true

    # Define the event handler for new file creation
    $OnCreated = Register-ObjectEvent $WatcherServer1 "Created" -Action {
        # Get the name of the new file
        $NewFile = $Event.SourceEventArgs.Name
        # Move the new file to the archive share using service account
        Move-Item "$SourceShare1\$NewFile" $DestinationShare1 -erroraction continue -confirm:$false -force
        # Log the file transfer activity to a text file
        Write-Output "Moved file $NewFile from $SourceShare1 to $DestinationShare1 at $(Get-Date)" | Out-File $LogFilePath -Append
        
    }
}

# Wait for events to occur
while ($true) { Start-Sleep -Seconds 1 }

This works fine when i run it from Powershell ISE but only for a few hours (1 or 2ish) and then it stops working, by stops working i mean it wont move any files thats added even though the script still runs. Also wont work in powershell nor task scheduler. I have approved to run powershell scripts thats unsigned locally.

Tried removing the wait timer, removed the credentials needed, run as local, domain admin and as the specified service account but still wont be lasting long enough or work in task scheduler.

S3C
  • 1
  • 1

0 Answers0