FileSystemWatcher in a scedualed task

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of David Jones David Jones 2 years ago.

  • Author
    Posts
  • #25595
    Profile photo of David Jones
    David Jones
    Participant

    This works when ran interactively but ran as a scedualed job I get the log, but robocopy does not fire.


    if (-not (Test-Path -Path $env:ALLUSERSPROFILE\logs ))
    {
    New-Item -ItemType directory -Path $env:ALLUSERSPROFILE\logs
    }
    if (-not (Test-Path -Path $env:ALLUSERSPROFILE\logs\fsw.log))
    {
    New-Item -ItemType file -Path $env:ALLUSERSPROFILE\logs\fsw.log
    }

    $folder = 'C:\scripts\test' # Enter the root path you want to monitor.
    $filter = '*.*' # You can enter a wildcard filter here.

    # In the following line, you can change 'IncludeSubdirectories to $true if required.
    $fsw = New-Object -TypeName IO.FileSystemWatcher -ArgumentList $folder, $filter -Property @{
    IncludeSubdirectories = $true
    NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }

    # Here, all three events are registerd. You need only subscribe to events that you need:

    $BasicAction = {
    $destinationList = 'server1', 'server2', 'server3', 'server4'
    #write-host ($Event.SourceEventArgs | gm)
    $name = $Event.SourceEventArgs.FullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host -Object "The file '$name' was $changeType at $timeStamp" -ForegroundColor green
    Out-File -FilePath $env:ALLUSERSPROFILE\logs\fsw.log -Append -InputObject "The file '$name' was $changeType at $timeStamp"

    foreach ($destination in $destinationList)
    {
    #Write-Host -Object $destination
    $FilePath = "\\$destination\upload\test\$($Event.SourceEventArgs.Name)"
    Out-File -FilePath $env:ALLUSERSPROFILE\logs\fsw.log -Append -InputObject "The file '$FilePath' was $changeType at $timeStamp"
    Write-Host -Object "The file '$FilePath' was $changeType to at $timeStamp" -ForegroundColor green
    $SourcePath = $folder
    $DestPath = "\\$destination\upload\test\"
    c:\Windows\System32\Robocopy.exe $SourcePath $DestPath /COPYALL /B /MIR /R:0 /W:0 /NP /LOG+:h:\RCJobs\HJob.log /TEE /SEC /NFL /NDL
    }
    }

    $RenameAction = {
    $destinationList = 'server1', 'server2', 'server3', 'server4'
    #write-host ($Event.SourceEventArgs | gm)
    $name = $Event.SourceEventArgs.FullPath
    $oldname = $Event.SourceEventArgs.OldFullPath
    $changeType = $Event.SourceEventArgs.ChangeType
    $timeStamp = $Event.TimeGenerated
    Write-Host -Object "The file '$oldname' was $changeType to '$name' at $timeStamp" -ForegroundColor green
    Out-File -FilePath $env:ALLUSERSPROFILE\logs\fsw.log -Append -InputObject "The file '$name' was $changeType at $timeStamp"
    foreach ($destination in $destinationList)
    {
    #Write-Host -Object $destination
    $RenameSource = "\\$destination\upload\test\$($Event.SourceEventArgs.OldName)"
    $RenameDest = "\\$destination\upload\test\$($Event.SourceEventArgs.Name)"
    Out-File -FilePath $env:ALLUSERSPROFILE\logs\fsw.log -Append -InputObject "The file '$RenameSource' was $changeType to '$RenameDest' at $timeStamp"
    Write-Host -Object "The file '$RenameSource' was $changeType to '$RenameDest' at $timeStamp" -ForegroundColor green
    $SourcePath = $folder
    $DestPath = "\\$destination\upload\test\"
    c:\Windows\System32\Robocopy.exe $SourcePath $DestPath /COPYALL /B /MIR /R:0 /W:0 /NP /LOG+:h:\RCJobs\HJob.log /TEE /SEC /NFL /NDL
    }
    }

    Register-ObjectEvent -InputObject $fsw -EventName Created -SourceIdentifier FileCreated -Action $BasicAction
    Register-ObjectEvent -InputObject $fsw -EventName Changed -SourceIdentifier FileChanged -Action $BasicAction
    Register-ObjectEvent -InputObject $fsw -EventName Deleted -SourceIdentifier FileDeleted -Action $BasicAction
    Register-ObjectEvent -InputObject $fsw -EventName Renamed -SourceIdentifier FileRenamed -Action $RenameAction

    I have a scheduled task running this command.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -NoProfile -File "C:\scripts\FileObjectEvent.ps1"
    I know the task running

  • #25596
    Profile photo of David Jones
    David Jones
    Participant

    Never mind. I should have asked the duck.

    robocopy did not need /LOG+:h:\RCJobs\HJob.log /TEE and could not acces h:

    that's what I get for using copy and paste.

You must be logged in to reply to this topic.