Executing Bash Scripts Withinfrom Powershell

Sometimes you have processes which use multiple systems and if you are real lucky they cover multiple OS flavors, in this case I had a process which needs to make changes on Windows and Linux servers. Since Powershell was relatively new to me, I thought why not start with that. The Windows part was easily covered, but the Linux part, well euhm, takes a bit more effort. Unfortunately it is not possible (at least I don’t know how) to execute commands directly from Powershell within the Linux servers, so we had to create a workaround. It is easy to do this with the help of “ plink”, plink is a backend tool for Putty and makes it possible to run SSH commands on a command line bases. Now we have tool which can execute commands through SSH within a Linux machine, it is relatively easy :-) See the example below. The bash script (helloworld.sh) which we want to execute:

#!/bin/bash

function helloworld(){
  for i in {0..9}
    do
      echo "Hello World!";
    done
}
helloworld;

The Powershell script (testbash.ps1) which executes the bash script:

#Invoke-Expression -Command "& 'C:Program Files (x86)PuTTYplink.exe' root@server ~/helloworld.sh"
Rob Maas
Rob Maas
Technical Challanger at ON2IT

If it is broken, fix it! If it ain’t broken, make it better!

Related