How to properly use a PowerShell Module in C# Code

Viewed 1449

I recently wrote a library in C# which used System.Management.Automation to execute some PowerShell commands. More specifically, it invokes cmdlets with MSOnline module to provision an Office 365 license.

(MSOnline Module is installed on my machine and therefore not imported in the code)

Project-Setup:

not allowed to embed pictures

Code:

ProvisionHandler.cs @ WorkWithO365 Library

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

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. 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. For more information, run 'Import-Module 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. 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. 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.

But this did not work out for me. Also I do not understand, how PowerConsole works, while Test1 (exact same code) does not. I need to solve this issue because my aim is to use the WorkWithO365-library in a WCF-Service, hosted in 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\"

1 Answers

All credit goes to BDarley

I was able to get my C# PowerShell code working after setting the platform target to x64.

Related