This is the correct logic for getting all the Distribution groups and selecting the PrimarySMTPAddress and group name. Sometimes the group name returns GUIDS and not the correct group name. Any help would be great.
public List<ExchangeGroup> GetDistributionGroup()
{
List<ExchangeGroup> ExchangeGroup = new List<ExchangeGroup>();
ClearPowerShell();
powerShell.AddCommand("Get-DistributionGroup")
.AddParameters(new Dictionary<string, object>()
{
{ "ResultSize", "Unlimited" }
})
.AddCommand("Select-Object")
.AddArgument(new string[] { "PrimarySMTPAddress", "Name" });
Collection<PSObject> results = powerShell.Invoke();
foreach (var result in results)
{
ExchangeGroup emailGroup = new ExchangeGroup();
var propertyName = result.Properties.Where(w => w.Name == "Name").Select(s => s.Value).FirstOrDefault();
var propertyEmail = result.Properties.Where(w => w.Name == "PrimarySmtpAddress").Select(s => s.Value).FirstOrDefault();
emailGroup.ExchangeName = (string)propertyName;
emailGroup.ExchangeEmail = (string)propertyEmail;
ExchangeGroup.Add(emailGroup);
}
if (powerShell.HadErrors)
{
var exception = powerShell.Streams.Error.ElementAt(0).Exception;
}
return ExchangeGroup;
}
This is a planned change, you should use the displayname property for consistent human readable data.
I'm not saying this was a good change, it is what it is. This burned me and many others out there with existing scripts/automation.