作为服务运行时的PowerShell模块找不到 [英] PowerShell module not found when running as a service

查看:817
本文介绍了作为服务运行时的PowerShell模块找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的分配的Office 365许可证用户的项目。有一个web项目,允许管理员查看可用的许可证,并选择适合用户的许可。在后台,有执行实际许可证分配(以及许多其他任务),每15分钟的服务。为什么我们不直接分配许可证的原因是,用户可能没有被同步到Office 365呢。该服务自动稍后再尝试,如果目录同步尚未运行。

I have a project where I assign Office 365 licenses to users. There is a web project that allows the administrators to view the available licenses and choose a license for a user. In the background, there is a service which performs the actual license assignment (and many other tasks) every 15 minutes. The reason why we don't assign the licenses directly is that a user may not have been synchronized to Office 365 yet. The service automatically tries again later if the DirSync hasn't run yet.

回到我的问题:到Office 365的连接使用PowerShell的模块MSOnline。的问题是,这种模块不能由服务加载。我总是得到标准的错误信息:

Back to my problem: The connection to Office 365 is using the PowerShell module "MSOnline". The problem is that this module can't be loaded by the service. I always get the standard error message:

指定的模块MSOnline'未加载,因为没有有效的模块文件中发现了任何模块目录。

The specified module 'MSOnline' was not loaded because no valid module file was found in any module directory.

Web应用程序并没有这个问题。它连接并可以检索可用的许可证,例如。如果我使用code从LINQPad或作为一个小的独立的可执行文件服务应用程序,一切正常了。只有该服务无法加载模块。谁能帮我?

The web application doesn't have this problem. It connects and can retrieve the available licenses, for example. If I use the code from the service application in LINQPad or as a small standalone executable, everything works fine, too. Only the service can't load the module. Can anyone help me?

下面code片段都使用,Web应用程序和服务,以连接到Office 365(code被简化,以增加可读性)。

The following code snippet is used by both, web application and service, to connect to Office 365 (code is simplified to increase readability).

var iss = InitialSessionState.CreateDefault();

iss.ThrowOnRunspaceOpenError = true;
iss.AuthorizationManager = new AuthorizationManager("MyPowerShellInvoker");

iss.ImportPSModule("MSOnline");

_runspace = RunspaceFactory.CreateRunspace(iss);
_runspace.Open();
_invoker = new RunspaceInvoke(_runspace);

using (Pipeline pipeline = _runspace.CreatePipeline())
{
    var cmd = new Command("Connect-MsolService");
    cmd.Parameters.Add("Credential", /* PSCredential object */);

    pipeline.Commands.Add(cmd);
    pipeline.Invoke();
}

PS:的该服务在本地系统帐户下运行的

P.S.: The service is running under the "Local System" account.

推荐答案

是您的服务运行的32位或64位的目标。我在多例符合32/64的问题。

Is your service running as 32 bits or 64 bits target. I met the 32/64 problem in multiple cases.

这篇关于作为服务运行时的PowerShell模块找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆