简体   繁体   English

如何在 C# 代码中正确使用 PowerShell 模块

[英]How to properly use a PowerShell Module in C# Code

I recently wrote a library in C# which used System.Management.Automation to execute some PowerShell commands.我最近用 C# 编写了一个库,它使用 System.Management.Automation 来执行一些 PowerShell 命令。 More specifically, it invokes cmdlets with MSOnline module to provision an Office 365 license.更具体地说,它使用 MSOnline 模块调用 cmdlet 来提供 Office 365 许可证。

(MSOnline Module is installed on my machine and therefore not imported in the code) (MSOnline 模块安装在我的机器上,因此未在代码中导入)

Project-Setup:项目设置:

不允许嵌入图片

Code:代码:

ProvisionHandler.cs @ WorkWithO365 Library ProvisionHandler.cs @ WorkWithO365

private PowerShell connectToO365(string adminprincipal, string password)
    {
        //Admin Credentials
        PSCredential credential = new PSCredential(adminprincipal, convertToSecureString(password));

        //open Shell
        var ps = PowerShell.Create();

        //Admin-Login
        ps.AddCommand("Connect-MsolService").AddParameter("-Credential", credential);
        ps.Invoke();//Error here

        return ps;
    }

(there are actually more functions but this is not my point) (实际上还有更多功能,但这不是我的观点)

Then I wrote a console application, which references that library:然后我写了一个控制台应用程序,它引用了那个库:

Programm.cs @ PowerConsole Programm.cs @ PowerConsole

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WorkWithO365;

namespace Test1
{
    class Program
    {
        static void Main(string[] args)
        {
            var tenant = "myO365tenant.com";

            var adminpric = "admin.login@" + tenant;
            var adminpw = "somePassword";

            var usrPre = "Console";
            var usrG = "Test";


            ProvisionHandler pro = new ProvisionHandler();
            pro.provision(tenant, adminpric, adminpw, usrPre, usrG);
        }
    }
}

Result: Everything works out just fine.结果:一切正常。

Just to play around, I added a second console Application to the Solution.只是为了玩,我在解决方案中添加了第二个控制台应用程序。 This second Console has the exact same Code as PowerConsole and also references the same library.第二个控制台具有与 PowerConsole 完全相同的代码,并且还引用了相同的库。 But suddenly I get this confusing Error:但突然间我得到了这个令人困惑的错误:

System.Management.Automation.CommandNotFoundException : 'The 'Connect-MsolService' command was found in the module 'MSOnline', but the module could not be loaded. System.Management.Automation.CommandNotFoundException :在模块“MSOnline”中找到“Connect-MsolService”命令,但无法加载该模块。 For more information, run 'Import-Module MSOnline'.'有关详细信息,请运行“导入模块 MSOnline”。

inner Exception 1: BadImageFormatException : 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.内部异常 1: BadImageFormatException :无法加载文件或程序集“file:///C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\MSOnline\\Microsoft.Online.Administration.Automation.PSModule.dll”或其中之一它的依赖。 An attempt was made to load a program with an incorrect format.试图加载格式不正确的程序。

inner Exception 2: 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.内部异常 2: CmdletInvocationException :无法加载文件或程序集“file:///C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\MSOnline\\Microsoft.Online.Administration.Automation.PSModule.dll”或其中之一它的依赖。 An attempt was made to load a program with an incorrect format.试图加载格式不正确的程序。

I found someone with nearly the same problem that had the following solution:我发现有人有几乎相同的问题,有以下解决方案:

it was an issue with the project file itself.这是项目文件本身的问题。 Creating a new one (and copying everything back in again) worked a treat, which also means my MSOnline module is installed correctly and working as intended.创建一个新的(并再次复制所有内容)工作得很好,这也意味着我的 MSOnline 模块已正确安装并按预期工作。

But this did not work out for me .但这对我不起作用 Also I do not understand, how PowerConsole works, while Test1 (exact same code) does not.我也不明白 PowerConsole 是如何工作的,而 Test1(完全相同的代码)则不然。 I need to solve this issue because my aim is to use the WorkWithO365-library in a WCF-Service, hosted in azure.我需要解决这个问题,因为我的目标是在 WCF 服务中使用 WorkWithO365 库,该库托管在 azure 中。

What am I doing wrong here?我在这里做错了什么? may there be another way to import the MSOnline module into the Visual Studio Project to avoid the lookup at "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\MSOnline\\"可能有另一种方法将 MSOnline 模块导入到 Visual Studio 项目中,以避免在“C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules\\MSOnline\\”中查找

All credit goes to BDarley所有功劳都归功于BDarley

I was able to get my C# PowerShell code working after setting the platform target to x64.将平台目标设置为 x64 后,我能够让我的 C# PowerShell 代码工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM