Collecting the Output of Remoting Commands

Collecting the Output of Remoting Commands

Rate This
  • Comments 1

One question we often get around PowerShell Remoting is "How do I collect the output of remoting commands into a different file for each computer?" 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 easy in PowerShell due to the automatic properties that we add to remote output. The trick is to just know that it exists :)

                                                                                                                                  
106 [C:\windows\system32]                                                                                                         
>> Invoke-Command myComputer { "Hello World" }                                                                                     
Hello World                                                                                                                       
                                                                                                                                  
107 [C:\windows\system32]                                                                                                         
>> Invoke-Command myComputer { "Hello World" } | Format-List * -Force                                                              
                                                                                                                                  
                                                                                                                         
PSComputerName     : myComputer
RunspaceId         : 30c313ff-00a2-4a21-a001-1309c5d12a1d                                                                         
PSShowComputerName : True                                                                                                         
Length             : 11                                                                                                           
                                                                                                                                  

 

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:

                                                                                                                                
130 [C:\windows\system32]                                                                                                         
>> $job = Invoke-Command comp1,comp2 {
"Hello World: from $(hostname): $(Get-Random)" } -AsJob 131 [C:\windows\system32] >> Wait-Job $job 132 [C:\windows\system32] >> $r = Receive-Job $job 133 [C:\windows\system32] >> $r Hello World: from comp1: 341068382 Hello World: from comp2: 1357111073 134 [C:\windows\system32] >> $r.PSComputerName comp1 comp2 135 [C:\windows\system32] >> $r | % { $_ > c:\temp\$($_.PSComputerName).output } 136 [C:\windows\system32] >> dir c:\temp\*.output Directory: C:\temp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/16/2014 11:04 AM 88 comp1.output -a--- 4/16/2014 11:04 AM 82 comp2.output

 

Lee Holmes [MSFT]
Windows PowerShell Development

Leave a Comment
  • Please add 4 and 4 and type the answer here:
  • Post
  • Lee,

    Great tip!  Totally agree...it's not just "impressive" or "easy"...it's "impressively easy" #PowerShellSwagger

Page 1 of 1 (1 items)