Schedule a TCP test and do some actions

Welcome Forums General PowerShell Q&A Schedule a TCP test and do some actions

Viewing 10 reply threads
  • Author
    Posts
    • #211890
      Participant
      Topics: 2
      Replies: 8
      Points: 15
      Rank: Member

      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.

      1) 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.

      2) 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.

      Thank you a lot!

       

      • This topic was modified 3 months, 1 week ago by Barabba11.
      • This topic was modified 3 months, 1 week ago by Barabba11.
    • #211905
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      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.

      Thanks

      https://social.technet.microsoft.com/Forums/en-US/72fd383d-1784-4acc-b53f-ca561e74d866/schedule-a-tcp-test-and-do-some-actions?forum=winserverpowershell

    • #211908
      Participant
      Topics: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero
      $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
      }
      
      • This reply was modified 3 months, 1 week ago by Sam Boutros.
      • This reply was modified 3 months, 1 week ago by Sam Boutros.
    • #212103
      Participant
      Topics: 2
      Replies: 8
      Points: 15
      Rank: Member

      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!

    • #212163
      Participant
      Topics: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero
      #region Input
      
      $IPv4Address = '192.168.0.102'
      $PortNumber = 1111
      $FirewallRuleName = 'Block .0.102'
      $WaitTimeSec = 180
      
      #endregion
      
      
      #region Process
      
      While ($true) {
      
          #region Test 1
          try {
              $Result = Test-NetConnection -ComputerName $IPv4Address -port $PortNumber -EA 1
              if ($Result.TcpTestSucceeded) {
                  “Computer $IPv4Address port $PortNumber is open”
                  Remove-NetFirewallRule -DisplayName $FirewallRuleName
              } else {
                  “Computer $IPv4Address port $PortNumber is NOT open”
                  New-NetFirewallRule -DisplayName $FirewallRuleName -Direction Inbound -RemoteAddress $IPv4Address -Action Block
              }
          } catch {
              $_.Exception.Message
          } 
          #endregion
      
          #region Test 2
          try {
              $Result = <Some cmdlet> -EA 1
              <some code>
          } catch {
              $_.Exception.Message
          } 
          #endregion
      
          "Waiting $WaitTimeSec seconds, hit CTRL-C to exit script.."
          Start-Sleep -Seconds $WaitTimeSec
      
      } 
      
      #endregion
      

      I recommend that you;
      – Break your script into regions for easier readability
      – Separate the data from the code as much as possible – put all data in an ‘Input’ region on top
      – Try to write psudo-code before you code your script. That’s the logic flow of the script in English, like:

      Perform Test 1
      Perform Test 2
      Wait 180 sec
      Repeat
      

      Then you start coding by making a region called ‘Perform Test 1’ where you code that task, and so on..

    • #212166
      Participant
      Topics: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero

      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!

      See this post for formatting code in your posts in this forum https://powershell.org/forums/topic/read-me-before-posting-youll-be-glad-you-did/

    • #212169
      Participant
      Topics: 2
      Replies: 8
      Points: 15
      Rank: Member

      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:

      $n = 5
      while ( $n -gt 0 ) {

      “******Cam1 Test******”
      $Result = Test-NetConnection -ComputerName 192.168.2.1 -port 554 -EA 1
      if ($Result.TcpTestSucceeded) {
      “Cam1 Funziona”
      Remove-NetFirewallRule -DisplayName “Blocca Cam1”
      } else {
      “Cam1 RIAVVIO”
      New-NetFirewallRule -DisplayName “Blocca Cam1” -Direction Inbound -RemoteAddress 192.168.2.1 -Action Block
      }

      ……

      “******Cam7 Test******”
      $Result = Test-NetConnection -ComputerName 192.168.2.7 -port 554 -EA 1
      if ($Result.TcpTestSucceeded) {
      “Cam7 Funziona”
      Remove-NetFirewallRule -DisplayName “Blocca Cam7”
      } else {
      “Cam7 RIAVVIO”
      New-NetFirewallRule -DisplayName “Blocca Cam7” -Direction Inbound -RemoteAddress 192.168.2.7 -Action Block
      }

      Start-Sleep -s 180
      cls
      }

    • #212178
      Participant
      Topics: 2
      Replies: 8
      Points: 15
      Rank: Member

      thank you for help! unfortunately we posted at same time and I couldn’t see your psto before 🙂 I’m pleasant to see you had the same ideas :))

    • #212340
      Participant
      Topics: 2
      Replies: 8
      Points: 15
      Rank: Member

      I would ask again help, if I can,

      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:

      function Test-Port($server, $port) {
      $client = New-Object Net.Sockets.TcpClient
      try {
      $client.Connect($server, $port)
      $true
      } catch {
      $false
      } finally {
      $client.Dispose()
      }
      }

      Test-Port (127.0.0.1,8000)
      I’m sure the port 8000 is opened on local host, thank you for help

    • #212343
      Participant
      Topics: 5
      Replies: 2368
      Points: 5,987
      Helping Hand
      Rank: Community MVP

      I would ask again help, if I can,

      If you have a new question create a new thread next time, please. Thanks.

      … Windows7, even if I upgraded Powershell to version 5 I can’t use the command Test-NetConnection,

      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.

    • #212535
      Participant
      Topics: 2
      Replies: 8
      Points: 15
      Rank: Member

      I’ve found my solution here 🙂

      ANy alternative to “Test-NetConnection” under Win7?

Viewing 10 reply threads
  • The topic ‘Schedule a TCP test and do some actions’ is closed to new replies.