I have to write a Port Scanner for school, All look to work fine, but -ErrorAction Ignore doesnt work, errors are showing up.
function portScanner($xhost){
$range=1..1024
foreach($port in $range){
$connection = New-Object System.Net.Sockets.TCPClient($xhost, $port) -ErrorAction Ignore
if($connection.Connected){
Write-Host "Port $port is open!" -ForegroundColor Magenta
}
elseif(!($connection.Connected)){
Write-Host "Port $port is closed" -ForegroundColor Red
}
}
}
portScanner($env:COMPUTERNAME)
New-Object : Exception calling ".ctor" with "2" argument(s): "No connection could be made because the target machine actively refused it 192.168.10.10:1"
At C:\Users\Administrator\Documents\Untitled3.ps1:4 char:23
+ ... onnection = New-Object System.Net.Sockets.TCPClient($xhost, $port) -E ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Port 1 is closed