title: How to check open ports using powershell?
body: <p>I would like to write a script to check radom IP or hostname to see if ports are open. Here is what I have so far. The scripts name is checkports.</p>
  
  <pre><code>foreach ($xhost in $computername){
      Write-Host $xhost
      foreach ($port in $ports) {
          $Socket = New-Object System.Net.Sockets.TCPClient            
          $Connection = $Socket.BeginConnect($xhost,$port,$null,$null) 
  
          $Connection.AsyncWaitHandle.WaitOne(5000,$false) | out-null
  
          if ($Connection -eq $true)
              { write-host = "$xhost port $port is open" }
          else
              { write-host = "port $port is closed" }      
  
          $Socket.EndConnect($Connection)
          $Socket.Close()
     }
  }
  </code></pre>
  
  <p>I would like to input values in the following way:
  .\checkport '192.186.1.5' 
  or
  '192.168.1.5', '192.168.1.105', 192.168.1.110' | checkport</p>
  
  <p>It doesn't seem to be reading IP address or displaying results. </p>
  
  <p>I was wondering if anyone could point out there could show me what I am doing wrong in with this script?</p>