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

本文关键字:任何 结果 返回 Get-MsolUser 调用 | 更新日期: 2023-09-27 18:01: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());
}

油。hadererrors为假,connectOutput始终为空。代码似乎成功运行,但没有返回任何结果。

I have try;

  • 在Windows Powershell中执行相同的命令,我得到了一个预期结果的列表。
  • 拼写错误搜索字符串(只是为了检查命令正在运行和参数正在传递),并产生一个错误,因为我也有
  • 使用ImportPSModule(new[] {"MsOnline"})来确保MsOnline模块可用
  • 其他命令(例如get - msolgroup)并返回空列表

我知道我发现自己在星期五的下午抓耳挠耳,希望别人能帮助我…

提前感谢,

达伦

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

首先,您需要从以下链接安装SharePoint Online Management Shell: https://www.microsoft.com/en-us/download/details.aspx?id=35588。但是,您可能还需要从以下链接安装Microsoft Online Services Sign-In Assistant 7.0或更高版本:https://www.microsoft.com/en-my/download/details.aspx?id=39267,然后需要安装Windows Azure Active Directory Module。

之后可以执行以下代码:

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

尝试在Get-MsolUser后面附加-All