1

I am totally new to Powershell, but I needed an all-Windows solution for a Slack bot that monitors local folders.

I am able to use the CLI to post to Slack successfully with the following two commands:

$postSlackMessage = @{token="";channel="#general";text="Test message";username="Bot User"}

Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage

I am also able to monitor the folder using the script provided on this superuser answer by @nixda.

My version of this great solution doesn't seem to cut it. Here it is:

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Location\"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $token = ""
                $channel = "$general"
                $text = "$changeType, $path"
                $username = "Bot User"
                $postSlackMessage = @{token=$token; channel=$channel; text=$text; username=$username}
                Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
    $created = Register-ObjectEvent $watcher "Created" -Action $action
    $changed = Register-ObjectEvent $watcher "Changed" -Action $action
    $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
    $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}

I've tried a number of different variations on this and I am not sure what I'm doing wrong. I can get the nixda script to work and write to a txt file but changing the action to Invoke-RestMethod doesn't work.

Is this a syntax problem I'm not getting, or something greater I'm missing?

ezgoodnight
  • 133
  • 6
  • Does it produce an error when it "doesn't work" with Invoke-RestMethod? Is $postSlackMessage populated as expected when you get to that line during execution? – Ƭᴇcʜιᴇ007 Jul 11 '16 at 18:57
  • This is an excellent question, nothing seems to come up in the PS window running it and I don't have any experience using Try Catch. If you have a reference or advice I'll update the script and share the error. – ezgoodnight Jul 11 '16 at 19:00
  • Off hand I see `$channel = "$general"` appears to be wrong. You haven't defined `$general` anywhere, and in your "working" example, it's `"#general"`. – Ƭᴇcʜιᴇ007 Jul 11 '16 at 19:01
  • Use an editor that allows breakpoint: https://msdn.microsoft.com/en-us/powershell/scripting/core-powershell/ise/how-to-debug-scripts-in-windows-powershell-ise – Ƭᴇcʜιᴇ007 Jul 11 '16 at 19:02
  • Yes, that's a bit of ignorance I caught and thought I fixed. It's supposed to be slack channel #general. I'll update that. – ezgoodnight Jul 11 '16 at 19:02
  • Re: breakpoint I have a lot to learn. I'll digest what's on that page and see what I can update. Thank you for this advice. – ezgoodnight Jul 11 '16 at 19:06
  • @Ƭᴇcʜιᴇ007 That syntax error actually fixed the problem if you'd like to post it as an answer. The script works as expected with the channel listed correctly. – ezgoodnight Jul 11 '16 at 19:37

1 Answers1

0

Since there are a few views on this I'll post the corrected script, as pointed out by Techie007. It was only a minor typo and this posts to Slack if you'd like use Powershell scripts to watch folders in Windows:

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Location\"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $false
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER A EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $token = ""
                $channel = "#general"
                $text = "$changeType, $path"
                $username = "Bot User"
                $postSlackMessage = @{token=$token; channel=$channel; text=$text; username=$username}
                Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED + SET CHECK FREQUENCY  
    $created = Register-ObjectEvent $watcher "Created" -Action $action
    $changed = Register-ObjectEvent $watcher "Changed" -Action $action
    $deleted = Register-ObjectEvent $watcher "Deleted" -Action $action
    $renamed = Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 5}
ezgoodnight
  • 133
  • 6