gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:54 25 4
gpt4 key购买 nike

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

我知道自己在周五下午摸不着头脑,希望有人能帮助我...

提前致谢

达伦

最佳答案

首先,您需要从以下链接安装 SharePoint Online Management Shell:https://www.microsoft.com/en-us/download/details.aspx?id=35588 .但您可能还需要从以下链接安装 Microsoft 在线服务登录助手 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());
}
}
}

关于c# - 调用 Get-MsolUser 不返回任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23432335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com