Forum Discussion

Gaurav_Kabra_25's avatar
Gaurav_Kabra_25
Icon for Nimbostratus rankNimbostratus
Mar 08, 2016
Solved

Call Powershell Script from Unix Shell Script

Hi All,

 

Trust you're all having a great day.

 

I have a powershell script on my Windows server which does some active directory operations. I wish to call that script from another Linux machine using a shell script. I have installed powershell server on my Windows server and can communicate with my linux machine using key based ssh authentication. The issue is that my script just stops once the command for ssh to Windows server is executed.

 

My shell script below:

 

!/bin/bash

echo "script run" GROUP=$1 ACCOUNT=$2 ssh Administrator@ sleep 10 echo "\scriptname.ps1 -group $GROUP -account $ACCOUNT" sleep 5 echo "exit" exit

 

I have tried without the echo command as well and it still hangs. Any idea what I must be doing wrong here? How can I make it work please?

 

Regards, Gaurav

 

  • Tried the operation using an "expect" script and it all works well now.

     

3 Replies

  • I am assuming you actually have a server after the @. I suspect what's happening is that you are running sleep on the windows server, rather than the \scriptname.ps1 file. Try something like this:

     

    GROUP=$1 ACCOUNT=$2 ssh Administrator@ "\scriptname.ps1 -group $Group -account $ACCOUNT"

     

    SSH should take care of the delay while logging in and the exit on completion of the command natively.

     

  • Tried the operation using an "expect" script and it all works well now.

     

    • Chris_Grant's avatar
      Chris_Grant
      Icon for Employee rankEmployee
      Expect is an awesome tool. Would you be willing to share the code you used so the community can benefit?