【问题标题】:Calling Get-MsolUser returns no results调用 Get-MsolUser 不返回任何结果
【发布时间】:2014-06-19 09:15:16
【问题描述】:

我正在为一个将管理大量 Office365 帐户的系统编写一些概念验证代码,但是,我似乎在遇到一个相当愚蠢的问题。

我正在使用 RunspaceFactory 在 Office 365 上触发我的 Powershell 命令,虽然代码似乎正在运行且没有任何错误,但我再也没有得到用户列表。

首先这是我的代码....

Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();

Pipeline pipeline = runSpace.CreatePipeline();

Command msolConnect = new Command("Connect-MsolService");

System.Security.SecureString securePassword = new System.Security.SecureString();
foreach (char pwdLetter in password)
{
    securePassword.AppendChar(pwdLetter);
}
PSCredential credential = new PSCredential(username, securePassword);
msolConnect.Parameters.Add("Credential", credential);
pipeline.Commands.Add(msolConnect);

Command msolGetUser = new Command("Get-MsolUser");
msolGetUser.Parameters.Add("SearchString", "hayley");
pipeline.Commands.Add(msolGetUser);


Collection<PSObject> connectOutput = pipeline.Invoke();
foreach (PSObject psObject in connectOutput)
{
    Console.WriteLine(psObject.Members["DisplayName"].Value.ToString());
}

pipline.HadErrors 为 false,connectOutput 始终为空。代码似乎成功运行,但没有返回任何结果。

我试过了;

  • Windows Powershell 中的相同命令,我得到了预期结果列表。
  • SearchString 拼写错误(只是为了检查命令是否正在运行以及是否正在传递参数)并生成错误,正如我所期望的那样
  • 使用 ImportPSModule(new[] { "MsOnline" }) 确保 MSOnline 模块可用
  • 其他命令(例如 Get-MsolGroup)并返回一个空列表

我知道自己在星期五下午摸不着头脑,希望其他人可以帮助我...

提前致谢,

达伦

【问题讨论】:

  • 您可能已经有了这些想法,但是:1)在结果回来之前管道是否有可能关闭? 2) 有机会检查管道运行的安全上下文(特别是:这如何影响 PS 配置文件)?
  • 感谢@Aidanapword,据我所知,当返回结果时,管道仍处于打开状态。我很确定这不是安全上下文问题,但今天下午会进行一些测试。
  • 安全上下文看起来也不错...
  • @Darren 这是如何解决的?

标签: c# powershell office365 user-management


【解决方案1】:

首先,您需要通过以下链接安装 SharePoint Online 命令行管理程序:https://www.microsoft.com/en-us/download/details.aspx?id=35588。但您可能还需要通过以下链接安装 Microsoft Online Services 登录助手 7.0 或更高版本:https://www.microsoft.com/en-my/download/details.aspx?id=39267,然后需要安装 Windows Azure Active Directory 模块。

之后你可以按照下面的代码:

InitialSessionState iss = InitialSessionState.CreateDefault();
            iss.ImportPSModule(new[] { "MSOnline" });
            using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
            {
                Pipeline pipeline = myRunSpace.CreatePipeline();
                myRunSpace.Open();


                // Execute the Get-CsTrustedApplication cmdlet.
                using (System.Management.Automation.PowerShell powershell = System.Management.Automation.PowerShell.Create())
                {
                    powershell.Runspace = myRunSpace;
                    Command connect = new Command("Connect-MsolService");
                    System.Security.SecureString secureString = new System.Security.SecureString();
                    string myPassword = "Password";
                    foreach (char c in myPassword)
                        secureString.AppendChar(c);

                    connect.Parameters.Add("Credential", new PSCredential("admin@domain.microsoftonline.com", secureString));
                    powershell.Commands.AddCommand(connect);


                    Collection<PSObject> results = null;
                    Collection<ErrorRecord> errors = null;
                    results = powershell.Invoke();
                    errors = powershell.Streams.Error.ReadAll();
                    powershell.Commands.Clear();
                    Command getuser = new Command("Get-MsolUser");
                    getuser.Parameters.Add("MaxResults", 4);
                    powershell.Commands.AddCommand(getuser);
                    results = null;
                    errors = null;
                    results = powershell.Invoke();

                    foreach (PSObject psObject in results)
                    {
                        Console.WriteLine(psObject.Members["DisplayName"].Value.ToString());
                    }
                }
            }

【讨论】:

    【解决方案2】:

    尝试使用 -All 附加 Get-MsolUser

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2018-01-09
      • 2012-09-26
      • 1970-01-01
      相关资源
      最近更新 更多