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
powershell-dev2
powershell-dev3
powershell-dev1

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. . . . . . . . . . . : 10.2.31.214 powershell-dev3 Subnet Mask . . . . . . . . . . . : 255.255.254.0 powershell-dev3 Default Gateway . . . . . . . . . : 10.2.30.1 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: