调用Get-MsolUser不会返回任何结果

时间:2014-05-02 16:01:59

标签: c# powershell office365 user-management

我正在研究一个管理大量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)并返回空列表

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

提前致谢,

的Darren

2 个答案:

答案 0 :(得分:2)

首先,您需要从以下链接安装SharePoint Online Management Shell: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());
                    }
                }
            }

答案 1 :(得分:0)

尝试使用-All

附加Get-MsolUser