{"id":598,"date":"2020-09-15T18:55:00","date_gmt":"2020-09-15T18:55:00","guid":{"rendered":"http:\/\/draith.azurewebsites.net\/?p=598"},"modified":"2021-01-20T19:03:23","modified_gmt":"2021-01-20T19:03:23","slug":"easy-powershell-api-endpoint-with-fluentd","status":"publish","type":"post","link":"https:\/\/draith.com\/?p=598","title":{"rendered":"EASY PowerShell API Endpoint with FluentD"},"content":{"rendered":"\n<p>One of the biggest problems that I have had with PowerShell is that it&#8217;s just too good. I want to use it for everything. Need to perform automation based on monitoring events? Pwsh. Want to update rows in a database when someone clicks a link on a webpage? Pwsh. Want to automate annoying your friends with the push of a button on your phone? Pwsh. I use it for everything. There is just one problem.<\/p>\n\n\n\n<p>I am lazy and impatient. Ok, that&#8217;s two problems. Maybe counting is a third.<\/p>\n\n\n\n<p>I want things to happen instantly. I don&#8217;t want to schedule something in task scheduler. I don&#8217;t want have to run a script manually. I want an API like end-point that will allow me to trigger my shenanigans immediately.<\/p>\n\n\n\n<p>Oh, and I want it to be simple.<\/p>\n\n\n\n<p>Enter <a href=\"https:\/\/www.fluentd.org\/\" data-type=\"URL\" data-id=\"https:\/\/www.fluentd.org\/\">FluentD<\/a>. What is Fluentd you ask? From the website &#8211; &#8220;Fluentd allows you to unify data collection and consumption for a better use and understanding of data.&#8221; I don&#8217;t necessarily agree with this statement, though &#8211; I believe it&#8217;s so much more than that. I view it more like an integration engine with a wide community of <a href=\"https:\/\/www.fluentd.org\/plugins\/all\" data-type=\"URL\" data-id=\"https:\/\/www.fluentd.org\/plugins\/all\">plug-ins<\/a> that allow you to integrate a wide variety of toolsets. It&#8217;s simple, light-weight, and quick. It doesn&#8217;t consume a ton of resources sitting in the background, either. You can run it on a ton of different platforms too &#8211; *nix, windows, docker, etc\u2026 There is even a slim version for edge devices &#8211; IOT or small containers. And I can run it all on-prem if I want.<\/p>\n\n\n\n<p>What makes it so nice to use with PowerShell is that I can have a web API endpoint stood up in seconds that will trigger my PowerShell scripts. Literally &#8211; it&#8217;s amazingly simple. A simple config file like this is all it takes:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;no&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">&lt;source&gt;\n  @type http\n  port 9880\n&lt;\/source&gt;\n<\/pre><\/div>\n\n\n\n<p>Boom &#8211; you have an listener on port 9880 ready to accept data. If you want to run a PowerShell script from the data it receives, just expand your config file a little.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;powershell&quot;,&quot;mime&quot;:&quot;application\/x-powershell&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;PowerShell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;powershell&quot;}\">&lt;source&gt;\n  @type http\n  port 9880\n&lt;\/source&gt;\n\n#Outputs\n&lt;match **&gt;\n  @type exec\n  command &quot;e:\/tasks\/pwsh\/pwsh.exe  -file e:\/tasks\/pwsh\/events\/start-annoyingpeople.ps1&quot;\n  &lt;format&gt;\n    @type json\n  &lt;\/format&gt;\n  &lt;buffer&gt;\n    flush_interval 2s\n  &lt;\/buffer&gt;\n&lt;\/match&gt;<\/pre><\/div>\n\n\n\n<p>With this config file you are telling FluentD to listen on port 9880 (http:\/\/localhost:9880\/automation?) for traffic. If it sees a JSON payload (post request) on that port, it will execute the command specified &#8211; in this case, my script to amuse me and annoy my friends. All I have to do is run this as a service on my Windows box (or a process on *Nix, of course) and I have a fully functioning PowerShell executing web API endpoint. <\/p>\n\n\n\n<p>It doesn&#8217;t have to just be web, either. They have over <a href=\"https:\/\/www.fluentd.org\/plugins\/all\" data-type=\"URL\" data-id=\"https:\/\/www.fluentd.org\/plugins\/all\">800 plug-ins<\/a> for input and output channels. Want SNMP traps to trigger your scripts? You can do it. How about an entry in a log starting your PowerShell fun? Sure! Seriously &#8211; take a look at FluentD and how it can up your PowerShell game immensely.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the biggest problems that I have had with PowerShell is that it&#8217;s just too good. I want to use it for everything. Need to perform automation based on monitoring events? Pwsh. Want to update rows in a database when someone clicks a link on a webpage? Pwsh. Want to automate annoying your friends &hellip; <a href=\"https:\/\/draith.com\/?p=598\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;EASY PowerShell API Endpoint with FluentD&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[16],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-powershell"],"_links":{"self":[{"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/598","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=598"}],"version-history":[{"count":1,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/draith.com\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/draith.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}