Hi mates, I need your help, thanks to anyone who can write some suggestions here.
My goal is to test a TCP port on a remote device, if it doesn’t work it will execute a command to filter out the ICMP (ping) reply till the TCP will answer again (the device will fail to ping and reboot itself, the TCP will become available again). I think I can use Powershell but as a newbie I don’t know how to implement the script commands.
I figured out I can use for example test-netconnection -computername 192.168.0.1 -port 80 for example, but how can I anaylze the result and depending on it execute the next command?
my goal is to execute one of those lines: New-NetFirewallRule -DisplayName “Test block this IP” -Direction Inbound -RemoteAddress 192.168.0.102 -Action Block in case the TCP test fail Remove-NetFirewallRule -DisplayName “Test block this IP” in case the TCP test succeed.
I need to schedule this every minute, for every IP I need to test i can create a different script file, I can use the windows schedule but how I can execute the script? Also I have to execute it with administrator privileges.
When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.
$ComputerName = '192.168.0.1'
$PortNumber = 80
try {
$Result = Test-NetConnection -ComputerName $ComputerName -port $PortNumber -EA 1
if ($Result.TcpTestSucceeded) {
"Computer $ComputerName port $PortNumber is open"
# Some other commands
} else {
"Computer $ComputerName port $PortNumber is NOT open"
# Some other commands
}
} catch {
$_.Exception.Message
}
Thank you mate!! You gave me the solution :))
I’ve created a file named script.ps1 with the following code, then I’ve created a .bat file with the execution command powershell <path>/script.ps1 and I’ve created a shortcut for the .bat file to execute it as administrator.
I would use the windows scheduler but it supports a minimum of 5 minutes, and I need to do the test every 3 minutes, can I please ask you to show me how can I add multiple checks in one script, add a pause and start from the begin? Thank you a lot!
try { $Result = Test-NetConnection -ComputerName 192.168.0.102 -port 1111 -EA 1 if ($Result.TcpTestSucceeded) { “Computer $ComputerName port $PortNumber is open” Remove-NetFirewallRule -DisplayName “Block .0.102” } else { “Computer $ComputerName port $PortNumber is NOT open” New-NetFirewallRule -DisplayName “Block .0.102” -Direction Inbound -RemoteAddress 192.168.0.102 -Action Block } } catch { $_.Exception.Message } Here I need to add a second check as before, then pause for 180 seconds, then loop to the begin. Thank you a lot for help!
[quote quote=212103]Thank you mate!! You gave me the solution :))
I’ve created a file named script.ps1 with the following code, then I’ve created a .bat file with the execution command powershell <path>/script.ps1 and I’ve created a shortcut for the .bat file to execute it as administrator.
I would use the windows scheduler but it supports a minimum of 5 minutes, and I need to do the test every 3 minutes, can I please ask you to show me how can I add multiple checks in one script, add a pause and start from the begin? Thank you a lot!
I’ve tried to find a way myself, I’m using a while with a statement to loop forever, I’m using a start-sleep for pause, a cls to don’t leak with RAM after months of usage. I hope it’s correct:
the PC i’m going to install there only for testing is small win Windows7, even if I upgraded Powershell to version 5 I can’t use the command Test-NetConnection, I googled for an alternative but the following script returns me always false, I’ve created a script like this and executed:
If you have a new question create a new thread next time, please. Thanks.
Despite the fact that Windows 7 is not longer supported anymore - the availablitity of certain cmdlets depends on the underlying system - not just on the version of Powershell. So you’re out of luck in the case I’m afraid. Sorry.