{"id":5636,"date":"2018-06-13T21:10:44","date_gmt":"2018-06-13T19:10:44","guid":{"rendered":"https:\/\/michlstechblog.info\/blog\/?p=5636"},"modified":"2018-07-24T08:14:17","modified_gmt":"2018-07-24T06:14:17","slug":"powershell-execute-a-process-and-capture-stdout-and-stderr","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-execute-a-process-and-capture-stdout-and-stderr\/","title":{"rendered":"Powershell: Execute a process and capture STDOUT  and STDERR"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_5636 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_5636')){$('.twoclick_social_bookmarks_post_5636').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20Execute%20a%20process%20and%20capture%20STDOUT%20%20and%20STDERR\",\"the_excerpt\":\"Hi%2C%0D%0A%0D%0Aif%20have%20to%20execute%20a%20process%20or%20script%20from%20powershell%20and%20you%20have%20to%20capture%20the%20output%20you%20can%20use%20the%20System.Diagnostics.Process%20class.%20%0D%0A%0D%0AI%27ve%20written%20a%20simple%20function%20for%20this%20case%20%3A-%29%0D%0A%0D%0A%20%28more%26hellip%3B%29\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-execute-a-process-and-capture-stdout-and-stderr\\\/\",\"post_id\":5636,\"post_title_referrer_track\":\"Powershell%3A+Execute+a+process+and+capture+STDOUT++and+STDERR\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Hi,<\/p>\n<p>if have to execute a process or script from powershell and you have to capture the output you can use the System.Diagnostics.Process class. <\/p>\n<p>I&#8217;ve written a simple function for this case \ud83d\ude42<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nfunction fStartProcess(&#x5B;string]$sProcess,&#x5B;string]$sArgs,&#x5B;ref]$pOutPut)\r\n{\r\n\t$oProcessInfo = New-Object System.Diagnostics.ProcessStartInfo\r\n\t$oProcessInfo.FileName = $sProcess\r\n\t$oProcessInfo.RedirectStandardError = $true\r\n\t$oProcessInfo.RedirectStandardOutput = $true\r\n\t$oProcessInfo.UseShellExecute = $false\r\n\t$oProcessInfo.Arguments = $sArgs\r\n\t$oProcess = New-Object System.Diagnostics.Process\r\n\t$oProcess.StartInfo = $oProcessInfo\r\n\t$oProcess.Start() | Out-Null\r\n\t$oProcess.WaitForExit() | Out-Null\r\n\t$sSTDOUT = $oProcess.StandardOutput.ReadToEnd()\r\n\t$sSTDERR = $oProcess.StandardError.ReadToEnd()\r\n\t$pOutPut.Value=&quot;Commandline: $sProcess $sArgs`r`n&quot;\r\n\t$pOutPut.Value+=&quot;STDOUT: &quot; + $sSTDOUT + &quot;`r`n&quot;\r\n\t$pOutPut.Value+=&quot;STDERR: &quot; + $sSTDERR + &quot;`r`n&quot;\r\n\treturn $oProcess.ExitCode\r\n}\r\n<\/pre>\n<p>and a usage example <\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nD:\\&gt; $Output=&quot;&quot;\r\nD:\\&gt; $iRet=fStartProcess ping.exe &quot;-n 1 localhost&quot; (&#x5B;ref]$Output)\r\nD:\\&gt; write-host &quot;Exitcode: $iRet`r`n Output: $Output&quot;\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, if have to execute a process or script from powershell and you have to capture the output you can use the System.Diagnostics.Process class. I&#8217;ve written a simple function for this case \ud83d\ude42<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344,1],"tags":[696,133,1213,1214,1212,1211],"class_list":["post-5636","post","type-post","status-publish","format-standard","hentry","category-powershell-scripting","category-uncategorized","tag-capture","tag-powershell","tag-start-process","tag-start-program","tag-stderr","tag-stdout"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/5636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=5636"}],"version-history":[{"count":3,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/5636\/revisions"}],"predecessor-version":[{"id":5640,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/5636\/revisions\/5640"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=5636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=5636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=5636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}