2

I have build a powershell script to run commands remotely. Nothing really to it, just open a session, invoke-command, do some logging and exit. That script is called remoteExecute.ps1

It is working fine as intended when I run it the outputs are what to be expected.

I'm having trouble running shell scripts using bash command. I have a server on which I installed WSL2 Linux distribution Ubuntu 20. When I log in on the server I can open a terminal window and run the bash command to execute a shell script without issues.
BUT, when I call this remotely from the powershell script it returns an error:

PS D:\batch> .\remoteExecute.ps1 -computer myserver -command "bash -c /mnt/c/somefile.sh"**

Opening new section on remote computer myserver

**The term 'bash -c myshellscript.sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.**
    + CategoryInfo          : ObjectNotFound: (bash -c /mnt/c/somefile.sh:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : myserver

Executed: 'bash -c /mnt/csomefile.sh'

Closed session

Powershell version used on both machines is v7.1 and I defaulted it to windows by setting this registry key

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon' -name Shell -Value 'C:\Program Files\PowerShell\7\PowerShell.exe -noExit'

What am I missing?

Rui Claro
  • 53
  • 5
  • Why do you expect Bash to be installed on the remote server? Clearly it's running PowerShell, too. – tripleee Jun 17 '21 at 12:34
  • Or do you not understand the difference? This is like calling a number in Vladivostok and being annoyed that they speak Russian. – tripleee Jun 17 '21 at 12:37
  • @tripleee, I do understand the diference. The remote server has WSL Ubuntu installed. If I run that same command on the remote server in a powershell or command shell it runs perfectly. – Rui Claro Jun 17 '21 at 13:52
  • The error message clearly comes from PowerShell. I'm guessing it's trying to say don't put double quotes around `bash` and its arguments. – tripleee Jun 17 '21 at 14:01
  • @tripleee I removed the double quotes but is has the same effect. Tried without quotes and it returns an error saying it does not understand the parameters "Parameter cannot be processed because the parameter name 'c' is ambiguous. Possible matches include: -computer -command." . Tried also with single quotes but it did not work. – Rui Claro Jun 17 '21 at 17:23

0 Answers0