3

After switching an application from Linux to Windows, I need to convert a shell script to a Windows equivalent. My choices were basically batch and PowerShell and I decided to give a shot to PowerShell.

For anyone interested, it's a local check for Check_MK to get information about SoftEther installed version and the number of sessions with performance data.

The initial shell script was as follow:

#!/bin/sh
cmd=$(/usr/local/vpnserver/vpncmd localhost:port /server /password:password /in:/usr/lib/check_mk_agent/local/vpncmd.txt)
version=$(echo "$cmd" | head -4 | tail -1)
sessions=$(echo "$cmd" | grep Sessions | awk '$1=$1' | cut -c21-22)

if [ -z "$version" ]; then
 echo "3 VPN_Version - Can't get the information from vpncmd"
else
 echo "0 VPN_Version - SoftEther VPN Server $version"
fi

if [ -z "$sessions" ]; then
 echo "3 VPN_Sessions - Can't get the information from vpncmd"
else
 echo "P VPN_Sessions sessions=$sessions;2;2"
fi

I basically got everything working except the 2 hardest lines of code:

cd "C:\Program Files\SoftEther VPN Server"
$cmd = vpncmd localhost:port /server /password:password /in:vpncmd.txt
$version=
$sessions=

if($version -eq $null) {
    echo "3 VPN_Version - Can't get the information from vpncmd"
} else {
    echo "0 VPN_Version - SoftEther VPN Server $version"
}

if($sessions -eq $null) {
    echo "3 VPN_Sessions - Can't get the information from vpncmd"
} else {
    echo "P VPN_Sessions sessions=$sessions;2;2"
}

I need help with going from the head, tail, grep, awk and cut one liners to whatever is equivalent in PowerShell. I read about Get-Content but I'm not sure if it's the most efficient way to do this and would like to prevent going from 1 line definition to 10 lines if that's possible to be as efficient in PowerShell.

Sample output of vpncmd's output: https://pastebin.com/J5FcHzHK

9
  • it seems you want to pare the output of an exe ... so, please post the text in your $cmd variable. Commented May 29, 2019 at 12:56
  • For the first line echo "$cmd" | head -4 | tail -1, lets says you've a string array that is sent to the pipeline: Invoke-Expression "your command" | Select-Object -first 4 | Select-Object -Last 1 Commented May 29, 2019 at 12:59
  • @Lee_Dailey, the executable outputs hundreds of rows of information, the grep part allows me to get the only row with the word Sessions, while the version is always on the 3rd line, hence the head -4 | tail -1. Commented May 29, 2019 at 13:04
  • @Moerwald It's not an array, just rows of text, I should have specified. I added an example output. pastebin.com/J5FcHzHK Commented May 29, 2019 at 13:07
  • Just edited that pastebin link, a part was missing. If you CTRL+F Sessions it's there once at the end. Commented May 29, 2019 at 13:10

1 Answer 1

2

with the data being an array of lines & the word Version appearing multiple times in the actual source, the code needs to change a tad. in this version, it uses the way that -match works on an array to give the whole line as a result. that requires working on the output line to parse the desired data.

$Version = ($Vpncmd_Output -match '^Version \d{1,}\.\d{1,}' -split 'Version ' )[-1].Trim()
$SessionCount = [int]($Vpncmd_Output -match 'Number of Sessions\s+\|').Split('|')[-1].Trim()

$Version
$SessionCount

output ...

4.29 Build 9680   (English)
0

using the data in your PasteBin post, and presuming that is a multiline string, not an array of strings, this seems to work [grin] ...

$Vpncmd_Output -match '(?m)Number of Sessions\s+\|(?<Sessions>.*)'
$Matches.Sessions
# output = 0
$Vpncmd_Output -match '(?m)Version (?<Version>.+)'
$Matches.Version
# output = 4.29 Build 9680   (English)

i tried to combine the regex into one, but failed. [blush] the way i have it requires two passes, but it does work.

Sign up to request clarification or add additional context in comments.

15 Comments

Turns out it's probably an array of strings. I still tried to match the version with your expression and here is what I'm getting: imgur.com/a/NZoeOjJ
The word "version" is sadly there multiple times, hence why I was using shell to get the 3rd line instead. I guess it's even easier now that I know that it's an array.
The session part is working, except that it returns the full line rather than just the number at the end of the line. I was using cut in shell. I think I can figure out the rest from what you have provided, thank you for your help!! @Lee_Dailey
@dnLL - ah! i managed to miss your response earlier on string array or multiline string. [blush] is Version only at the start of the line when the number is there?
If you mean Session, the line is always Number of Sessions |0 with the number at the end (it's always the same number of spaces too, StackOverflow removes them but you can see in the PasteBin). If you mean Version, I'm really just looking for that 3rd line... Version 4.29 Build 9680 (English). But it's there multiple times in the output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.