Discussion:
Exchange 2007 - Getting mailbox size from enterprise service
(too old to reply)
Brian Clayton
2008-01-08 14:20:31 UTC
Permalink
I am attempting to retrieve the mailbox size for a specified account using
PowerShell from a .NET enterprise service (running as a domain admin). The
same code works fine from a console application, but I always get an error
like "The specified mailbox database "..." does not exist" at the call to
invoke Get-MailboxStatistics from the enterprise service. Here is the code:

string totalItemSize = "";
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info =
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out
snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
RunspaceInvoke ri = new RunspaceInvoke(myRunSpace);
IList errors;
Collection<PSObject> commandResults =
ri.Invoke("Get-MailboxStatistics -Identity \"someuser\"", null, out errors);
foreach (PSObject cmdlet in commandResults)
{
totalItemSize = cmdlet.Properties["TotalItemSize"].Value.ToString();
}

Any ideas?

Thanks,
Brian
Henning Krause [MVP - Exchange]
2008-01-09 21:05:05 UTC
Permalink
Hi,

this won't work because some sort of impersonation issues... the same thing
happened with CDOEXM on Exchange 2003.

You need to seperate the logic from your ASP.NET application and put it in a
COM+ application. This way, the powershell code runs in a separate process.

If you need more info on this, feel free to drop me a mail. I should have
some code somewhere...

Kind regards,
Henning Krause
Post by Brian Clayton
I am attempting to retrieve the mailbox size for a specified account using
PowerShell from a .NET enterprise service (running as a domain admin). The
same code works fine from a console application, but I always get an error
like "The specified mailbox database "..." does not exist" at the call to
string totalItemSize = "";
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info =
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out
snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
RunspaceInvoke ri = new RunspaceInvoke(myRunSpace);
IList errors;
Collection<PSObject> commandResults =
ri.Invoke("Get-MailboxStatistics -Identity \"someuser\"", null, out errors);
foreach (PSObject cmdlet in commandResults)
{
totalItemSize = cmdlet.Properties["TotalItemSize"].Value.ToString();
}
Any ideas?
Thanks,
Brian
Henning Krause [MVP - Exchange]
2008-01-09 21:16:24 UTC
Permalink
Hi,

just ignore my last post.... I didn't get that you are already working in a
COM+ application...

Henning
Post by Henning Krause [MVP - Exchange]
Hi,
this won't work because some sort of impersonation issues... the same
thing happened with CDOEXM on Exchange 2003.
You need to seperate the logic from your ASP.NET application and put it in
a COM+ application. This way, the powershell code runs in a separate
process.
If you need more info on this, feel free to drop me a mail. I should have
some code somewhere...
Kind regards,
Henning Krause
Post by Brian Clayton
I am attempting to retrieve the mailbox size for a specified account using
PowerShell from a .NET enterprise service (running as a domain admin). The
same code works fine from a console application, but I always get an error
like "The specified mailbox database "..." does not exist" at the call to
string totalItemSize = "";
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info =
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin",
out snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
RunspaceInvoke ri = new RunspaceInvoke(myRunSpace);
IList errors;
Collection<PSObject> commandResults =
ri.Invoke("Get-MailboxStatistics -Identity \"someuser\"", null, out errors);
foreach (PSObject cmdlet in commandResults)
{
totalItemSize = cmdlet.Properties["TotalItemSize"].Value.ToString();
}
Any ideas?
Thanks,
Brian
Henning Krause [MVP - Exchange]
2008-01-09 22:12:37 UTC
Permalink
Hi Brian,

I've just put your code into a serviced component:

[ProgId("Example.MailboxStatistics"), ComVisible(true)]
public class MailboxStatistics: ServicedComponent, IMailboxStatistics
{
public string GetMailboxSize(string username)
{
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException;

rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin",
out snapInException);

using (Runspace myRunSpace =
RunspaceFactory.CreateRunspace(rsConfig))
{
myRunSpace.Open();
using (Pipeline pipeLine = myRunSpace.CreatePipeline())
{
Command command;

command = new Command("Get-MailboxStatistics");
command.Parameters.Add("Identity", username);

pipeLine.Commands.Add(command);

Collection<PSObject> commandResults = pipeLine.Invoke();

foreach (PSObject cmdlet in commandResults)
{
PSPropertyInfo value =
cmdlet.Properties["TotalItemSize"];
return value.Value.ToString();
}

return "nothing";
}
}
}
}

Just slight modifications to your code.... but It works when I put it in a
COM+ application and call it from a website.

Kind regards,
Henning Krause
Post by Brian Clayton
I am attempting to retrieve the mailbox size for a specified account using
PowerShell from a .NET enterprise service (running as a domain admin). The
same code works fine from a console application, but I always get an error
like "The specified mailbox database "..." does not exist" at the call to
string totalItemSize = "";
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
PSSnapInInfo info =
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out
snapInException);
Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
RunspaceInvoke ri = new RunspaceInvoke(myRunSpace);
IList errors;
Collection<PSObject> commandResults =
ri.Invoke("Get-MailboxStatistics -Identity \"someuser\"", null, out errors);
foreach (PSObject cmdlet in commandResults)
{
totalItemSize = cmdlet.Properties["TotalItemSize"].Value.ToString();
}
Any ideas?
Thanks,
Brian
Brian Clayton
2008-01-10 15:50:19 UTC
Permalink
Hi Henning,
Thanks very much for your response. In trying out your code, I realized what
I was doing wrong; I was using a static method (which of course uses the
caller's security context). Doh!
I changed it to an instance method and everything works great!

Thanks again,
Brian
Post by Henning Krause [MVP - Exchange]
Hi Brian,
[ProgId("Example.MailboxStatistics"), ComVisible(true)]
public class MailboxStatistics: ServicedComponent, IMailboxStatistics
{
public string GetMailboxSize(string username)
{
RunspaceConfiguration rsConfig =
RunspaceConfiguration.Create();
PSSnapInException snapInException;
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out
snapInException);
using (Runspace myRunSpace =
RunspaceFactory.CreateRunspace(rsConfig))
{
myRunSpace.Open();
using (Pipeline pipeLine = myRunSpace.CreatePipeline())
{
Command command;
command = new Command("Get-MailboxStatistics");
command.Parameters.Add("Identity", username);
pipeLine.Commands.Add(command);
Collection<PSObject> commandResults =
pipeLine.Invoke();
foreach (PSObject cmdlet in commandResults)
{
PSPropertyInfo value =
cmdlet.Properties["TotalItemSize"];
return value.Value.ToString();
}
return "nothing";
}
}
}
}
Just slight modifications to your code.... but It works when I put it in a
COM+ application and call it from a website.
Kind regards,
Henning Krause
Continue reading on narkive:
Loading...