title: Simple script to ping PCs, output table - what's wrong?
body: <p>I was trying to write a simple script to ping a list of computers, and output the result in a table. I ended up doing this, which works fine:</p>
  
  <pre><code>Get-ADComputer -searchbase "OU=Materials,OU=MMC Computers,OU=REI,DC=REIDOMAIN,DC=LOCAL" -filter * | select -expand name | % {
      $output = New-Object PSObject
      $output | Add-Member NoteProperty "Computer name"($_)
      $output | Add-Member NoteProperty "Ping result"("$($(Test-Connection $_ -count 1 -quiet).ToString())")
      write $output }
  </code></pre>
  
  <p>However, I'd like to understand why my first two attempts didn't work, so that I can avoid making the same mistakes later. Here they are:</p>
  
  <p><strong>Script 1: <em>See edit below</em></strong></p>
  
  <pre><code>Get-ADComputer -searchbase "OU=Materials,OU=MMC Computers,OU=REI,DC=REIDOMAIN,DC=LOCAL" -filter * | 
  select -expand name | % { $computer = $_; $obj = "" | 
  select @{Name="Computer";Expression="$computer"},`
  @{Name="Pingable";Expression="$($(Test-Connection $computer -count 1 -quiet).ToString())"} 
  $obj }
  </code></pre>
  
  <p><strong>Output 1:</strong></p>
  
  <pre><code>Computer                                 Pingable
  -----—                                   -----—
  </code></pre>
  
  <p><em>Note:</em> Under the table headers, this script actually prints one blank line for each computer I'm pinging.</p>
  
  <p><strong>Script 2:</strong></p>
  
  <pre><code>$table = @{Expression={$_};Label="Computer"},@{Expression={"$($(Test-Connection $_ -count 1 -quiet).ToString())"};Label="Pingable"}
  Get-ADComputer -searchbase "OU=Materials,OU=MMC Computers,OU=REI,DC=REIDOMAIN,DC=LOCAL" -filter * | select -expand name | format-table $table
  </code></pre>
  
  <p><strong>Output 2:</strong></p>
  
  <pre><code>mickeymouse
  goofy
  minnie
  pluto
  frank
  </code></pre>
  
  <p>This one doesn't even output a table...it just prints one computer name per line.</p>
  
  <p>I'd appreciate if someone could explain what's going wrong in these two attempts. Thanks!    </p>
  
  <p><strong>Edit:</strong> Got Script 1 to work. </p>
  
  <pre><code>Get-ADComputer -searchbase "OU=Materials,OU=MMC Computers,OU=REI,DC=REIDOMAIN,DC=LOCAL" -filter * | 
  select @{Name="Computer";Expression={$_.Name}},@{Name="Pingable";Expression={"$($(Test-Connection $_.Name -count 1 -quiet).ToString())"}}; 
  </code></pre>
  
  <p>Still curious about Script 2</p>