首页 > 解决方案 > 从 asp 核心网站导入带有 powershell 的模块

问题描述

我目前正在尝试从我的 asp 核心应用程序中调用一个 powershell 应用程序。

我的第一个命令是 Import-Module 以确保加载了正确的模块。

我尝试了几种方法来做到这一点:

using (PowerShell ps = PowerShell.Create())
    {
        ps.AddCommand("Import-Module AzureRm").Invoke();
    }

这会引发 CommandNotFoundException:

CommandNotFoundException

术语“Import-Module AzureRm”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。

如果我改用 AddScript,我根本不会得到任何反馈:

var result = ps.AddScript("Import-Module AzureRm").Invoke();

结果始终作为空对象返回。

甚至可以在 ASP 核心应用程序中使用 Import-Module 吗?

如何从 AddScript 获得结果而不是空对象?

标签: azurepowershellimport-module

解决方案


推荐阅读