CTP2, Running Console Apps against multiple computers
June 8, 2008 at 5:06 AM
—
Andy Schneider
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.
6450a6e0-c0ac-414d-a0b0-2af0fc12f1ee|0|.0
Posted in:
Tags: