Search code examples
powershellshellsshservercygwin

how to pass parameter to a powershell script when running from a shell script through cygwin?


i have sh script let say a.sh in which i am doing a ssh to windows server (configured with cygwin) and running a B.ps1 script which takes the parameter defined in a.sh.

Content of a.sh:

var1="abc"
var2="xyz"

#sshing to windows box
/usr/bin/scp -r -q /home/$user/Jenkins/workspace/job/jobname $user2@$x:/cygdrive/C/
/usr/bin/ssh $user2@$x 'powershell C:\\B.ps1 $var1 $var2'

this is running the script but without any parameters, when i write host variable name in B.ps1, i get blank output, which means the var1&var2 values are not getting passed to my ps1 script.

Content of B.ps1:

$var1=$args[0]
$var2=$args[1]

Write-Host "var1 is:" $var1
Write-Host "var2 is:" $var2

i have tried to use double quotation in my sh script , didn't work, it seems like there must be some way i can pass parameter but may be missing out anything on syntax.

please help.


Solution

  • /usr/bin/ssh $user2@$x "powershell C:/B.ps1 $var1 $var2"
    
    • You need double quoting ("...") rather than single quoting ('...') in order for the shell-variable references $var1 and $var2 to be expanded.

    • By using / as the path separator - which PowerShell accepts interchangeably with \ - you avoid the need to escape \ characters, which you would have had to double on transitioning from '...' to "..."; that is, the following would have worked too:
      /usr/bin/ssh $user2@$x "powershell C:\\\\B.ps1 $var1 $var2"