Paul Coan
Paul Coan

Reputation: 302

Exchange Powershell in c#

I have a bunch of powershell commands within c# however one is returning 0 results and I just cant figure it out, so with the power of the internet I am hoping you guys have an answer.

My c# code running the power is as follows

internal static List<ExchangeMailboxes> ExchangeMailboxList(string snapIn)
        {
            List<ExchangeMailboxes> data = new List<ExchangeMailboxes>();
            StringBuilder stringBuild = new StringBuilder();
            stringBuild.AppendLine("$script:WarningPreference = 'SilentlyContinue'");
            stringBuild.AppendLine(
                "Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select DisplayName,@{name='TotalItemSize';expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}},@{name='TotalDeletedItemSize';expression={[math]::Round((($_.TotalDeletedItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}}");
            using (PowerShell inst = PowerShell.Create())
            {
                inst.AddScript("Add-PSSnapin " + snapIn)
                    .AddScript(stringBuild.ToString());
                Collection<PSObject> results = inst.Invoke();
                foreach (PSObject obj in results)
                {
                    data.Add(new ExchangeMailboxes()
                    {
                        Name = obj.Members["DisplayName"].Value.ToString(),
                        InboxSize = obj.Members["TotalItemSize"].Value.ToString(),
                        DeletedSize = obj.Members["TotalDeletedItemSize"].Value.ToString()
                    });
                }
            }
            return data;
        } 

I can confirm that the snapin is loading correctly and if I run the powershell command manually it is all fine and I con confirm there are no rights issues

here is the powershell command in its raw format

Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics | Select DisplayName,@{name='TotalItemSize';expression={[math]::Round((($_.TotalItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}},@{name='TotalDeletedItemSize';expression={[math]::Round((($_.TotalDeletedItemSize.Value.ToString()).Split('(')[1].Split(' ')[0].Replace(',','')/1GB),2)}}

Upvotes: 2

Views: 558

Answers (1)

Trondh
Trondh

Reputation: 3341

The recommended way to interact with Exchange 2010 and newer is to open a session to http://servername/powershell using the microsoft.exchange configuration:

$ExSession = New-PSSession –ConfigurationName Microsoft.Exchange –ConnectionUri ‘http://ExServer1.contoso.com/PowerShell/?SerializationLevel=Full’ -Credential $Credentials –Authentication Kerberos

I've never tried remoting from c# code, but I guess it shouldn't be any different than what your doing now (except for the powershell code itself, of course). Since you're interacting with various "size" attributes in Exchange, it is still recommended to have the management tools installed locally, otherwise those values don't serialize/deserialize properly (you'll find other posts on serverfault on that topic).

Upvotes: 1

Related Questions