Inconsistent behaviour Powershell Get-Recipient (Exchange 2010)

N

NZSchoolTech

Good day,

If I open a Powershell remote session to Outlook Live and type
get-Recipient -recipientType mailboxuser, mailcontact
It returns a list of mailbox users and mail contacts as expected.

If I create a C# application to call remote powershell and pass it the same
command and parameters as above
Instead of returning a list of mailbox users and mail contacts, it returns
ONLY the list of MailUniversalDistributionGroup recipient types instead.

It looks like the only way in a C# application I can get the recipients I
want is to get all of them and iterate the collection and throw out the ones
that are not those two types that I want (as listed above).

--
 
N

NZSchoolTech

Pretty simple, it is a console app at the moment (for testing)

static void Main(string[] args)
{
string CredentialName = "something";
string passwd = "something";

SecureString password = new SecureString();

foreach (char c in passwd) {password.AppendChar(c);}

PSCredential credential = new PSCredential(CredentialName,
password);

Uri liveIdconnectionUri = new
Uri("https://pod51003psh.outlook.com/PowerShell-LiveID?PSVersion=2.0");

// Set the connection Info
WSManConnectionInfo connectionInfo = new
WSManConnectionInfo(liveIdconnectionUri,
"http://schemas.microsoft.com/powershell/Microsoft.Exchange",
credential);

connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;

// create a runspace on a remote path
// the returned instance must be of type RemoteRunspace

Runspace runspace =
System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);

PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
command.AddCommand("Get-Recipient");
command.AddParameter("RecipientType", "usermailbox, mailcontact");
powershell.Commands = command;
try
{
// open the remote runspace
runspace.Open();
// associate the runspace with powershell
powershell.Runspace = runspace;
// invoke the powershell to obtain the results
Collection<PSObject> results = powershell.Invoke();

//spew out some data

foreach (PSObject obj in results)
{
Console.Write(obj.Members["DisplayName"].Value.ToString());
Console.Write(" ");
Console.Write(obj.Members["RecipientType"].Value.ToString());
Console.WriteLine();
}

Console.WriteLine();
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
finally
{
// dispose the runspace and enable garbage collection
runspace.Dispose();
runspace = null;
// Finally dispose the powershell and set all variables to null to
free
// up any resources.
powershell.Dispose();
powershell = null;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top