在 Powershell 中-
如何在 bash 脚本(.sh)中运行 powershell 脚本(.ps1)
我可以只运行 powershell 脚本-
& .\scriptfile.ps1
以及 bash 脚本。
但是当我尝试在 bash 脚本中运行 powershell 时,我得到了
file.ps1:找不到命令
两个脚本都位于同一路径中。
你在努力吗
.\scriptfile.ps1
? 应该是这样
./scriptfile.ps1
而且,当从 bash 脚本调用 powershell 时,您需要运行 pwsh 命令,例如
pwsh ./scriptfile.ps1
或者 Powershell 脚本文件的第一行应该是 shebang(解释器指令),例如:
#!/usr/bin/env pwsh
我在 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
尝试更改权限并使其可以执行
chmod +x file.ps1
# 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
在我的系统中,“powershell”调用 v5,“pwsh”调用 v7。 如果安装了 v7,则不会删除 v5。 这可能会消除一些混乱。 此外,如果从 bash 中运行 pwsh,wsl(Linux 的 Windows 子系统)可能能够调用 powershell,并且 linux 中的 sudo 比 pwsh 中的 admin 更合适。