{"id":1731,"date":"2014-04-16T12:34:07","date_gmt":"2014-04-16T12:34:07","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2014\/04\/16\/collecting-the-output-of-remoting-commands\/"},"modified":"2019-02-18T13:05:21","modified_gmt":"2019-02-18T20:05:21","slug":"collecting-the-output-of-remoting-commands","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/collecting-the-output-of-remoting-commands\/","title":{"rendered":"Collecting the Output of Remoting Commands"},"content":{"rendered":"<p>One question we often get around PowerShell Remoting is &quot;How do I collect the output of remoting commands into a different file for each computer?&quot; For example, you want to invoke a remote command across 1,000 machines as a multi-threaded job and then create 1,000 text files for their output.<\/p>\n<p>This is in fact impressively easy in PowerShell due to the automatic properties that we add to remote output. The trick is to just know that it exists \ud83d\ude42<\/p>\n<pre>                                                                                                                                  \n106 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>Invoke-Command<\/span> myComputer { <span>&quot;Hello World&quot;<\/span> }                                                                                     \nHello World                                                                                                                       \n                                                                                                                                  \n107 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>Invoke-Command<\/span> myComputer { <span>&quot;Hello World&quot;<\/span> } | <span>Format-List<\/span> <span>*<\/span> <span>-Force<\/span>                                                              \n                                                                                                                                  \n                                                                                                                         \nPSComputerName     : myComputer\nRunspaceId         : 30c313ff-00a2-4a21-a001-1309c5d12a1d                                                                         \nPSShowComputerName : True                                                                                                         \nLength             : 11                                                                                                           \n                                                                                                                                  <\/pre>\n<p>&#160;<\/p>\n<p>As you can see in the example, we tag remote output with the computer name from which it originated. If we leverage that property, we can now create output files for remote output quite easily:<\/p>\n<pre>                                                                                                                                \n130 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>$job<\/span> <span>=<\/span> <span>Invoke-Command<\/span> comp1<span>,<\/span>comp2 {<br \/>    <span>&quot;Hello World: from <\/span>$(<span>hostname<\/span>)<span>: <\/span>$(<span>Get-Random<\/span>)<span>&quot;<\/span> } <span>-AsJob<\/span>                                     \n                                                                                                                                  \n131 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>Wait-Job<\/span> <span>$job<\/span>                                                                                                                  \n                                                                                                                                  \n132 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>$r<\/span> <span>=<\/span> <span>Receive-Job<\/span> <span>$job<\/span>                                                                                                          \n                                                                                                                                  \n133 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>$r<\/span>                                                                                                                             \nHello World: from comp1: 341068382                                                                                            \nHello World: from comp2: 1357111073                                                                                         \n                                                                                                                                  \n134 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>$r<\/span>.<span>PSComputerName<\/span>                                                                                                              \ncomp1                                                                                                                         \ncomp2                                                                                                                       \n                                                                                                                                  \n135 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>$r<\/span> | <span>%<\/span> { <span>$_<\/span> &gt; c:\\temp\\$(<span>$_<\/span>.<span>PSComputerName<\/span>).output }                                                                            \n                                                                                                                                  \n136 [C:\\windows\\system32]                                                                                                         \n&gt;&gt; <span>dir<\/span> c:\\temp\\*.output                                                                                                                                                                                                                                       \n                                                                                                                                  \n    Directory: C:\\temp                                                                                                            \n                                                                                                                       \n                                                                                                                                  \nMode                LastWriteTime     Length Name                                                                                 \n----                -------------     ------ ----                                                                                 \n-a---         4\/16\/2014  11:04 AM         88 comp1.output                                                                   \n-a---         4\/16\/2014  11:04 AM         82 comp2.output                                                                     \n                                                                                                                                  \n                     <\/pre>\n<p>&#160;<\/p>\n<p>Lee Holmes [MSFT] \n  <br \/>Windows PowerShell Development<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One question we often get around PowerShell Remoting is &quot;How do I collect the output of remoting commands into a different file for each computer?&quot; For example, you want to invoke a remote command across 1,000 machines as a multi-threaded job and then create 1,000 text files for their output. This is in fact impressively [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1731","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>One question we often get around PowerShell Remoting is &quot;How do I collect the output of remoting commands into a different file for each computer?&quot; For example, you want to invoke a remote command across 1,000 machines as a multi-threaded job and then create 1,000 text files for their output. This is in fact impressively [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=1731"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/1731\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=1731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=1731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=1731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}