0

早上好,我在尝试在 ASP.NET 中加载名为 MSOnline 的 Powershell 模块时遇到了一个问题。

  • 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
  • 这是一个通过 C# 中的 Code Behind 运行脚本的按钮。当我尝试加载模块时,它引发了异常。

     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);
            }
        }
    

    我需要的是可能出错的地方。我引用了 system.management.automation (dll)。但我不知道如果安装了它为什么没有列出。感谢您的帮助。

    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...

    4

    1 回答 1

    3

    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.

    于 2014-10-20T01:20:37.167 回答