CTP2, Running Console Apps against multiple computers


With CTP2, one of the biggest features is remoting. This works really well when you are running native cmdlets and scriptblocks in runspaces with multiple computers.

For example

function q {$args}
$rs = New-Runspace (q powershell-dev1 powershell-dev2 powershell-dev3)
Invoke-Command -ScriptBlock {hostname} -Runspace $rs

# This will create the following output
PS C:\Users\andys> Invoke-Command -ScriptBlock {hostname} -Runspace $rs

Pretty cool, and you can use the computername property to find out which computer returned which object.

But with commands like ipconfig or netsh that are not native to Powershell, just tacking on the computername property is a little difficult. When you pipe everthing to select-object, format-table, or format-list, which properties do you select.

Under normal circumstances, with say something like Get-Process, you could pipe it to Format-Table Id, Name, WorkingSet.

But with native commands, there are no properties. So here's what you can do.

You can pipe it to select-object and select the whole object using $_ and then also select  $_.ComputerName. The only trick is you have to pass the properties in as scriptblocks,

PS C:\Users\andys> Invoke-Command -ScriptBlock {ipconfig} -Runspace $rs |

select {$_} ,{$_.ComputerName} $_ $_.ComputerName -- --------------- powershell-dev3 Windows IP Configuration powershell-dev3 powershell-dev3 powershell-dev3 Ethernet adapter Local Area Connection: powershell-dev3 powershell-dev3 Connection-specific DNS Suffix . : example.com.... powershell-dev3 Link-local IPv6 Address . . . . . : fe80::4c04:b... powershell-dev3 IPv4 Address. . . . . . . . . . . : powershell-dev3 Subnet Mask . . . . . . . . . . . : powershell-dev3 Default Gateway . . . . . . . . . : powershell-dev3 powershell-dev3 Tunnel adapter Local Area Connection* 8: powershell-dev3 powershell-dev3 Media State . . . . . . . . . . . : Media discon... powershell-dev3 Connection-specific DNS Suffix . : corp.avanade... powershell-dev3 powershell-dev2 Windows IP Configuration powershell-dev2 powershell-dev2 powershell-dev2 Ethernet adapter Local Area Connection: powershell-dev2

The output is not incredibly wonderful but you can at least easily know which remote computer returned which line of text from the command you executed.

Comments are closed