【问题标题】:Office 365: Connecting to Office Online 365 service using Power Shell Script issueOffice 365:使用 Powershell 脚本问题连接到 Office Online 365 服务
【发布时间】:2012-04-27 11:43:42
【问题描述】:

我有 Windows Server 2008 R2 机器,它有 Power Shell v1.0。我想使用带有 C# 的 Power Shell 连接到 MS 365 在线服务。我已安装 Office 365 cmdlet 和 Microsoft Online Services 登录助手。 (参考:http://onlinehelp.microsoft.com/en-us/office365-enterprises/hh124998.aspx#BKMK_install

我的脚本是:

$password = ConvertTo-SecureString "xxxxx" -AsPlainText –Force

$credential = New-Object System.Management.Automation.PsCredential("xxxx@xxxx.onmicrosoft.com",$password)

$cred = Get-Credential -cred $credential

Import-Module MSOnline

Connect-Msolservice -cred $cred

我可以在 Power Shell 命令窗口中成功运行此脚本。但我在 c# 应用程序中运行此脚本时遇到问题。

这是我的 C# 代码:

  public void RunScript()
    {
        StringBuilder ss = new StringBuilder();
        ss.AppendLine("$password = ConvertTo-SecureString \"" + pwd + "\" -AsPlainText –Force");
        ss.AppendLine("$credential = New-Object  System.Management.Automation.PsCredential(\"" + userName + "\",$password)");
        ss.AppendLine("$cred = Get-Credential -cred $credential");
        ss.AppendLine("Import-Module MSOnline");
        ss.AppendLine("Connect-Msolservice -cred $cred");

        using (Runspace runspace = RunspaceFactory.CreateRunspace())
        {
            Collection<PSObject> results = null;
            try
            {
                runspace.Open();
                Pipeline pipeline = runspace.CreatePipeline();
                pipeline.Commands.AddScript(ss.toString());

                results = pipeline.Invoke();
            }
            finally
            {
                runspace.Close();
            }                
        }
    }

我得到以下异常:

“Connect-Msolservice”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。

有什么遗漏吗?

谢谢

【问题讨论】:

    标签: c# sharepoint powershell cmdlets


    【解决方案1】:
    InitialSessionState iss = InitialSessionState.CreateDefault();
    iss.ImportPSModule(new string[] { "MSOnline" });
    
    using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))
    {
    // blah
    }
    

    【讨论】:

    • 感谢您的评论。我也会尝试你的建议。但是,我找到了它无法识别“Connect-Msolservice”命令的原因。原因很简单。我创建了一个控制台应用程序,其平台目标是 x86,而我的开发机器是 Microsoft Server 2008 64 位,我在其中安装了 MS Online Service 模块(64 位)。通过将平台目标更改为任何 cpu 来解决问题。
    • 嗨大卫,我尝试了上述解决方案,但结果相同(没有成功)。顺便提一句。似乎必须从 64 位进程/应用程序执行/运行 MS Online 服务模块(64 位)的 cmdlet。这是我所经历的。如果您对此有任何想法,请分享。
    • 感谢分享,普拉卡什。我遇到了同样的问题。
    • 我遇到了问题,然后,在我的 64 位 Windows 10 机器上,在项目属性中,我将 Build -> Platform Target 设置为 x64,脚本成功导入了模块。
    【解决方案2】:

    请确保您已安装以下内容:

     1. SharePoint Online Management Shell
     2. Microsoft Online Services Sign-In Assistant version 7.0 or greater version
     3. Windows Azure Active Directory Module
    

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 2015-05-06
      • 2019-03-11
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多