Passing pwd from WSL2 to Powershell.exe - windows

I want to run Windows native PhpStorm from within WSL2 and ask it to open the current directory.
JetBrains Toolbox includes scripts to launch their applications from the command line (i.e. phpstorm.cmd). WSL2 supports running windows executables from WSL2, however, this does not seem to work with these scripts.
Therefore, I decided to try:
$ powershell.exe phpstorm.cmd
And this works fine and opens PhpStorm. I then need to tell PhpStorm to open the current working directory.
In Linux, this would be phpstorm . or in windows phpstorm.cmd .
I tried both of these:
$ powershell.exe phpstorm.cmd .
$ powershell.exe phpstorm.cmd pwd
$ powershell.exe -c "phpstorm.cmd $((pwd).Path)"
But neither passed the pwd as the context.
It seems to be an issue with UNC paths not being supported in CMD.EXE:
glen#Sisko:~/repos/gclark18/tdd-dailyjobs$ powershell.exe -c "phpstorm.cmd $((pwd).Path)"
-bash: command substitution: line 1: syntax error near unexpected token `.Path'
-bash: command substitution: line 1: `(pwd).Path'
'\\wsl$\Ubuntu\home\glen\repos\gclark18\tdd-dailyjobs'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
Can anyone advise how to do this please?

Related

How to call PowerShell script from WSL?

In my Windows directory
C:\Users\jholmes\pichak\analytics
I have run1.ps1 code.
I run wsl.exe, now my pwd is
/mnt/c/WINDOWS/system32
How to point to the first path and execute the script?
In WSL2, using the -File option worked for me:
powershell.exe -File path/to/script.ps1
When running wsl (or wsl.exe) from PowerShell, it should start in the same directory that you were in under PowerShell, just with the Linux version of it. For instance, if you are in PowerShell in C:\Users\jholmes\pichak\analytics, and you run wsl, you should end up in /mnt/c/Users/jholmes/pichak/analytics.
If not, then you may have something in your startup file already like the other answer recommend. You should remove any cd commands from your .bashrc, .zshrc, .profile, .bash_profile, or any other startup file you may have edited.
The wsl command provides a few handy arguments for specifying the starting directory:
wsl ~ will start in your Linux user's home directory (e.g. /home/jholmes)
wsl --cd c:\ will start in /mnt/c (or whatever the equivalent Linux directory is to the Windows version passed in).
wsl --cd \\wsl$\<distroname>\etc will start in your /etc directory (or whatever WSL path you passed in. Note that you will need to specific your distribution name (obtained from wsl -l -v).
Let's say that you are in /home/jholmes, and you want to execute the PowerShell script C:\Users\jholmes\pichak\analytics\run1.ps1. You first need to translate the Windows path to the Linux version:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
You can then run it via:
powershell.exe /mnt/c/Users/jholmes/pichak/analytics/run1.ps
It's also possible to set up a PowerShell script with a "shebang" line to execute it directly. If you add the following as the first line of run1.ps1:
#!/usr/bin/env -S powershell.exe -ExecutionPolicy Bypass
Then set it to be executable via chmod +x /mnt/c/Users/jholmes/pichak/analytics/run1.ps, then it becomes a "command" of sorts (technically, an "executable script") that you can execute directly by just typing the fully qualified name of the script at the command line:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
or
cd /mnt/c/Users/jholmes/pichak/analytics
./run1.ps
There are two ways to go about this:
You can change your working directory to that of your shell script and execute it normally. To do so, follow these steps:
Mount the relevant drive cd /mnt/c/.
Change directories according to the path of the script.
This approach is more of a hack that I use for the sake of convenience. I have created a folder in my Windows storage wherein I store all Ubuntu WSL related files. Say, D:\Ubuntu. To avoid changing the working directory every time you open WSL, you can modify the shell profile file (bashrc, zshrc etc.) to load the relevant directory at the end.
i.e., Add cd /mnt/d/Ubuntu/ at the end of your ~/.zshrc file if you use zsh or the relevant profile file otherwise.
Here is the evidence, that with the WSL2, our software can make the Powershell script encrypted and protected:
See this picture:
And it can detect and kill the process that uses fanotify because we think it can be used by attackers to hide critical file change (it also can find and kill dtrace/strace/stap/bpftrace/debuggers and process that opens /dev/kmem, /dev/mem, /proc/kcore, and the protected process' /proc/pid/mem etc):
This picture shows the encrypted PowerShell script now can detect and kill possible attackers' processes
For me this answer was almost the good one but when running for instance:
powershell.exe -File /mnt/c/path/to/script/script.ps1
I got following error:
The argument '/mnt/c/path/to/script/script.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
I then have to:
cd /mnt/c/path/to/script
powershell.exe -File script.ps1

why command are not recognized by the command prompt?

I have tried several times to type the command 'ls' at the command prompt of Windows 10 but it shows me that the command is not recognized as an internal or external command, an executable program or a batch file.
The "is" command is for Linux, not for Windows, and like Kraigolas said, the Windows OS command is "dir". Hope this helped.
The ls command is for linux and it's equivalent for windows command prompt is dir. But if you are more of a linux user then you can try using power-shell on windows where ls command works just like the way it works in linux. Power-shell is like a hybrid between windows command prompt and linux terminal.

How can I run a batch-file under windows git-bash console?

How can I run a batch-file uder windows git-bash console.
When I run it I get:
user#DESKTOP-DF012sh MINGW64 /c/project-folder
$ util.bat
bash: util.bat: command not found
It should work if you mention where the bat is.
./util.bat
By default, a $PATH does not include the current folder.
I just tested executing a .bat script that way, and it worked just fine.

Git for Windows: Difference between Git\bin\bash.exe and Git\usr\bin\bash.exe

From what I understand bin\bash.exe invokes usr\bin\bash.exe I guess with some options to help manage windows paths but can't find any documentation to indicate that.
All I know is my scripts seem not to work as expected if I have my environment set to use Git\usr\bin\ rather than Git\bin.
A new dev box seems to have this setup:
where bash
C:\Windows\System32\bash.exe
C:\Program Files\Git\usr\bin\bash.exe
I can understand wsl being top but can't understand the second entry if bin/bash.exe is the one I am supposed to use?
%windir%\system32\bash.exe = inline bash shell hosted by whatever windows subsystem for linux [ wsl ] environment you have enabled using microsoft store | windows subsystem for linux | | install. An alternative but essentially same result as the separate window that start menu shortcut %windir%\system32\wsl.exe ~ -d Ubuntu launches.
%programfiles%\git\bin\bash.exe -> %programfiles%\git\usr\bin\bash.exe + some automatically injected arguments = inline [ main stdin/stdout ] bash shell provided by git layered on top of windows cmd.exe command prompt environment
%programfiles%\git\git-bash.exe = windows app [ winmain ] bash shell provided by git layered on top of windows cmd.exe command prompt environment
So it appears that:
"C:\Program Files\Git\usr\bin\bash" -li
yields:
Me#MYPC MSYS /usr/bin
and
"C:\Program Files\Git\bin\bash" -li
yields:
Me#MYPC MINGW64 /usr/bin
So to my eyes that implies the EXE are compiled differently.
But in my case the issues I was having with my scripts was purely down to the PATH each exe sets up:
For
"C:\Program Files\Git\usr\bin\bash.exe"
$ where FIND
C:\Windows\System32\find.exe
C:\Program Files\Git\usr\bin\find.exe
Whereas:
"C:\Program Files\Git\bin\bash.exe"
$ where FIND
C:\Program Files\Git\usr\bin\find.exe
C:\Windows\System32\find.exe
So my script in the former case was failing with a
FIND: Parameter format not correct
as it wasn't finding the correct version of find.

WebStorm mocha test 'sh' is not recognized as an internal or external command

I'm trying to run a mocha test for my node.js code which runs the following line:
exec(`sh ${scriptFile}`);
When I run it from the command line (Git Bash) using npm test, it passes. However, when I run it from WebStorm I get the following error:
'sh' is not recognized as an internal or external command,
operable program or batch file.
I feel like I'm probably missing some WebStorm setting, but I can't figure out what it is. Any ideas?
P.S. I'm on Windows.
For those who are using phpStorm on Windows and come here by Google
Install Git on your machine. Go to Settings/Terminal and set shell path to C:\Program Files\Git\bin\sh.exe
Also add C:\Program Files\Git\bin to your PATH in environment variables.
Git Bash implements a *nix-esque shell simulating bash which can parse your command. Webstorm appears to be trying to execute your command in either powershell or cmd, which do not support sh syntax.
Open the Terminal page of the Settings/Preferences dialog, and configure the Shell path field as follows:
"[path to the git installation]\bin\sh.exe" -login -i
This will probably be "C:\Program Files\Git\bin\sh.exe" -login -i
Source: https://www.jetbrains.com/help/webstorm/2017.1/working-with-embedded-local-terminal.html

Resources