Created
June 30, 2021 18:23
-
-
Save steviecoaster/e537f38fc2206e64fbffd0acddbd0416 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
Param( | |
[Parameter()] | |
[String] | |
$Path = 'C:\drop' | |
) | |
process { | |
$FileFilter = '*' | |
$IncludeSubfolders = $false | |
$AttributeFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite | |
try { | |
$watcher = New-Object -TypeName System.IO.FileSystemWatcher -Property @{ | |
Path = $Path | |
Filter = $FileFilter | |
IncludeSubdirectories = $IncludeSubfolders | |
NotifyFilter = $AttributeFilter | |
} | |
# define the code that should execute when a change occurs: | |
$action = { | |
# change type information: | |
$file = (Get-ChildItem $Path -Include *.exe,*.msi -Recurse).Fullname | |
Write-Host "Processing: $file" | |
$chocoArgs = @('new' | |
"--file='$file'" | |
'--build-package' | |
"--output-directory='C:\processed'") | |
Write-Host "Received arguments: $($chocoArgs -join ' ')" | |
& choco @chocoArgs | |
Write-Host "Removing binary" | |
Remove-Item $file -Force | |
Get-ChildItem -Path 'C:\processed' -Exclude *.nupkg | Remove-Item -Recurse -Force | |
} | |
$handlers = . { | |
Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action | |
} | |
$watcher.EnableRaisingEvents = $true | |
Write-Host "Watching for changes to $Path" | |
do { | |
Wait-Event -Timeout 1 | |
} while ($true) | |
} | |
finally { | |
# this gets executed when user presses CTRL+C: | |
$watcher.EnableRaisingEvents = $false | |
$handlers | ForEach-Object { | |
Unregister-Event -SourceIdentifier $_.Name | |
} | |
$handlers | Remove-Job | |
$watcher.Dispose() | |
Write-Warning "Event Handler disabled, monitoring ends." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment