在PowerShell中,如何在Bash脚本中运行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
/c/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe
,并包含在我的$PATH
中。
因此,我可以这样说:powershell ./scriptfile.ps1
# or inline:
powershell 'my script'
powershell.exe ./scriptfile.ps1
powershell ./scriptfile.ps1
能够正常工作。
powershell scriptfile.ps1
会出现错误,而 powershell 'scriptfile.ps1'
也会出现错误。 - Infineightchmod +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