0

I'm attempting to connect to an Office 365 Group and list the membership of the Group using Powershell through a C# Project.

From this article, I've determined the command I need to use is

Get-UnifiedGroupLinks –Identity groupalias –LinkType Members

Here is my current code:

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)
    {

    }
}

Variables used above, declaration not shown: "UserName", "Password", "groupAddress"

I am able to make a connection to the service, but when I try to get the group members I get the error "A parameter cannot be found that matches parameter name 'Identity'"

I'm not sure how to proceed in troubleshooting my code. I've tried the Group email, Group Alias, and Group Display Name in the Identity parameter. Perhaps I have something else wrong?

I'm using the following Libraries in Visual Studio 2017 on a Windows 10 machine:

using System.Management.Automation;
using System.Management.Automation.Runspaces;
MrMatt
  • 175
  • 2
  • 12

2 Answers2

0

Usually I get that error when I'm passing the variable with some extra characters. Check how the variable is being passed, if its just "email" or if it contains other info like @{"email"}. Very common in Powershell.

Hope that sheds some light. Because the command is very simple:

Get-UnifiedGroupLinks -Identity [name] -LinkType members
Alex
  • 498
  • 2
  • 8
  • I double checked the variable doesn't have anything special to it. I even tried putting the email address directly into the command. Unfortunately, the same error occurs. – MrMatt Apr 21 '17 at 11:18
  • Oh sorry, its saying that it doesn't recognize that flag "identity" what if you add "-Identity" instead of just "identity"? – Alex Apr 21 '17 at 15:52
  • Hi Alex, I did try with the '-Identity', it didn't seem to make a difference, but I found a possible solution. I added the following code before I look to group members, and it started working. PSCommand ImportSession = new PSCommand(); ImportSession.AddCommand("Import-PSSession"); ImportSession.AddParameter("Session", SessionHolder); powershell.Commands = ImportSession; powershell.Invoke(); – MrMatt Apr 21 '17 at 17:10
  • 1
    Oh that makes sense. It when you connected, it didn't launch the new PSsession that it loaded. Glad it got fixed! – Alex Apr 26 '17 at 14:20
0

I found a bit of code that makes things work... It slows it down, so I'm not sure if there is a better way, but here is my updated code:

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
    }
}

The key seems to be that I need to Import the session before I can begin using it.

MrMatt
  • 175
  • 2
  • 12