【发布时间】:2019-08-01 20:36:03
【问题描述】:
我正在尝试在我的 C# 应用程序中获取有关 MFA 的信息。我已经在 Powershell 中取得了令人满意的结果,但我正在努力在 C# 中做同样的事情。
我在 Powershell 中的代码:
Get-MsolUser -SearchString c.test@mytenant.com |
Where-Object {$_.StrongAuthenticationRequirements -like “*”} |
Select-Object UserPrincipalName, DisplayName, @{n='MFA';e=
{$_.StrongAuthenticationRequirements.State}}, @{n='Methods'; e=
{($_.StrongAuthenticationMethods).MethodType}}, @{n='Default Method'; e=
{($_.StrongAuthenticationMethods).IsDefault}}
UserPrincipalName : c.test@mytenant.com
DisplayName : Cyril test
MFA : Enforced
Methods : {OneWaySMS, TwoWayVoiceMobile, PhoneAppOTP,
PhoneAppNotification}
Default Method : {False, False, False, True}
如您所见,我得到了 MFA 状态和使用的方法。
现在,我想在 C# 中做同样的事情。
我的功能:
public static List<string> GetMFA(Runspace runspace, string nom)
{
List<string> listResult = new List<string>();
try
{
Command getLicenseCommand = new Command("Get-MsolUser");
getLicenseCommand.Parameters.Add(new CommandParameter("SearchString", nom));
var pipe = runspace.CreatePipeline();
pipe.Commands.Add(getLicenseCommand);
var props = new string[] { "displayname", "userprincipalname", "StrongAuthenticationRequirements" };
Command CommandSelect = new Command("Select-Object");
CommandSelect.Parameters.Add("Property", props);
pipe.Commands.Add(CommandSelect);
// Execute command and generate results and errors (if any).
Collection<PSObject> results = pipe.Invoke();
if (results.Count != 0)
{
var error = pipe.Error.ReadToEnd();
if (error.Count > 0)
{
throw new Exception(error[0].ToString());
}
foreach (PSObject resultat in results)
{
string dn = resultat.Properties["displayname"].Value.ToString();
string upn = resultat.Properties["userprincipalname"].Value.ToString();
string mfa = resultat.Properties["StrongAuthenticationRequirements"].Value.ToString();
string res = dn + '/' + upn + '/' + mfa;
listResult.Add(res);
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return listResult;
}
“StrongAuthenticationRequirements”属性没有返回“Enforced”之类的内容,而是
System.Collections.Generic.List`1[Microsoft.Online.Administration.StrongAuthenticationRequirement]
我在这里错过了什么?
【问题讨论】:
-
您是否费心探索列表及其对象?
-
@Seth 感谢您的回答。我没有设法遍历它似乎是一个列表。即使是调试模式下的 Visual Studio 工具也不能比这更进一步。无论如何,我设法通过直接查询 powershell 命令得到了一些结果,并且我得到了原始结果中的所有信息
标签: c# powershell