-2

this is my script to telnet multiple IP addresses

$server_list = @('1.1.1.1:443', '10.100.8.22:3389', '10.100.8.21:22')
Foreach ($t in $server_list)
{
  $source = $t.Split(':')[0]
  $port = $t.Split(':')[1]
  Write-Host "Connecting to $source on port $port" | Out-File 'output.txt' -Append
  try
  {
    $socket = New-Object System.Net.Sockets.TcpClient($source, $port)
  }
  catch [Exception]
  {
    Write-Host $_.Exception.GetType().FullName | Out-File 'output.txt' -Append
    Write-Host $_.Exception.Message | Out-File 'output.txt' -Append
  }
  Write-Host "Connected`n" | Out-File 'output.txt' -Append
}

i want this output to be greean line when status is connected???

  • The Write-Host cmdlet allows you to set any color allowed via its *color switches. These are documented in the PS cmdlet help file(s) and MS PowerShell docs for Write-Host. Why are you not using ***Test-NetConnection*** for this use case? It has a port switch for this. Unless you are on an older version of PS, which would be a reason to use the .Net namespace. If you are on an older PS version, then Write-Host empties the buffer, so there is nothing to output. You cannot pipe Write-Host results to anything, and it can/will cause issues if you try. – postanote Sep 10 '22 at 07:16
  • [`Write-Host` is typically the wrong tool to use](http://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/), unless the intent is to write _to the display only_, bypassing the success output stream and with it the ability to send output to other commands, capture it in a variable, or redirect it to a file. To output a value, use it _by itself_; e.g., `$value` instead of `Write-Host $value` (or use `Write-Output $value`). See [the linked duplicate](https://stackoverflow.com/q/60532805/45375) for more information. – mklement0 Sep 10 '22 at 12:45
  • In short: you need to use `Write-Output` rather than `Write-Host`, or you can use the output strings / expressions as-is, which is like using `Write-Output` _implicitly_. – mklement0 Sep 10 '22 at 12:47

1 Answers1

0

As per my comment. This line is invalid.

Write-Host "Connecting to $source on port $port" | Out-File 'output.txt'

It will simply error on any PS version. Now, on a file, it would just be empty. So, that is the reason I am showing a string as a reference error. If you'd use Write-Output, then the file would get populated, but then you'd need another Write-Host to show the screen text in color or use ANSI code.

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
...

Write-Host 'Left side of the pipeline' | Out-String 'Right side of the pipeline'
Out-String : A positional parameter cannot be found that accepts argument 'Right side of the pipeline'.
...

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.6
...

 Write-Host 'Left side of the pipeline' | Out-String 'Right side of the pipeline'
Out-String: A positional parameter cannot be found that accepts argument 'Right side of the pipeline'.

BTW: There already exist many PowerShell port scanner scripts all over the web to leverage as is or learn from.

However, here is a quick refactor of your posted code.

$Error.Clear()
Clear-Host
'1.1.1.1:443', '10.100.8.22:3389', '10.100.8.21:22' | 
ForEach-Object {
    If ((Test-NetConnection -ComputerName ($PSitem -split(':'))[0] -Port ($PSitem -split(':'))[1]).TcpTestSucceeded -eq 'True')
    {
        Write-Host "Connection successful to $(($PSItem -split ':')[0]) on port $(($PSItem -split ':')[1])" -ForegroundColor Green
        "Connection successful to $PSItem" | 
        Out-File -FilePath 'D:\temp\PortScanOutput.txt' -Append
    }
    Else 
    {
        Write-Warning "Connection error to $PSitem"

        "Connection failed to $PSItem", 
        ((($Error)[0]).GetType()).FullName, 
        ((($Error)[0]).Exception.Message) | 
        ForEach-Object {
            $PSitem  | 
            Out-File -FilePath 'D:\temp\PortScanOutput.txt' -Append
        }
    }
}
# Results
<#
Connection successful to 1.1.1.1 on port 443
WARNING: TCP connect to (10.100.8.22 : 3389) failed
WARNING: Ping to 10.100.8.22 failed with status: TimedOut
WARNING: Connection error to 10.100.8.22:3389
WARNING: TCP connect to (10.100.8.21 : 22) failed
WARNING: Ping to 10.100.8.21 failed with status: TimedOut
WARNING: Connection error to 10.100.8.21:22
#>

Live demo: enter image description here

postanote
  • 15,138
  • 2
  • 14
  • 25