Watcher + Comparer Problem

Hello!
I hope you’re having a fantastic day so far!
I’ve been banging my head on this for a little while and thought I’d try and reach out for some help.

To start, I’d like to put up front, I am hands down a powershell n00b. I am also not extremely versed in general programming (experience 10 years ago haha) - so I’ve been muddling my way through things.

I’m trying to write a script, I’ve got a good chunk of it down, but now I need to write the watcher portion of it.

I pretty much need the watcher to watch for newly created zip folders that are nested within 2 folders. Example of the tree. In this case, I’d be looking for Partybus.#.#.zip:

    Release 1
      Release 1.1
        Partybus.1.1-kdjhfakjfh.zip
        Partywheels.1.1_adflkasdfasd.zip
      Release 1.2
        Partybus.1.2-kdjhfakjfh.zip
        Partywheels.1.2_adflkasdfasd.zip
      Release 1.3
        Partybus.1.3-kdjhfakjfh.zip
        Partywheels.1.3_adflkasdfasd.zip
    Release 2
      Release 2.1
        Partybus.2.1-kdjhfakjfh.zip
        Partywheels.2.1_adflkasdfasd.zip
      Release 2.2
        Partybus.2.2-kdjhfakjfh.zip
        Partywheels.2.2_adflkasdfasd.zip
      Release 2.3
        Partybus.2.3-kdjhfakjfh.zip
        Partywheels.2.3_adflkasdfasd.zip

Now here comes the tricky part. I need to compare the most recent partybus.#.#.zip against the most recent installed partybus version. So if Partybus2.2 is installed and Partybus1.3 is released, nothing will happen; if Partybus 2.3 is released, then proceed to the next steps. I’ve considered having a csv file created and comparing the most recently created partybus zip against it.

I am unsure if the File Watcher will help me in this case, so I built a manual GCI that exports the version into a CSV. My thoughts were the following:

  • GCI exports to CSV Watcher1
  • Compares against last entry in Watcher2
  • If version is higher, add entry in Watcher 2, else nothing.

Here was my stab:
WATCHER 1

Get-ChildItem -path $Stagedrop -include $filter -recurse | Select-Object name | export-csv -path 'c:\temp\watcher1.csv'

Compare against WATCHER2

$file1= import-csv 'c:\temp\Watcher1.csv'
$file2= import-csv 'c:\temp\Watcher2.csv' 

Get-Content 'c:\temp\watcher2.csv' | Select-Object -Last 1 | Compare-Object $file1

InputObject                                       SideIndicator
-----------                                       -------------
"Partybus.2.3-v28e7258c.internal.zip"       =>           
@{Name=Partybus.2.3-v28e7258c.internal.zip} <=   

That output didn’t make me happy haha, nevermind that I don’t (yet) know how to get the versions to show me greater than if partybus version is greater than 2.3 or smaller than 2.3…

I also tried exporting the last line of Watcher 2 into a Watcher3 CSV and comparing the 2 CSVs. But my Watcher3 ends up looking like this:
Code:

Get-Content 'c:\temp\watcher2.csv' | Select-Object -Last 1 | export-csv -path 'c:\temp\watcher3.csv'

Results:

#TYPE System.String
"PSPath","PSParentPath","PSChildName","PSDrive","PSProvider","ReadCount","Length"
"C:\temp\watcher2.csv","C:\temp","watcher2.csv","C","Microsoft.PowerShell.Core\FileSystem","7","43"

Which, out of everything, confuses me more… I’m sure I’m just missing one thing.

EITHER WAY! How would you go about this problem? Any suggestions or ideas? Pointing me in the right direction would be an amazing help! :slight_smile:

Thank you

You may find use with the FileSystemWatcher that PowerShell can use from the .NET framework.

You set the directory and use the Register-ObjectEvent cmdlet to execute actions when things (e.g. FileName, DirectoryName, LastWrite) change in that path. It also has the option to include sub-directories.

I’ve not used the FileSystemWatcher call my self so I don’t know if it continues to work after PowerShell is closed. Test and find if it works as you need!

FileSystemWatcher will close when you close the powershell session. You could trigger the script from a .bat file that will start a powershell session and trigger that on startup if the machine is re-started.

Hello Simon and Dakota,
Thank you for your responses.

To start, I’d like to change the way the hierarchy looks because that’s part of the problem:
Release 1

  • Release 1.1

    • Partybus.1.1-kdjhfakjfh.zip
    • Partywheels.1.1_adflkasdfasd.zip
  • Release 1.2

    • Partybus.1.2-kdjhfakjfh.zip
    • Partywheels.1.2_adflkasdfasd.zip
  • Release 1.3

    • Partybus.1.3-kdjhfakjfh.zip
    • Partywheels.1.3_adflkasdfasd.zip

Release 2

  • Release 2.1

    • Partybus.2.1-kdjhfakjfh.zip
    • Partywheels.2.1_adflkasdfasd.zip
  • Release 2.2

    • Partybus.2.2-kdjhfakjfh.zip
    • Partywheels.2.2_adflkasdfasd.zip
  • Release 2.3

    • Partybus.2.3-kdjhfakjfh.zi
    • Partywheels.2.3_adflkasdfasd.zip

  • I’m still unclear whether the FileSystemWatcher will also compare the version. Here’s what I ended up coming up with:
#Listens for new stable sites dropped on Dev\Stage_drop

#Finds all relevant Partybus Zips
Write-Verbose 'Finding Partybus'

#Create Temp Folder to store files for the comparison
New-Item -ItemType Directory -Force -Path 'c:\tools\autoupgrader\temp\Partybus'

#Exports all Partybus zips to a alphabetical CSV then imports last entry and exports the single line to csv:
Get-ChildItem -path $Stagedrop | Select-Object name | Sort-Object name | export-csv -Path 'c:\tools\autoupgrader\temp\Partybus\watcherTemp.csv'
import-csv 'c:\tools\autoupgrader\temp\Partybus\watcherTemp.csv' | Select-Object -Last 1 | export-csv -path 'c:\tools\autoupgrader\temp\Partybus\watcher1.csv'

#Imports the UpgradeListPartybus and exports the last line:
import-csv 'c:\Tools\AutoUpgrader\UpgradeListPartybus.csv' | Select-Object -Last 1 | export-csv -path 'c:\tools\autoupgrader\temp\Partybus\watcher3.csv'


#Comparing Files:
Write-Verbose "Comparing Partybus Versions"
$file1= import-csv 'c:\tools\autoupgrader\temp\Partybus\watcher1.csv'
$file3= import-csv 'c:\tools\autoupgrader\temp\Partybus\watcher3.csv'
$file1c= $file1 | Out-string
$file3c= $file3 | Out-String

#If the version is greater than the previous recorded version, write it to the upgrade list and continue with the script
IF($file1c -gt $file3c){$file1 | export-csv -path 'c:\Tools\AutoUpgrader\UpgradeListPartybus.csv' -Append

#Setting Filter
$filter = Get-Content 'c:\tools\autoupgrader\temp\Partybus\watcher1.csv' | Select-Object -Skip 2 |  % {$_ -replace '"', ""} 

#The rest of the script runs from here}

Else {write-host 'No new packages to upgrade Partybus found'}