我可以 运行 来自 .ps1 文件的 powershell 命令吗?

Ho can i run a powershell comand from a .ps1 file?

我有以下内容:

$command = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$arguments = @('Set-AzLogicApp -ResourceGroupName "MyResourceGroup" -Name "MyLogicAppName" -State "Disabled" -Force')
& $command $arguments

当我从命令中 运行 它说 Set-AzLogicApp : The term 'Set-AzLogicApp' is not recognized as the name of a cmdlet

这是什么原因?

已排序,我必须 运行 Install-Module -Name Az。感谢大家的帮助

我想了解您为什么要从 .ps1 文件调用 Powershell.exe。无论如何,如果您正在执行 PS 文件,所有已安装的模块都可以执行。您可以简单地导入模块和 运行 命令。

此外,您必须在机器中安装 Joy 后提到的导入 Az.LogicApp。

代码如下所示:

Import-Module -Name "Az.LogicApp"
$command = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$arguments = @('Set-AzLogicApp -ResourceGroupName "MyResourceGroup" -Name "MyLogicAppName" -State "Disabled" -Force')
& $command $arguments

我已经尝试过了,它对我有用。如果您需要任何进一步的帮助,请尝试告诉我。