在 C# 中使用 PowerShell 读取 Office 365 组成员

Using PowerShell to Read Office 365 Group Members in C#

我正在尝试连接到 Office 365 组并通过 C# 项目使用 Powershell 列出该组的成员。

From this article,我确定我需要使用的命令是

Get-UnifiedGroupLinks –Identity groupalias –LinkType Members

这是我当前的代码:

string connectionUri = "https://outlook.office365.com/powershell-liveid/";
SecureString secpassword = new SecureString();
foreach (char c in Password)
{
    secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(UserName, secpassword);

Runspace runspace = RunspaceFactory.CreateRunspace();
PSObject SessionHolder = null;
using (PowerShell powershell = PowerShell.Create())
{
    PSCommand command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri(connectionUri));
    command.AddParameter("Credential", credential);
    command.AddParameter("Authentication", "Basic");
    powershell.Commands = command;

    runspace.Open();
    powershell.Runspace = runspace;
    Collection<System.Management.Automation.PSObject> result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 || result.Count != 1)
    {
        throw new Exception("Fail to establish the connection");
    }
    else
        SessionHolder = result[0];
}
using (PowerShell powershell = PowerShell.Create())
{
    PSCommand command = new PSCommand();
    // –Identity groupalias –LinkType Members
    command = new PSCommand();
    command.AddCommand("Invoke-Command");
    command.AddParameter("ScriptBlock", System.Management.Automation.ScriptBlock.Create("Get-UnifiedGroupLinks"));
    command.AddParameter("Session", SessionHolder);
    command.AddParameter("Identity", groupAddress);
    command.AddParameter("LinkType", "Members");
    powershell.Commands = command;
    powershell.Runspace = runspace;

    Collection<PSObject> PSOutput = powershell.Invoke();
    // loop through each output object item
    foreach (PSObject outputItem in PSOutput)
    {

    }
}

上面使用的变量,未显示声明:"UserName"、"Password"、"groupAddress"

我可以连接到该服务,但是当我尝试获取组成员时出现错误 "A parameter cannot be found that matches parameter name 'Identity'"

我不确定如何继续对我的代码进行故障排除。我在身份参数中尝试了群组电子邮件、群组别名和群组显示名称。也许我还有其他问题?

我在 Visual Studio 2017 年的 Windows 10 机器上使用了以下库:

using System.Management.Automation;
using System.Management.Automation.Runspaces;

通常我在传递带有一些额外字符的变量时会出现该错误。检查变量是如何传递的,如果它只是 "email" 或者它是否包含其他信息,如 @{"email"}。在 Powershell 中很常见。

希望能有所启发。因为命令很简单:

Get-UnifiedGroupLinks -Identity [name] -LinkType members

我找到了一些让事情正常运行的代码...它减慢了速度,所以我不确定是否有更好的方法,但这是我更新的代码:

string connectionUri = "https://outlook.office365.com/powershell-liveid/";
SecureString secpassword = new SecureString();
foreach (char c in Password)
{
    secpassword.AppendChar(c);
}
PSCredential credential = new PSCredential(UserName, secpassword);

Runspace runspace = RunspaceFactory.CreateRunspace();
PSObject SessionHolder = null;
using (PowerShell powershell = PowerShell.Create())
{
    string connectionUri = "https://outlook.office365.com/powershell-liveid/";
    SecureString secpassword = new SecureString();
    foreach (char c in Password)
    {
        secpassword.AppendChar(c);
    }
    PSCredential credential = new PSCredential(UserName, secpassword);
    PSCommand command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri(connectionUri));
    command.AddParameter("Credential", credential);
    command.AddParameter("Authentication", "Basic");
    powershell.Commands = command;

    runspace.Open();
    powershell.Runspace = runspace;
    Collection<System.Management.Automation.PSObject> result = powershell.Invoke();
    if (powershell.Streams.Error.Count > 0 || result.Count != 1)
        throw new Exception("Fail to establish the connection");
    else
        SessionHolder = result[0];

    PSCommand ImportSession = new PSCommand();
    ImportSession.AddCommand("Import-PSSession");
    ImportSession.AddParameter("Session", SessionHolder);
    powershell.Commands = ImportSession;
    powershell.Invoke();

    PSCommand GrabGroup = new PSCommand();
    GrabGroup.AddCommand("Get-UnifiedGroupLinks");
    GrabGroup.AddParameter("Identity", GroupAddress);
    GrabGroup.AddParameter("LinkType", "Members");
    powershell.Commands = GrabGroup;
    Collection<PSObject> PSOutput_GroupMembers = powershell.Invoke();
    foreach (PSObject outputItem in PSOutput_GroupMembers)
    {
        //Process Members
    }
}

关键似乎是我需要导入会话才能开始使用它。