gpt4 book ai didi

How to call PowerShell script from WSL?(如何从WSL调用PowerShell脚本?)

转载 作者:bug小助手 更新时间:2023-10-25 19:02:19 27 4
gpt4 key购买 nike



In my Windows directory

在我的Windows目录中


C:\Users\jholmes\pichak\analytics

I have run1.ps1 code.
I run wsl.exe, now my pwd is

我已经运行了1.ps1代码。我运行wsl.exe,现在我的PWD是


   /mnt/c/WINDOWS/system32

How to point to the first path and execute the script?

如何指向第一条路径并执行脚本?


更多回答

Hey did you try traversing back? cd .. from your PWD and traverse to \Users\jholmes\pichak\analytics ? Also how do you want to execure .ps1 scripts in linux?

嘿,你有没有试着往回走?光盘..。从您的PWD遍历到\USERS\jholmes\Pichak\analytics?另外,您希望如何在Linux中执行.ps1脚本?

@AvidJoe Impossible,I am still in Linux after cd ..

@AvidJoe Impact,CD之后我还在Linux中..

I'm not sure i understand your question. If you are already inside Ubuntu or any subsystem, the mount point for accessing windows directories is mnt/ you can traverse to any windows volume from the mnt/ for example. /mnt/c/Users/jholmes/pichak/analytics would be accessible from your linux subsystem. Im not sure what you mena when you say i run wsl.exe. If this isnt the case I might be completely wasting your time as well. lol.. sorry

我不太明白你的问题。如果您已经在Ubuntu或任何子系统中,访问Windows目录的挂载点是mnt/,例如,您可以从mnt/遍历到任何Windows卷。/mnt/c/USERS/jholmes/pichak/analytics可以从您的Linux子系统访问。我不确定你说我运行wsl.exe是什么意思。如果不是这样的话,我可能也完全是在浪费你的时间。哈哈..。抱歉的

If you wish to run windows tools (like powershell) from Linux please see : learn.microsoft.com/en-us/windows/wsl/… , if you wish to do it vice versa please see learn.microsoft.com/en-us/windows/wsl/… . in your case going back with cd and running with powershell <path>.

如果您希望在linux上运行Windows工具(如PowerShell),请参阅:learn.microsoft.com/en-us/WINDOWS/wsl/…,反之亦然,请访问learn.microsoft.com/en-us/WINDOWS/wsl/…。在您的情况下,使用CD返回并使用PowerShell<路径>运行。

优秀答案推荐

In WSL2, using the -File option worked for me:

在WSL2中,使用-File选项对我有效:


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.

当从PowerShell运行WSL(或wsl.exe)时,它应该在PowerShell下的同一目录中启动,只不过是它的Linux版本。例如,如果您在PowerShell中的C:\USERS\jholmes\Pichak\analytics中运行WSL,那么您最终应该得到/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.

如果没有,那么您的启动文件中可能已经有一些内容,就像另一个答案建议的那样。您应该从.bashrc、.zshc、.PROFILE、.bash_PROFILE或您可能编辑过的任何其他启动文件中删除所有CD命令。


The wsl command provides a few handy arguments for specifying the starting directory:

WSL命令为指定起始目录提供了几个方便的参数:



  • 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:

假设您在/home/jholmes中,并且希望执行PowerShell脚本C:\USERS\jholmes\pichak\analytics\run1.ps1。您首先需要将Windows路径转换为Linux版本:


/mnt/c/Users/jholmes/pichak/analytics/run1.ps

/mnt/c/USERS/jholmes/pichak/analytics/run1.ps


You can then run it via:

然后,您可以通过以下方式运行它:


powershell.exe /mnt/c/Users/jholmes/pichak/analytics/run1.ps

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:

还可以设置一个带有“shebang”行的PowerShell脚本来直接执行它。如果将以下代码添加为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:

然后通过chmod+x/mnt/c/Users/jholmes/pichak/analytics/run1.ps,将其设置为可执行,然后它将成为某种类型的“命令”(从技术上讲,是“可执行脚本”),您只需在命令行中键入脚本的完全限定名即可直接执行:


/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:

有两种方法可以做到这一点:



  1. 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.



  1. 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.



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


None of these answers are correct. powershell.exe is a Windows program so expects a Windows path as an argument.

这些答案没有一个是正确的。Powershell.exe是一个Windows程序,因此需要Windows路径作为参数。


powershell.exe -File C:\\Users\\jason\\Documents\\WindowsPowerShell\\Scripts\\Write-EventLog-1777.ps1

As the backslash character is used for escaping in Linux, you need to escape the backslash with another backslash.

由于在Linux中使用反斜杠字符进行转义,因此需要使用另一个反斜杠对反斜杠进行转义。



Here is the evidence, that with the WSL2, our software can make the Powershell script encrypted and protected:
See this picture:

这里有证据表明,有了WSL2,我们的软件可以对PowerShell脚本进行加密和保护:请参见下图:


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

此外,它还可以检测并杀死使用fantify的进程,因为我们认为攻击者可以使用它来隐藏关键的文件更改(它还可以找到并杀死dtrace/strace/stap/bpftrace/buggers和打开/dev/kmem、/dev/mem、/proc/kcore和受保护进程的进程‘/proc/id/mem等):此图片显示加密的PowerShell脚本现在可以检测和杀死可能的攻击者的进程


更多回答

powershell.exe -F ~/win/common/utils/fix_wsl.ps1 gives me an Error saying The argument '/home/user/win/common/utils/fix_wsl.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.

Powershell.exe-F~/win/Common/utils/fix_wsl.ps1给我一个错误,指出-File参数的参数‘/home/user/win/Common/utils/fix_wsl.ps1’不存在。提供现有“”.ps1“”文件的路径作为-File参数的参数。“”

Cd to the dir and executing PowerShell with file in current dir works though.

Cd到目录,然后使用当前目录中的文件执行PowerShell。

I really recommend that new users (at least) stay away from the "adding a cd in the startup config` hack. I see too many questions from folks who have done this and it messes with them later.

我真的建议新用户(至少)远离“在启动配置中添加CD”的黑客。我看到太多这样做的人提出的问题,这会在以后扰乱他们。

I get this, also. Is there any way to use a full absolute path like this? I don't like having to cd to the directory.

我也知道这个。有没有办法使用这样的完整绝对路径?我不喜欢不得不用CD连接到目录。

I did not find any way to call with full absolute path, and I don't like to have to cd too... Moreover when we want to stay in same folder, we have to cd - (or cd - > /dev/null if we don't want to have new path echoed).

我没有找到任何方法来调用完整的绝对路径,我不喜欢不得不CD太…此外,当我们想要保留在同一个文件夹中时,我们必须cd-(或者cd->/dev/NULL,如果我们不想回显新路径的话)。

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com