如何直接在bash脚本中运行PowerShell核心命令?

问题描述

我通常在Az CLI脚本中使用bash命令来管理Azure资源。有时,由于PowerShell core版本不存在,我需要利用Az CLI命令。

我想在Az CLI外壳程序中同时使用PS corebash命令。我的意思是,我想拥有only one bash脚本并在其中执行所有必需的命令。

您知道在bash脚本中直接直接运行PowerShell core命令的方法吗?

解决方法

您不能那样做。但是您可以将Powershell用于Linux(pwsh)-https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7

例如从bash开始,您可以调用该程序并传递命令

$ pwsh -c "write-host Hello world"
Hello world
,

通常,如果需要执行许多命令,我会从bash内部调用PowerShell脚本,但是如果您只想执行一个命令,则可以运行pwsh -command COMMAND 例: pwsh -command get-childitem