Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a shell script to PowerShell

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

like image 674
dan Avatar asked Dec 08 '25 09:12

dan


1 Answers

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.

like image 148
Lee_Dailey Avatar answered Dec 11 '25 22:12

Lee_Dailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!