0

i try to get a list of email for specific user in exchange server using this PowerShell command via c#

"get-mailbox -Identity username -resultsize unlimited | select Name -expand EmailAddresses | Select SmtpAddress"

this work in powershell console

but if i call Invoke on PowerShell no PsObject is return is there a way to resolve that ?

I try only calling this script "get-mailbox -Identity user" i got a PSObject but i have to do this

    Collection<PSObject> getInfo = new Collection<PSObject>();
    getInfo = runExchCmd(Pscmd); // runExchCmd just a function to invoke PowerShellCommand to be execute and return Collection PSObject
    PSMemberInfo psobjEmail = getInfo[0].Members["EmailAddresses"];
    object obj = psobjEmail.Value;

but yeah the value is some kind of a long string with all email associate to the user and it look like a pain to just fetch each email into array any help?

DarkVision
  • 1,373
  • 3
  • 20
  • 33

1 Answers1

1

In my experience running powershell and Microsoft Exchange code in C# can be a little tricky. Here is what I had to do:

In your App.Config add the useLegacyV2RuntimeActivationPolicy="true" to the startup tag like so(you don't have to have .net 4.5) This allows the Microsoft.Exchange.Management.PowerShell.Admin to load properly(which you must have the Exchange Tools installed on the local computer to run).:

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

Then, assuming you have the System.Management.Automation reference added to your project you can structure your Powershell query like so in C#:

    string username = "username";

    using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        PowerShellInstance.AddScript("param ([string]$user); add-pssnapin 'Microsoft.Exchange.Management.PowerShell.Admin';" +
            "get-mailbox -Identity $user -resultsize unlimited | select Name -expand EmailAddresses | Select SmtpAddress;");

        PowerShellInstance.AddParameter("user", username);
        Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
        if (PowerShellInstance.Streams.Error.Count > 0)
        {
            foreach (var error in PowerShellInstance.Streams.Error)
            {
                //for reading errors in debug mode
                StringBuilder sb = new StringBuilder();
                sb.AppendLine(error.Exception.Message);
                sb.AppendLine(error.Exception.InnerException.Message);
                sb.AppendLine(error.CategoryInfo.Category.ToString());
                sb.AppendLine(error.CategoryInfo.Reason);
                sb.AppendLine(error.ErrorDetails.Message);
                sb.AppendLine(error.ErrorDetails.RecommendedAction);
                Console.WriteLine(sb.ToString());
            }
        }
        if (PSOutput.Count > 0)
        {
            foreach (var item in PSOutput)
            {
                Console.WriteLine(item);
            }
        }
    }

I also had to change my project to build as x64 because the powershell addin for Exchange is 64-bit.

Tyler Jennings
  • 8,761
  • 2
  • 44
  • 39