首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Office 365:使用Power Shell脚本问题连接到Office Online 365服务

Office 365:使用Power Shell脚本问题连接到Office Online 365服务
EN

Stack Overflow用户
提问于 2012-04-16 13:36:09
回答 2查看 8.9K关注 0票数 3

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

我的脚本是:

代码语言:javascript
复制
$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#代码:

代码语言:javascript
复制
  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、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

有什么东西丢了吗?

谢谢

EN

回答 2

Stack Overflow用户

发布于 2012-04-16 14:22:57

代码语言:javascript
复制
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "MSOnline" });

using (Runspace runspace = RunspaceFactory.CreateRunspace(iss))
{
// blah
}
票数 1
EN

Stack Overflow用户

发布于 2016-01-11 22:01:40

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

代码语言:javascript
复制
 1. SharePoint Online Management Shell
 2. Microsoft Online Services Sign-In Assistant version 7.0 or greater version
 3. Windows Azure Active Directory Module
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10169140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档