未加载指定的模块“MSOnline”,因为在任何模块目录中都找不到有效的模块文件

cyr*_*ilv 5 powershell .net azure microsoft-office-365 azure-active-directory

对于我的一个应用程序,我有一个 Windows 服务(在 Windows Server 2012 R2 x64 上),它的作用是在不同的计划和触发器上执行许多作业。其中之一是在 Office 365 上重置用户密码。运行该服务的服务器安装了 Microsoft Online Services 登录助手和用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块(MSOnline 版本 1.1.166.0)。

从 PowerShell 我可以成功地与我的用户调用以下内容。

$> $cred = Get-Credential
$> Connect-MsolService -Credential $cred
Run Code Online (Sandbox Code Playgroud)

如果我以帐户身份运行 PowerShell,我的服务启动时也能正常运行。从 Windows 服务运行重置密码功能失败并显示以下消息:

The term 'Connect-MsolService' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Run Code Online (Sandbox Code Playgroud)

在代码中为模块导入添加调试标志允许我们将问题拖到 import-module MSOnline 命令。我们得到了错误:

The specified module 'MSOnline' was not loaded because no valid module file was found in any module directory.
Run Code Online (Sandbox Code Playgroud)

我们已经尝试在 x64 版本中删除并重新安装这两个工具(Microsoft Online Services 登录助手和用于 Windows PowerShell 的 Microsoft Azure Active Directory 模块)。系统变量“PATH”是正确的:%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft\Web Platform安装程序\

文件夹 C:\Windows\System32\WindowsPowerShell\v1.0\Modules 包含 MSOL 模块子文件夹 MSOnline 和 MSOnlineExtended

复制 C:\Windows\sysWOW64\WindowsPowerShell\v1.0\Modules 中的两个文件夹作为解决方案在许多其他主题中失败,并出现以下错误:

System.Management.Automation.CmdletInvocationException: Could not load file or assembly 'file:///C:\Windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format. ---> System.BadImageFormatException: Could not load file or assembly 
Run Code Online (Sandbox Code Playgroud)

用于 Windows 服务的代码已作为单独工具在同一台服务器上成功运行和测试,并且该代码也在开发人员机器上完美运行。

这些调查使我们认为服务本身存在某种问题,但无法弄清楚是什么/为什么。

谢谢你。

cyr*_*ilv 1

我们已经发现问题了。在项目属性中将目标平台构建参数更改为“首选 32 位”解决了该问题。