Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, September 3, 2015 7:05 PM
Hi,
Is there a way to have Test-NetConnection not perform a ping test ? I am only trying to check if a specific port is opened and can't seem to find the option to remove the ping test.
RemotePort : 53
InterfaceAlias : Ethernet
SourceAddress : 10.1.0.30
PingSucceeded : False
PingReplyDetails (RTT) : 0 ms
TcpTestSucceeded : True
Above a typical case where I know ping will fail and I'd rather not spend the time waiting for a timeout or getting a nifty warning message especially when running this in a bigger script. When you are testing thousands of firewall rules, the ping test requires time which increases the runtime of the script (by a LOT).
Regards,
All replies (5)
Thursday, September 3, 2015 7:13 PM âś…Answered
Hi,
Based on the docs, it looks like the answer is no:
https://technet.microsoft.com/en-us/library/dn372891(v=wps.630).aspx
I think I have something in my script vault that might help you. I'll post back if I find it.
EDIT: This is what I've used in the past. Been quite a while since I've needed it:
$servers = 'Server1','Server2','Server3','Server4'
$portToCheck = '80'
foreach ($server in $servers) {
try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}
catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
New-Object PsObject -Property $props
}
Thursday, September 3, 2015 7:35 PM | 1 vote
Thanks, thanks for confirming I did not mis read that TechNet doc. Might have been a hidden parameter :) It would be nice if they made it a parameter.
And thanks for the script, should be faster than the current way I do it.
Regards,
Edit:
$commandTCP = {
param($1,$2);
try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $1,$2;
$true;
} catch {
$false;
}
}
is faster than:
$commandTCP = {
param($1,$2);
test-netconnection -computername $1 -port $2 -informationlevel quiet;
}
at least for me
Sweet and thanks again
Friday, September 4, 2015 2:01 AM
Cheers, you're very welcome. Glad I could help out.
Tuesday, October 4, 2016 5:13 PM
Hi,
Based on the docs, it looks like the answer is no:
https://technet.microsoft.com/en-us/library/dn372891(v=wps.630).aspx
I think I have something in my script vault that might help you. I'll post back if I find it.
EDIT: This is what I've used in the past. Been quite a while since I've needed it:
$servers = 'Server1','Server2','Server3','Server4' $portToCheck = '80' foreach ($server in $servers) { try { $null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck $props = @{ Server = $server PortOpen = 'Yes' } } catch { $props = @{ Server = $server PortOpen = 'No' } } New-Object PsObject -Property $props }
Nice - you can also use this as a function, so that it can be used just as test-netconnection is.
function Query-TCPPort {
param([string]$Server, [int]$Port, [int]$Timeout=100)
$Connection = New-Object System.Net.Sockets.TCPClient
try {
$Connection.SendTimeout = $Timeout
$Connection.ReceiveTimeout = $Timeout
$Connection.Connect($Server,$Port)
return $true
}
catch {
return $false
}
}
#Example:
Query-TCPPort -Server www.google.com -Port 80
EDIT: I just learned timeout is ignored unless we're actually sending data. :(
Mike Crowley | MVP
My Blog -- Baseline Technologies
Thursday, October 6, 2016 11:16 AM
Hi Mike,
Thanks for the info on the timeout.
And true I could use it as a function. But what I am building is actually a tool to test/check the ACL rules remotely. So I have to send the port testing to the source server via an invoke-command which makes the function a bit un-necessary in my case.
Cheers