【问题标题】:Running powershell script inside a bash script在 bash 脚本中运行 powershell 脚本
【发布时间】:2020-08-16 00:30:39
【问题描述】:

在 Powershell 中-
如何在 bash 脚本 (.sh) 中运行 powershell 脚本 (.ps1)
我可以只运行 powershell 脚本-
& .\scriptfile.ps1
以及 bash 脚本。
但是当我尝试在 bash 脚本中运行 powershell 时,我得到了
file.ps1:找不到命令
两个脚本位于同一路径中。

【问题讨论】:

  • 所以你运行 .\scriptfile.ps1 并且你的错误说 file.ps1 not found??
  • 没有。它运行良好,没有错误。当我在 .sh 脚本中运行它时,找不到文件错误。

标签: bash powershell


【解决方案1】:

你在努力吗

.\scriptfile.ps1

? 那应该是

./scriptfile.ps1

但是,当从 bash 脚本调用 powershell 时,您需要像运行 pwsh 命令一样

pwsh ./scriptfile.ps1

或者您的 Powershell 脚本文件的第一行应该是 shebang(解释器指令),例如:

#!/usr/bin/env pwsh

How can I use a shebang in a PowerShell script?

【讨论】:

  • 我试过 pwsh ./scriptfile.ps1,我得到 - pwsh: command not found
  • 添加 #!/usr/bin/env pwsh,给我 #!/usr/bin/env : 错误的解释器:没有这样的文件或目录
  • 同样 pwsh ./scriptfile.ps1,失败。 pwsh 未被识别为 cmdlet 的名称.....等等。只是 .\scriptfile.ps1 本身运行没有错误。但是,当它从 .sh 脚本内部运行时会失败。
【解决方案2】:

我在 Windows 10 Pro 上使用 git bash,它是 powershell。 可执行文件在/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe 中,它包含在我的$PATH 中。 所以我可以说

powershell ./scriptfile.ps1
# or inline:
powershell 'my script'

在 Ubuntu-on-Windows bash 中我不得不说

powershell.exe ./scriptfile.ps1

【讨论】:

  • 对我来说,powershell ./scriptfile.ps1 是唯一有效的方法。 powershell scriptfile.ps1 出错,powershell 'scriptfile.ps1' 出错。
【解决方案3】:

尝试更改权限并使用chmod +x file.ps1使其可执行

【讨论】:

  • 我之前尝试过,但我得到 chmod 不是 cmdlet、函数、脚本文件等的名称。
  • 你是用超级用户还是root用户运行的?
  • 我以管理员身份运行 powershell。
  • 我在运行 $ which pwsh 时再次收到相同的消息($ 未被识别为 cmdlet 的名称.....)。还尝试了哪个pwsh。 (不被识别为 cmdlet 等的名称)
  • @hithesh 听起来您是在 Powershell shell 内部运行这些命令;尝试从您的默认 shell(Bash、ZSH 等)运行它们。
【解决方案4】:
# Update the list of packages
sudo apt-get update
# Install pre-requisite packages.
sudo apt-get install -y wget apt-transport-https software-properties-common
# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb
# Update the list of products
sudo apt-get update
# Enable the "universe" repositories
sudo add-apt-repository universe
# Install PowerShell
sudo apt-get install -y powershell
# Start PowerShell
pwsh

【讨论】:

    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2017-01-11
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多