Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created June 30, 2021 18:23
Show Gist options
  • Save steviecoaster/e537f38fc2206e64fbffd0acddbd0416 to your computer and use it in GitHub Desktop.
Save steviecoaster/e537f38fc2206e64fbffd0acddbd0416 to your computer and use it in GitHub Desktop.
[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