{"id":210,"date":"2017-06-28T13:19:41","date_gmt":"2017-06-28T19:19:41","guid":{"rendered":"http:\/\/draith.com\/?p=210"},"modified":"2017-06-28T13:19:41","modified_gmt":"2017-06-28T19:19:41","slug":"run-_anything_-with-flow-powershell-triggers","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=210","title":{"rendered":"Run _Anything_ with Flow.  PowerShell Triggers"},"content":{"rendered":"<p>Want to start PowerShell commands from a Tweet?  Yeah you do, and you didn&#8217;t even know you wanted to.<\/p>\n<p>Earlier this month, a great <a href=\"https:\/\/flow.microsoft.com\/en-us\/blog\/flow-of-the-week-local-code-execution\/\">Flow of the Week<\/a> was posted that highlighted the ability to use a .net filesystemwatcher to kick off local processes.  This sparked an idea &#8211; I think we can expand on this and basically run anything we want.  Here&#8217;s how:<\/p>\n<p>First, let&#8217;s start with the Connected Gateway.  The link above goes into a bit of detail on how to configure the connection.  Nothing special there.<br \/>\nSecond, on the Connected Gateway, run this PowerShell script:<\/p>\n<pre class=\"lang:ps decode:true \" >$FileSystemWatcher = New-Object System.IO.FileSystemWatcher\n$FileSystemWatcher.path = \"C:\\temp\\WatchMe\"\n$FileSystemWatcher.Filter = \"Flow.txt\"\n$FileSystemWatcher.EnableRaisingEvents = $true\n \nRegister-ObjectEvent $FileSystemWatcher \"Changed\" -Action {\n$content =  get-content C:\\temp\\WatchMe\\Flow.txt |select-object -last 1\npowershell.exe $content\n}<\/pre>\n<p>This script sets up a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.io.filesystemwatcher(v=vs.110).aspx\">FileSystemWatcher <\/a>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 &#8220;Changed&#8221; parameter &#8211; Created, Deleted, Renamed, Error, etc&#8230;  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.<\/p>\n<p>Third &#8211; 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 &#8211; I am fond of the Outlook.com Email Arrives action.  Supply a suitable trigger in the subject, and add the &#8216;Append File&#8217; action from the FileSystem service.  Here is how mine is configured:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/06\/EmailtoPowerShell.png\" alt=\"\" width=\"1075\" height=\"843\" class=\"alignnone size-full wp-image-213\" srcset=\"\/wp-content\/uploads\/2017\/06\/EmailtoPowerShell.png 1075w, \/wp-content\/uploads\/2017\/06\/EmailtoPowerShell-300x235.png 300w, \/wp-content\/uploads\/2017\/06\/EmailtoPowerShell-1024x803.png 1024w, \/wp-content\/uploads\/2017\/06\/EmailtoPowerShell-768x602.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>The only catch with this particular setup is that the body of the email needs to be in plain text &#8211; 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 &#8220;get-process -name chrome|out-file c:\\temp\\ChromeProcesses.txt&#8221;.  Here is what that results in:<br \/>\nBefore we send the email:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/06\/Before.jpg\" alt=\"\" width=\"664\" height=\"365\" class=\"alignnone size-full wp-image-215\" srcset=\"\/wp-content\/uploads\/2017\/06\/Before.jpg 664w, \/wp-content\/uploads\/2017\/06\/Before-300x165.jpg 300w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>The Email:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/06\/EmailtoPowerShell.jpg\" alt=\"\" width=\"804\" height=\"786\" class=\"alignnone size-full wp-image-220\" srcset=\"\/wp-content\/uploads\/2017\/06\/EmailtoPowerShell.jpg 804w, \/wp-content\/uploads\/2017\/06\/EmailtoPowerShell-300x293.jpg 300w, \/wp-content\/uploads\/2017\/06\/EmailtoPowerShell-768x751.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>After a few minutes &#8211; an new folder appears!:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/draith.com\/wp-content\/uploads\/2017\/06\/TextFile.jpg\" alt=\"\" width=\"943\" height=\"647\" class=\"alignnone size-full wp-image-219\" srcset=\"\/wp-content\/uploads\/2017\/06\/TextFile.jpg 943w, \/wp-content\/uploads\/2017\/06\/TextFile-300x206.jpg 300w, \/wp-content\/uploads\/2017\/06\/TextFile-768x527.jpg 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>The contents of the text file:<\/p>\n<pre class=\"lang:ps decode:true \" >\nHandles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                                  \n-------  ------    -----      -----     ------     --  -- -----------                                                  \n   1956      97   131588     191648     694.98    728   1 chrome                                                       \n    249      22    34268      43356       1.63   4264   1 chrome                                                       \n    381      81   307592     331312     145.16   6080   1 chrome                                                       \n    149      12     2140      10076       0.05   7936   1 chrome                                                       \n    632      86   277900     557484     974.00   9972   1 chrome                                                       \n    431      31   147956     159404     182.11  10056   1 chrome                                                       \n    219      12     2132       9608       0.08  11636   1 chrome                                                       \n    283      50   135932     141512      98.05  12224   1 chrome                                                       \n    396      54   133912     297432      18.58  12472   1 chrome                                                       \n    253      46   107348     106752      50.13  13276   1 chrome                                                       \n    381      48   114452     128836     242.89  14328   1 chrome                                                       \n<\/pre>\n<p>Think about what you could do with this &#8211; 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 &#8220;PowerShell.exe -file $content&#8221;.  Easy as pie.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to start PowerShell commands from a Tweet? Yeah you do, and you didn&#8217;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 &#8211; I think we can expand on &hellip; <a href=\"https:\/\/draith.com\/?p=210\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Run _Anything_ with Flow.  PowerShell Triggers&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8,16,24],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-flow","tag-powershell","tag-why-not"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=210"}],"version-history":[{"count":0,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/210\/revisions"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}