{"id":286,"date":"2014-02-03T23:41:00","date_gmt":"2014-02-03T21:41:00","guid":{"rendered":"http:\/\/kra.lc\/blog\/?p=286"},"modified":"2015-03-30T15:45:30","modified_gmt":"2015-03-30T13:45:30","slug":"powershell-java-bridge","status":"publish","type":"post","link":"https:\/\/kra.lc\/blog\/2014\/02\/powershell-java-bridge\/","title":{"rendered":"PowerShell and a bridged gap to Java"},"content":{"rendered":"<p>Please find me a reason why I have done this? \u2014 <i>No, no&#8230; I don&#8217;t need any excuse, Windows is a great operating system and the PowerShell is an overdue extention to it *cough, cough, the hurts, ahh I&#8217;m dying!*<\/i> \u2014 But seriously, due to the system environment at my workplace, I am mainly working with Microsoft Windows and affiliated programs, such as Microsoft Office (Outlook) and Lync. This does not reflect my personal preference, but adaption is key sometimes.<\/p>\n<p>In fact the Windows Power<a href=\"http:\/\/en.wikipedia.org\/wiki\/Koopa_Troopa\">Shell<\/a> is really not as bad, as the name might suggest it to be (plus, the <a href=\"http:\/\/tech.firstpost.com\/news-analysis\/microsofts-metro-ui-renamed-windows-8-ui-33799.html\">name<\/a> <a href=\"http:\/\/mashable.com\/2014\/01\/27\/microsoft-onedrive\/\">is not<\/a> <a href=\"http:\/\/microsoft-news.com\/microsoft-might-face-trademark-lawsuit-for-onedrive-brand-too\/\">claimed<\/a>, yet). The <i>Power<\/i>Shell is a quite <i>power<\/i>full framework when it comes to task automation and configuration management in an Windows environment. Due to its scripting capabilities and its full access to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Component_Object_Model\">COM<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Windows_Management_Instrumentation\">WMI<\/a> it is a major leap compared to its &quot;predecessor&quot;, the Windows Command Prompt. The concept of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms714395.aspx\">cmdlets<\/a> and especially the object-oriented pipes are a somewhat modern approach of the &quot;old school&quot; executables and text-based pipes. <b>But<\/b> \u2014 and this is a bold-<b>but<\/b> \u2014 is probably a blessing and a curse at once.<\/p>\n<p><!--more--> <\/p>\n<p>As always if you follow a new approach in programming, interoperability and commutability are two major challenges to face in development. Unsuprisingly as I tried to take advantage of the PowerShells capabilities, I painfully realized: Microsoft hasn&#8217;t tackled any of those, or did they? Let the devil take the hindmost! Now you could argue, why should you combine a platform-independent programming language such as Java, with a heavily platform dependent framework as the PowerShell? Simple answer, Java is a very sophisticated programming language, the PowerShell offers a very elegant way of accessing the programs I have to work with. <a href=\"http:\/\/dragonball.wikia.com\/wiki\/Fusion\"><strong>Fusion<\/strong><\/a>.<\/p>\n<p>Java is capable of communicating using text- and\/or binary pipes, such as they are used in the old Windows Command Line or any Unix shell. A few lines of code are enough to easily utilize those:<\/p>\n<pre class=\"syntax java\" title=\"ShellExample.java\">\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.io.OutputStream;\r\nimport java.io.PrintWriter;\r\n\r\npublic class ShellExample {\r\n\tpublic static void main(String[] args) throws IOException, InterruptedException {\r\n\t\t\/\/start a cmd \/ shell-instance (also works with \/bin\/sh)\r\n\t\tfinal Process process = Runtime.getRuntime().exec(&quot;cmd&quot;);\r\n\t\t\/\/handle the data of the input stream (non-blocking)\r\n\t\tpipe(process.getInputStream(),System.out);\r\n\t\t\/\/handle the data of the error stream (non-blocking)\r\n\t\tpipe(process.getErrorStream(),System.err);\r\n\t\t\r\n\t\t\/\/pass the commands to the output stream\r\n\t\tPrintWriter commands = new PrintWriter(process.getOutputStream(),true);\r\n\t\tcommands.println(&quot;cd&quot;);\r\n\t\tcommands.println(&quot;dir&quot;);\r\n\t\tcommands.close();\r\n\t\t\r\n\t\t\/\/wait for the process to close\r\n\t\tprocess.waitFor();\r\n\t}\r\n\t\r\n\tprivate static void pipe(final InputStream from,final OutputStream to) {\r\n\t\tnew Thread(new Runnable() {\r\n\t\t\t@Override public void run() {\r\n\t\t\t\tint length; byte[] buffer = new byte[128];\r\n\t\t\t\ttry {\r\n\t\t\t\t\twhile((length=from.read(buffer))!=-1)\r\n\t\t\t\t\t\tto.write(buffer,0,length);\r\n\t\t\t\t} catch(IOException e) { e.printStackTrace(); }\r\n\t\t\t\tfinally {\r\n\t\t\t\t  try { from.close(); }\r\n\t\t\t\t\tcatch(IOException e) { \/* ... *\/ }\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}).start();\r\n\t}\r\n}\r\n<\/pre>\n<p>So what&#8217;s it with the Windows PowerShell? Simply using the above code? Well, not in the first place at least.<\/p>\n<p>The default in- and output pipes of the Windows PowerShell do not talk text (&#8230;or do they?)! In my thoughts there was no way of pushing Java Objects to the PowerShell&#8217;s object-pipes (cause obviously Java Objects &#8800; PowerShell Objects). So I got my hands on the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh849893.aspx\">Invoke-Expression cmdlet<\/a> and the mission was set:<\/p>\n<ol>\n<li>Start an Windows PowerShell instance, invoking a prepared script,<\/li>\n<li>let the script continiously read a prepared input file and pass everything it reads on to the Invoke-Expression cmdlet,<\/li>\n<li>fill the file, from Java, handle all exceptions, close the PowerShell and delete all files properly.<\/li>\n<\/ol>\n<p>This does definitely not sound like a way to go, but what should I say? I&#8217;m <a href=\"http:\/\/en.wikiquote.org\/wiki\/Frankenstein_(1931_film)\">The man who made a monster<\/a>&#8230; which is threadsafe, does garbage collection and utilizes shutdown hooks. Please take a quick peak at <a href=\"\/blog\/releases\/stable\/powershell.zip\">the monster<\/a> &#8230; and delete it right afterwards cause:<\/p>\n<p>After hours of programming and to make a long story short, I found out that simply invoking the above script using the following exec-command:<\/p>\n<p><code>PowerShell -NoExit -Command -<\/code><\/p>\n<p>(notice the trailed dash), instead of just <code>PowerShell<\/code> works totally fine. <strong>F*ck me<\/strong>.<\/p>\n<p><i>Postscript:<\/i> Neither I can blame Microsoft, they have cautiously <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847736.aspx\">documented<\/a> the &quot;<code>-command -<\/code>&quot; parameter, which will read the command text from standard input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Please find me a reason why I have done this? \u2014 No, no&#8230; I don&#8217;t need any excuse, Windows is a great operating system and the PowerShell is an overdue extention to it *cough, cough, the hurts, ahh I&#8217;m dying!* \u2014 But seriously, due to the system environment at my workplace, I am mainly working [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[147,148,139,141,149,5,143,145,138,142,151,140,137,150,146,144,17],"class_list":["post-286","post","type-post","status-publish","format-standard","hentry","category-release","tag-activity","tag-availability","tag-bridge","tag-cmd","tag-execute","tag-java","tag-lync","tag-mail","tag-microsoft","tag-outlook","tag-pipe","tag-power-shell","tag-powershell","tag-process","tag-status","tag-unread","tag-windows"],"_links":{"self":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts\/286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/comments?post=286"}],"version-history":[{"count":52,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/posts\/286\/revisions\/383"}],"wp:attachment":[{"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/media?parent=286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/categories?post=286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kra.lc\/blog\/wp-json\/wp\/v2\/tags?post=286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}