2

Situation Summary

Currently I have two separate Powershell(PS) Consoles, each with its own Filewatcher(FW), watching for a created file(txt file) from each Build. The code is nearly identical excluding the Notify Filter. The both currently call the same script to start the tests. I have 2 sets of logs(Output added to a txt file) that come as a result of a triggered FW, One Log is for the TestPS Script and then the other is the Test Results pumped out through a CMD prompt(The tests are using Selenium Webdriver). The TestPS Script doesn't determine which tests to run but I cannot log which one was called because the of the handoff from FW to TestPS. I only know which one was called from the Test Results after the fact.

The order of operations are as follows:

-2 PS Scripts each with a FW are started prior, left running.

-One FW with Notify Filter "A" watching folder location "A"

-Second FW with Notify Filter "B" watching folder location "B"

-One Builds kicks off, creates file in corresponding location and filter

-Triggered FW calls Main PS script to start Tests

What I want it to do:

-Only One FW is started prior, left running

-FW with Notify Filters "A" and "B", Watches Folder Location "C"

-One of Builds kicks off, Triggers location "C"

-FW somehow Filters or Passes some sort of information(Such as the Filename which I can Parse in the Test PS for a key phrase or something) to the TestPS to indicate which build had been triggered.

-FW calls Main PS script to start Tests (Could happen in the same step as above, Doesn't necessarily matter)

I have tried the Following:

Method A: 2 PSs each with their own FW watching their own location with a filter variation each with a Register-ObjectEvent that calls the TestPS. (Current/technically works)

Method B: 1 PS with 2 FWs each with filter variation with 1 Register-ObjectEvent that calls the TestPS

Method C: 1 PS with 2 FW each with filter variation with 2 Register-ObjectEvents that calls the TestPS

Method D: 1 PS with 1 FW watching Subdirectories with 1 Register-ObjectEvent that calls the TestPS

Method E:(Theory) 1 PS with 1 FW with 2 filters that passes which filter was detected to TestPS

Function FileWatcher{
    $folder = New-Object IO.FileSystemWatcher " <FilePathA> ", "*.triggerOne" -Property @{
        IncludeSubdirectories = $false
        NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }
    $folderTwo = New-Object IO.FileSystemWatcher " <Either FilePathA or FilePathB> ", "*.triggerTwo" -Property @{
        IncludeSubdirectories = $false
        NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
    }
    $eventAction = Register-ObjectEvent ($folder) Created -SourceIdentifier FileCreated -Action {   
        try{
            Start-Process -FilePath Powershell -ArgumentList " <FilePath to TestPS> " #Within this powershell I want to somehow be able to determine which Filewatcher was triggered
        }
        catch [System.Exception]{
            Add-Content -Path " <FilePath to Txt file for logging errors> " -Value ("Error: "+$error[0])
            Write-Warning ("Error: "+$error[0])
        }
    }
    $eventActionTwo = Register-ObjectEvent ($folderTwo) Created -SourceIdentifier FileCreated -Action {   
        try{
            Start-Process -FilePath Powershell -ArgumentList " <FilePath to TestPS> " #Within this powershell I want to somehow be able to determine which Filewatcher was triggered
        }
        catch [System.Exception]{
            Add-Content -Path " <FilePath to Txt file for logging errors> " -Value ("Error: "+$error[0])
            Write-Warning ("Error: "+$error[0])
        }
    }
}

FileWatcher #Method Call

Results:

Method A: Current/Technically works, but requires setup as described in Summary.

Method B: Works, but No indication of which is called/No way to Create one Register-ObjectEvent with two filters

Method C: Apparently cannot have two Register-ObjectEvent with FileCreated source even with different filters. Error: Register-ObjectEvent : Cannot subscribe to the specified event. A subscriber with the source identifier 'FileCreated' already exists. <...> + CategoryInfo : InvalidArgument: (System.IO.FileSystemWatcher:FileSystemWatcher) [Register-ObjectEvent], ArgumentException + FullyQualifiedErrorId : SUBSCRIBER_EXISTS,Microsoft.PowerShell.Commands.RegisterObjectEventCommand

Method D: No indication to TestPS of which build was triggered

Method E: Theory, Haven't tried

bad_coder
  • 11,289
  • 20
  • 44
  • 72

0 Answers0