0

Below is my file moving script. My problems:

  1. Super slow in moving files
  2. It doesnt move existing files and
  3. Once it moves it also logs it x5, like its moved the same file x5
  4. Filewatcher kinda not working as it doesnt recognize new files, have to rerun the script a few times for it to make the move
# Define variables
$LogFilePath = "C:\Logs\FileTransferLog.txt"
$ServiceAccountUserName = "test\test"
$ServiceAccountPassword = ConvertTo-SecureString "Test123" -AsPlainText -Force
$ServiceAccountCredential = New-Object System.Management.Automation.PSCredential($ServiceAccountUserName, $ServiceAccountPassword)

# 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
        # 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 }

S3C
  • 1
  • 1

1 Answers1

0

move-item string needed -erroraction Continue -force solved the problems.

S3C
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 02 '23 at 13:10