Search code examples
c#asp.netpowershellmoduleoffice365-apps

Error while Trying to load Powershell Module "MSOnline" in ASP.NET web app


Good morning I have one problem trying o load a Powershell Module called MSOnline in a ASP.NET After following this links:

  • http://www.msdigest.net/2012/03/how-to-connect-to-office-365-with-powershell/
  • http://blogs.technet.com/b/educloud/archive/2013/01/23/managing-office-365-for-education-using-powershell.aspx
  • This is a Button that run a script via Code Behind in C#. When I was trying to load the module it threw an exception.

     protected void Bexecute_Click(object sender, EventArgs e)
        {
    
            //            Import-Module MSOnline
            try
            {
                //Loading MSOnline using a InitialSessionState
                InitialSessionState iss = InitialSessionState.CreateDefault();
                iss.ImportPSModule(new string[] { "MSOnline" });
                //Before the solution iss has an exception of "not found" the MSOnline Module
    
                //Create a runspace
                Runspace test = RunspaceFactory.CreateRunspace(iss);
    
                //Open a Runspace
                test.Open();
                //Create a Pipeline 
                Pipeline pipeLine = test.CreatePipeline();
    
                //Create the StringBuilder to get append the script
                StringBuilder script = new StringBuilder();
    
                //Take the script from Tscript (TextBoxt with ID="Tscript" in default.aspx
                script.AppendLine(@"Get-Module -ListAvailable");
                //Add the script and commands to the pipeline;
    
                pipeLine.Commands.AddScript(script.ToString());
    
                //Execute script and get the PSObject Results collection
                Collection<PSObject> resultObjects = pipeLine.Invoke();
    
                //Close the Runspace
                test.Close();
    
                if (resultObjects.Count > 0)
                {
                /*** DO SOMETHING WITH THE PSObjects ***/
                }
            }
            catch (System.Management.Automation.RuntimeException ex)
            {
                status.AppendLine(ex.Message);
            }
        }
    

    What I need is what could be wrong. I referenced the system.management.automation (dll). But I don't have a clue why is not listed if it's installed it. Thank you for the help.

    I have found MSOnline .dll files in : C:\windows\system32\WindowsPowerShell\v1.0\Modules\MSOnline But If I tried to add manually from visual studio it says "that MSOnline doesn't exist". This is quite weird, because all have same permissions to folder. What's going on! :S This is so frustrating...


    Solution

  • To get ASP.NET web app run under x64 bit architecture you should do this:

    1, MS Online Services Assistant needs to be downloaded and installed Microsoft Online Services Sign-In Assistant – 64 bit version http://go.microsoft.com/fwlink/?linkid=236300

    2.MS Online Module for PowerShell needs to be downloaded and installed Microsoft Online Services Module for Windows PowerShell (64-bit version) http://go.microsoft.com/fwlink/?linkid=236297

    After this step you can run: get-module -ListAvailable and the Module MSOnline will be listed. But it is still not working on 64 bits SOs. the original poster used win8 64bits and I am using win 8.1 64 bits and it works as well.

    1. Copy the MSOnline module folder from (Path1) to (Path2) where

      (Path1) -> "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline"

      and

      (Path2) -> "C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\MSOnline"

    Considering I'm running this on VS2013 under Windows 8.1 and 64 bits arquitecture.

    After doing this the MSOnline Module is listed in the Get-Module -ListAvailable (from webapp, not just for powershell)), so I could import it and use it.