title: Converting a shell script to PowerShell
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>The initial shell script was as follow:</p>
  
  
  
  <pre><code>#!/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
  </code></pre>
  
  <p>I basically got everything working except the 2 hardest lines of code:</p>
  
  <pre class="lang-bsh prettyprint-override"><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"
  }
  </code></pre>
  
  <p>I need help with going from the <code>head</code>, <code>tail</code>, <code>grep</code>, <code>awk</code> and <code>cut</code> one liners to whatever is equivalent in PowerShell. I read about <code>Get-Content</code> 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.</p>
  
  <p>Sample output of <code>vpncmd</code>'s output: <a href="https://pastebin.com/J5FcHzHK" rel="nofollow noreferrer">https://pastebin.com/J5FcHzHK</a></p>