我有一个ASP.NET Core Web应用程序(Linux)作为Azure应用程序服务,并且有一个页面尝试安装ExchangeOnlineManagement PowerShell模块。该脚本需要连接到exchange并调用Get-EXOMailbox。脚本报告PowerShell v7.3.6。
我得到这个错误:术语“Install-Module”不能识别为cmdlet、函数、脚本文件或可执行程序的名称
var scriptContents = "if(-not (Get-Module ExchangeOnlineManagement -ListAvailable))" + Environment.NewLine +
"{ " + Environment.NewLine +
"Write-Host $PSVersionTable.PSVersion" + Environment.NewLine +
"Install-Module ExchangeOnlineManagement -Scope CurrentUser -Force" + Environment.NewLine +
"}";
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript(scriptContents);
var pipelineObjects = await ps.InvokeAsync().ConfigureAwait(false);
}
字符串
1条答案
按热度按时间q3qa4bjr1#
上下文:
PowerShellGet
模块,而Install-Module
cmdlet是该模块的一部分。PSModulePath
环境变量($env:PSModulePath
)指向旧版 Windows PowerShell 目录,即使从PowerShell会话 * 外部 * 运行可执行文件,您仍然可以调用Install-Module
,在没有-Scope
参数的情况下,这将导致目标模块的 * 用户级 * 安装,Windows PowerShell会话也会看到。PSModulePath
环境变量,这解释了您的症状。PSModulePath
,但在SDK项目中,它将使用 * 托管应用程序 * 的目录作为其 * 系统 * 模块的目录,即与PowerShell本身捆绑在一起。结果:
托管PowerShell * 的应用程序/库可以 * 通过预先存在的 * 独立 * PowerShell安装 * 找到
PowerShellGet
模块(托管Install-Module
cmdlet):注意事项:
Install-Module
的使用也总是针对模块“借用”的独立PowerShell安装的模块目录-这可能是不希望的。由于不能假定
Install-Module
cmdlet存在于非Windows平台上,因此需要 * 自定义代码 * 来按需下载和安装模块。PowerShellGet
和PackageManagement
模块 * 并 * 将它们与您的应用程序/库捆绑在一起 * -但这将再次安装到与独立PowerShell(核心)安装共享的模块目录中。以下代码自动执行Manual Package Download中记录的手动步骤,以便从PowerShell Gallery直接下载和安装模块:
字符串