Want to start PowerShell commands from a Tweet? Yeah you do, and you didn’t even know you wanted to.
Earlier this month, a great Flow of the Week was posted that highlighted the ability to use a .net filesystemwatcher to kick off local processes. This sparked an idea – I think we can expand on this and basically run anything we want. Here’s how:
First, let’s start with the Connected Gateway. The link above goes into a bit of detail on how to configure the connection. Nothing special there.
Second, on the Connected Gateway, run this PowerShell script:
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher $FileSystemWatcher.path = "C:\temp\WatchMe" $FileSystemWatcher.Filter = "Flow.txt" $FileSystemWatcher.EnableRaisingEvents = $true Register-ObjectEvent $FileSystemWatcher "Changed" -Action { $content = get-content C:\temp\WatchMe\Flow.txt |select-object -last 1 powershell.exe $content }
This script sets up a FileSystemWatcher on the C:\temp\WatchMe\Flow.txt file. The watcher will only perform an action if the file is changed. There are several options for the “Changed” parameter – Created, Deleted, Renamed, Error, etc… Once created, the watcher will look at the last line of the c:\temp\WatchMe\Flow.txt file, and launch a PowerShell process that takes that last line as the input.
Third – This is the best part. Since we have a FileSystemWatcher, and that watcher is reading the last line of the C:\temp\WatchMe\Flow.txt file and kicking that process off, all we have to do is append a line to that file to start a PowerShell session. Flow has a built-in connection for FileSystem. You can see where this is going. Create a new Flow, and add an input action – I am fond of the Outlook.com Email Arrives action. Supply a suitable trigger in the subject, and add the ‘Append File’ action from the FileSystem service. Here is how mine is configured:
The only catch with this particular setup is that the body of the email needs to be in plain text – Windows 10 Mail app, for example, will not send in plain text. The body of the mail is the PowerShell command we want to run. For example, maybe we want PowerShell to get a list of processes that have a certain name, and dump those to a text file for parsing later. Simply send an email that has the body of “get-process -name chrome|out-file c:\temp\ChromeProcesses.txt”. Here is what that results in:
Before we send the email:
The Email:
After a few minutes – an new folder appears!:
The contents of the text file:
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 1956 97 131588 191648 694.98 728 1 chrome 249 22 34268 43356 1.63 4264 1 chrome 381 81 307592 331312 145.16 6080 1 chrome 149 12 2140 10076 0.05 7936 1 chrome 632 86 277900 557484 974.00 9972 1 chrome 431 31 147956 159404 182.11 10056 1 chrome 219 12 2132 9608 0.08 11636 1 chrome 283 50 135932 141512 98.05 12224 1 chrome 396 54 133912 297432 18.58 12472 1 chrome 253 46 107348 106752 50.13 13276 1 chrome 381 48 114452 128836 242.89 14328 1 chrome
Think about what you could do with this – Perhaps you want to do an Invoke-WebRequest every time a RSS Feed updates. Maybe start a set of diagnostic commands when an item is added to Sharepoint. Kick off actions with a Tweet. If you want full scripts to run instead of commands, change the Action section of the FileSystemWatcher to “PowerShell.exe -file $content”. Easy as pie.