none
Bash to Powershell script convert RRS feed

  • General discussion

  • I need to convert a bash script into powershell:

    s#!/bin/bash

    if [ $# -ne 1 ]; then echo "Usage:" echo ${0##*/}" " echo exit 1 fi

    r=ss -ln6tp | grep -c ":::$1"

      if [ $r -ne 1 ]; then

      echo "Agent on port $1 is not listening."

      exit_code=2

    else

      echo "Agent listening on port $1"

      exit_code=0 fi

    exit $exit_code

    I don't have experience with bash at all and my powershell knowledge is basic.

    My idea for R varibale part:

    $r = netstat -ta | Select-String ":$arg"
    $r.count

    If statement is not big problem, but Exitcode is the main problem for me here.
    Any ideas please?

    • Changed type Bill_Stewart Wednesday, September 4, 2019 6:04 PM
    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:05 PM This isn't free language conversion service
    Monday, February 25, 2019 9:36 PM

All replies

  • This isn't a free script conversion forum, but if you can explain the purpose of the script perhaps we can provide some tips.

    -- Bill Stewart [Bill_Stewart]

    Monday, February 25, 2019 10:32 PM
  • This can be done with one line of PowerShell.

    You can just check the port to see if it is listening.

    This can also be done with a simple utility.

    https://www.microsoft.com/en-us/download/details.aspx?id=17148

    If you run this in PowerShell the $LASTEXITCODE is the exit code of the utility.


    \_(ツ)_/

    Monday, February 25, 2019 11:09 PM
  • You can also find a listening port like this:

    netstat -a | Where-Object{ $_ -match 'LISTENING' -and $_ -match ':5040'}


    \_(ツ)_/

    Monday, February 25, 2019 11:12 PM
  • We can also use the Net classes to get a ports status:

    [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().GetActiveTcpListeners().Where({$_.Port -eq 5040})


    \_(ツ)_/

    Monday, February 25, 2019 11:19 PM
  • We can also get by process ID and port and state.

    Get-NetTCPConnection -State Listen -OwningProcess 4 -LocalPort 445


    \_(ツ)_/

    Monday, February 25, 2019 11:29 PM
  • I'll try this tool. Thanks
    Tuesday, February 26, 2019 10:06 PM